Sha256: c4677738cec0340494f77cbb645d867db2ef2bbb5493bdc0ebf85951b87d4ca8

Contents?: true

Size: 744 Bytes

Versions: 3

Compression:

Stored size: 744 Bytes

Contents

class Array

  def to_objects(&blk)
    records = blk.call
    return [] if records.empty?
    records.map { |record| create_object_for_this_record(record) }
  end

  private

  def create_object_for_this_record(record)
    result = Object.new
    self.each_with_index do |property_name, index|
      value = get_the_value(record, index)
      add_reader_for(result, property_name, value)
    end
    result
  end

  def get_the_value(value, index)
    return value unless value.kind_of?(Array)
    value[index]
  end

  def add_reader_for(result, property_name, this_value)
    result.instance_variable_set("@#{property_name}", this_value)
    result.instance_eval("
    class << self
      attr_accessor :#{property_name}
    end")
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
subtle-0.3.4 lib/subtle/array_to_object.rb
smoke_monster-0.3.3 lib/smoke_monster/array_to_object.rb
smoke_monster-0.3.2 lib/smoke_monster/array_to_object.rb