Sha256: 444585907d49ff25c496043686dba3dcbbcf4e20319b559aad81b756ee98989d

Contents?: true

Size: 743 Bytes

Versions: 4

Compression:

Stored size: 743 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

4 entries across 4 versions & 1 rubygems

Version Path
smoke_monster-0.3.0 lib/smoke_monster/array_to_object.rb
smoke_monster-0.2.3 lib/smoke_monster/array_to_object.rb
smoke_monster-0.2.2 lib/smoke_monster/array_to_object.rb
smoke_monster-0.2.1 lib/smoke_monster/array_to_object.rb