<commit_before>module Resourcery
  module ControllerExtensions
    module InstanceMethods
      def index
        respond_with collection
      end

      def show
        respond_with resource
      end

      def edit
        respond_with resource
      end

      def update
        resource.update_attributes(params[singular_resource_name])
        respond_with resource
      end

      def new
        respond_with collection.new
      end

      def create
        @user = collection.new(params[singular_resource_name])
        @user.save
        respond_with @user
      end

      def destroy
        resource.destroy
        respond_with resource
      end

    private

      def collection
        instance_variable_get("@#{plural_resource_name}") || begin
          instance_variable_set("@#{plural_resource_name}",
            self.class.resource_class.scoped)
        end
      end

      def resource
        if params[:id]
          instance_variable_get("@#{singular_resource_name}") ||
            instance_variable_set("@#{singular_resource_name}", collection.send(self.class.resource_options[:finder], params[:id]))
        end
      end

      def singular_resource_name
        self.class.resource_class.name.underscore
      end

      def plural_resource_name
        singular_resource_name.pluralize
      end
    end

    module ClassMethods
      def resource_class
        @resource_class
      end

      def resource_options
        @resource_options
      end
    end
  end
end
<commit_msg>Changed how ivars are being set. Also introduced #resource_base.<commit_after>module Resourcery
  module ControllerExtensions
    module InstanceMethods
      def index
        respond_with collection
      end

      def show
        respond_with resource
      end

      def edit
        respond_with resource
      end

      def update
        resource.update_attributes(params[singular_resource_name])
        respond_with resource
      end

      def new
        respond_with collection.new
      end

      def create
        @user = collection.new(params[singular_resource_name])
        @user.save
        respond_with @user
      end

      def destroy
        resource.destroy
        respond_with resource
      end

    protected

      # The starting point. Override this in a controller if necessary.
      #
      def resource_base
        self.class.resource_class.scoped
      end

      def collection
        self.collection_ivar ||= resource_base
      end

      def resource
        if params[:id]
          self.resource_ivar ||= collection.send(self.class.resource_options[:finder], params[:id])
        end
      end

      def singular_resource_name
        self.class.resource_class.name.underscore
      end

      def plural_resource_name
        singular_resource_name.pluralize
      end

      def resource_ivar
        instance_variable_get("@#{singular_resource_name}")
      end

      def resource_ivar=(v)
        instance_variable_set("@#{singular_resource_name}", v)
      end

      def collection_ivar
        instance_variable_get("@#{plural_resource_name}")
      end

      def collection_ivar=(v)
        instance_variable_set("@#{plural_resource_name}", v)
      end
    end

    module ClassMethods
      def resource_class
        @resource_class
      end

      def resource_options
        @resource_options
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.05363423212192263, 'fraction_numerical': 0.0, 'mean_word_length': 2.0747747747747747, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '512049', 'n_tokens_mistral': 917, 'n_tokens_neox': 889, 'n_words': 210}