<commit_before>module Daodalus
  module Model
    def initialize(result)
      @result = result
    end

    def self.included(base)
      base.extend(DocumentDescriptor)
    end

    module DocumentDescriptor
      def field(f, options={})
        key = options.fetch(:key, f)
        type = options.fetch(:type, nil)
        if options.has_key?(:default)
          define_field_with_default(f, key, type, options)
        else
          define_field(f, key, type)
        end
      end

      private

      def define_field(f, key, type)
        define_method f do
          value = result.fetch(key.to_s)
          if type.nil? then value else apply_conversion(value, type) end
        end
      end

      def define_field_with_default(f, key, type, options)
        default = options.fetch(:default)
        define_method f do
          value = result.fetch(key.to_s, default)
          if type.nil? then value else apply_conversion(value, type) end
        end
      end

    end

    private

    attr_reader :result

    def apply_conversion(value, type)
      case type.to_s
      when 'Integer' then value.to_i
      when 'Float' then value.to_f
      when 'String' then value.to_s
      when 'Symbol' then value.to_sym
      when 'Date' then Date.parse(value)
      when 'Time' then Time.parse(value)
      else type.new(value)
      end
    end

  end
end<commit_msg>Make model data public<commit_after>module Daodalus
  module Model
    def initialize(data)
      @data = data
    end

    attr_reader :data

    def self.included(base)
      base.extend(DocumentDescriptor)
    end

    module DocumentDescriptor
      def field(f, options={})
        key = options.fetch(:key, f)
        type = options.fetch(:type, nil)
        if options.has_key?(:default)
          define_field_with_default(f, key, type, options)
        else
          define_field(f, key, type)
        end
      end

      private

      def define_field(f, key, type)
        define_method f do
          value = data.fetch(key.to_s)
          if type.nil? then value else apply_conversion(value, type) end
        end
      end

      def define_field_with_default(f, key, type, options)
        default = options.fetch(:default)
        define_method f do
          value = data.fetch(key.to_s, default)
          if type.nil? then value else apply_conversion(value, type) end
        end
      end

    end

    private

    def apply_conversion(value, type)
      case type.to_s
      when 'Integer' then value.to_i
      when 'Float' then value.to_f
      when 'String' then value.to_s
      when 'Symbol' then value.to_sym
      when 'Date' then Date.parse(value)
      when 'Time' then Time.parse(value)
      else type.new(value)
      end
    end

  end
end
{'fraction_non_alphanumeric': 0.07979688066739209, 'fraction_numerical': 0.0, 'mean_word_length': 2.0850111856823266, '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': '4473034', 'n_tokens_mistral': 850, 'n_tokens_neox': 819, 'n_words': 238}