<commit_before>module BettyResource
  class Model < Base
    extend ActiveSupport::Autoload

    autoload :Record
    autoload :Property

    attr_accessor :id, :name, :properties

    def initialize(id, name, properties = [])
      @id, @name, @properties = id, name, properties
    end

    def self.parse(input)
      input.inject({}) do |hash, row|
        hash.merge(row["name"] => Model.new(row["id"], row["name"], Property.parse(row["properties"])))
      end
    end

    def all(options = {})
      load(self.class.get("/models/#{id}/records", :body => options))
    end

    def get(record_id)
      if data = load(self.class.get("/models/#{id}/records/#{record_id}"))
        data.first
      end
    end

    def new(attributes = {})
      BettyResource::Model::Record.new(self, attributes)
    end

    def create(attributes = {})
      new(attributes).tap do |record|
        record.save
      end
    end

    def to_s
      name
    end

  private

    def load(data)
      if parsed_data = data.parsed_response
        begin
          [parsed_data].flatten.collect do |data|
            if data
              id = data.delete "id"
              BettyResource::Model::Record.new(self, data).tap do |record|
                record.instance_variable_set :@id, id
              end
            end
          end
        rescue MultiJson::DecodeError
        end
      end
    end

  end
end<commit_msg>Refactored BettyResource::Model.all, BettyResource::Model.get and BettyResource::Model.load<commit_after>module BettyResource
  class Model < Base
    extend ActiveSupport::Autoload

    autoload :Record
    autoload :Property

    attr_accessor :id, :name, :properties

    def initialize(id, name, properties = [])
      @id, @name, @properties = id, name, properties
    end

    def self.parse(input)
      input.inject({}) do |hash, row|
        hash.merge(row["name"] => Model.new(row["id"], row["name"], Property.parse(row["properties"])))
      end
    end

    def all(options = {})
      begin
        self.class.get("/models/#{id}/records", :body => options).parsed_response.collect do |data|
          load data
        end
      rescue MultiJson::DecodeError
      end
    end

    def get(record_id)
      begin
        load self.class.get("/models/#{id}/records/#{record_id}").parsed_response
      rescue MultiJson::DecodeError
      end
    end

    def new(attributes = {})
      BettyResource::Model::Record.new(self, attributes)
    end

    def create(attributes = {})
      new(attributes).tap do |record|
        record.save
      end
    end

    def to_s
      name
    end

  private

    def load(data, record = nil)
      if data
        id = data.delete "id"
        (record || BettyResource::Model::Record.new(self)).tap do |record|
          record.instance_variable_set :@id, id
          record.attributes = data
        end
      end
    end

  end
end
{'fraction_non_alphanumeric': 0.11926921751120304, 'fraction_numerical': 0.0, 'mean_word_length': 2.2352285395763656, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '4482990', 'n_tokens_mistral': 883, 'n_tokens_neox': 860, 'n_words': 207}