<commit_before>module Stone
  # Adds the ability to persist any class it is included in
  # === Example
  #
  # class Post
  #   include Stone::Resource
  # 
  #   field :body, String
  # end
  #
  module Resource

    attr_accessor :id

    class << self
      def included(base)
        rsrc_sym = base.to_s.make_key

        @@callbacks ||= Callbacks.new
        @@callbacks.register_klass(base)

        @@store ||= DataStore.new

        base.send(:extend, self)
        base.send(:include, ::Validatable)

        # rspec breaks if its classes have their contructor overwritten
        unless base.to_s.downcase =~ /(spec::example::examplegroup::subclass_\d|blah)/
          # allow object to be created with a hash of attributes...
          # [] allows for obj[attribute] retrieval
          # to_s allows for stupid Rails to work
          base.class_eval <<-EOS, __FILE__, __LINE__
            def initialize(hash = {})
              self.id = self.next_id_for_klass(self.class)
              unless hash.blank?
                update_attributes(hash)
              end
              @@fields[self.model].each do |f|
                instance_variable_set("@"+f[:name].to_s,[]) if f[:klass] == Array
              end
            end

            def to_s
              id
            end

            def [](sym)
              self.send(sym)
            end
          EOS
        end

        unless @@store.resources.include?(rsrc_sym)
          @@store.resources[rsrc_sym] = DataStore.load_data(rsrc_sym)
        end
      end
    end # self

    @@fields = {}

    # Adds a given field to @@fields and inserts an accessor for that
    # field into klass
    # === Parameters
    # +name+<String>::
    #
    def field(name, klass, arg = nil)
      if arg && arg[:unique] == true
        unique = true
      else
        unique = false
      end
      klass_sym = self.to_s.make_key
      unless @@fields[klass_sym]
        @@fields[klass_sym] = [{:name => name, 
                                :klass => klass, 
                                :unique => unique}]
      else
        @@fields[klass_sym] << {:name => name, 
                                :klass => klass, 
                                :unique => unique}
      end
      name = name.to_s
      # add accessor for given field
      unless klass == Array
        self.class_eval <<-EOS, __FILE__, __LINE__
          def #{name}
            @#{name}
          end
          def #{name}=(value)
            @#{name} = value
          end
        EOS
      else
        self.class_eval <<-EOS, __FILE__, __LINE__
          def #{name}
            @#{name}
          end
        EOS
      end
      self.send(:define_method,"next_by_#{name}") {
        self.next(:"#{name}")
      }
      self.send(:define_method,"prev_by_#{name}") {
        self.prev(:"#{name}")
      }
    end # field

    def model
      self.class.to_s.downcase.to_sym
    end<commit_msg>Code cleanup<commit_after>module Stone
  # Adds the ability to persist any class it is included in
  # === Example
  #
  # class Post
  #   include Stone::Resource
  # 
  #   field :body, String
  # end
  #
  module Resource

    attr_accessor :id

    class << self
      def included(base)
        rsrc_sym = base.to_s.make_key

        @@callbacks ||= Callbacks.new
        @@callbacks.register_klass(base)

        @@store ||= DataStore.new

        base.send(:extend, self)
        base.send(:include, ::Validatable)

        # rspec breaks if its classes have their contructor overwritten
        unless base.to_s.downcase =~ /(spec::example::examplegroup::subclass_\d|blah)/
          # allow object to be created with a hash of attributes...
          # [] allows for obj[attribute] retrieval
          # to_s allows for stupid Rails to work
          base.class_eval <<-EOS, __FILE__, __LINE__
            def initialize(hash = {})
              self.id = self.next_id_for_klass(self.class)
              unless hash.blank?
                update_attributes(hash)
              end
              @@fields[self.model].each do |f|
                instance_variable_set("@"+f[:name].to_s,[]) if f[:klass] == Array
              end
            end

            def to_s
              id
            end

            def [](sym)
              self.send(sym)
            end
          EOS
        end

        unless @@store.resources.include?(rsrc_sym)
          @@store.resources[rsrc_sym] = DataStore.load_data(rsrc_sym)
        end
      end
    end # self

    @@fields = {}

    # Adds a given field to @@fields and inserts an accessor for that
    # field into klass
    # === Parameters
    # +name+<String>::
    #
    def field(name, klass, arg = nil)
      if arg && arg[:unique] == true
        unique = true
      else
        unique = false
      end
      klass_sym = self.to_s.make_key
      unless @@fields[klass_sym]
        @@fields[klass_sym] = [{:name => name, 
                                :klass => klass, 
                                :unique => unique}]
      else
        @@fields[klass_sym] << {:name => name, 
                                :klass => klass, 
                                :unique => unique}
      end
      name = name.to_s
      # add accessor for given field
      unless klass == Array
        self.class_eval <<-EOS, __FILE__, __LINE__
          def #{name}
            @#{name}
          end
          def #{name}=(value)
            @#{name} = value
          end
        EOS
      else
        self.class_eval <<-EOS, __FILE__, __LINE__
          def #{name}
            @#{name}
          end
        EOS
      end
      self.send(:define_method,"next_by_#{name}") {
        self.next(name.to_sym)
      }
      self.send(:define_method,"prev_by_#{name}") {
        self.prev(name.to_sym)
      }
    end # field

    def model
      self.class.to_s.downcase.to_sym
    end
{'fraction_non_alphanumeric': 0.10855878849753275, 'fraction_numerical': 0.0, 'mean_word_length': 1.5034071550255537, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 17, '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': '5485305', 'n_tokens_mistral': 1756, 'n_tokens_neox': 1725, 'n_words': 422}