Class: OMGCSV::GlorifiedHash

Inherits:
Object
  • Object
show all
Defined in:
lib/omgcsv.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(columns = [], data = []) ⇒ GlorifiedHash

Returns a new instance of GlorifiedHash.



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/omgcsv.rb', line 61

def initialize(columns=[], data=[])
  @columns=columns
  @data=data
  @contents={}
  columns.each_with_index do |header, index|
    @contents[header] = data[index]
  end

  [@columns, @data, @contents].each do |member|
    member.freeze
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Pull out data by column name method



88
89
90
91
# File 'lib/omgcsv.rb', line 88

def method_missing(name, *args)
  return @contents[name.to_s] if @contents.key? name.to_s
  super(*args)
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



59
60
61
# File 'lib/omgcsv.rb', line 59

def columns
  @columns
end

#contentsObject (readonly)

Returns the value of attribute contents.



59
60
61
# File 'lib/omgcsv.rb', line 59

def contents
  @contents
end

#dataObject (readonly)

Returns the value of attribute data.



59
60
61
# File 'lib/omgcsv.rb', line 59

def data
  @data
end

Instance Method Details

#===(other) ⇒ Object

Accepts arrays of the same values as equal



96
97
98
# File 'lib/omgcsv.rb', line 96

def ===(other)
  other === data()
end

#[](index) ⇒ Object

Pull out data by index or key (as in column)



76
77
78
79
80
81
82
83
84
# File 'lib/omgcsv.rb', line 76

def [](index)
  if (index.is_a? Fixnum)
    @data[index.to_i]
  elsif (index.respond_to? :to_s)
    @contents[index.to_s]
  else
    raise "Can't reference a value with #{index.class}"
  end
end