Class: OMGCSV::GlorifiedHash
- Inherits:
-
Object
- Object
- OMGCSV::GlorifiedHash
- Defined in:
- lib/omgcsv.rb
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#contents ⇒ Object
readonly
Returns the value of attribute contents.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Instance Method Summary collapse
-
#===(other) ⇒ Object
Accepts arrays of the same values as equal.
-
#[](index) ⇒ Object
Pull out data by index or key (as in column).
-
#initialize(columns = [], data = []) ⇒ GlorifiedHash
constructor
A new instance of GlorifiedHash.
-
#method_missing(name, *args) ⇒ Object
Pull out data by column name method.
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
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
59 60 61 |
# File 'lib/omgcsv.rb', line 59 def columns @columns end |
#contents ⇒ Object (readonly)
Returns the value of attribute contents.
59 60 61 |
# File 'lib/omgcsv.rb', line 59 def contents @contents end |
#data ⇒ Object (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 |