Class: Enumeration::Collection
- Inherits:
-
Object
- Object
- Enumeration::Collection
- Defined in:
- lib/enumeration/collection.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Instance Method Summary collapse
-
#[](key) ⇒ Object
lookup collection value by a key.
-
#initialize(map_or_list) ⇒ Collection
constructor
A new instance of Collection.
-
#key(value) ⇒ Object
lookup collection key by a value.
- #list? ⇒ Boolean
- #map? ⇒ Boolean
- #set ⇒ Object
Constructor Details
#initialize(map_or_list) ⇒ Collection
Returns a new instance of Collection.
6 7 8 9 10 11 |
# File 'lib/enumeration/collection.rb', line 6 def initialize(map_or_list) unless map_or_list.kind_of?(::Hash) || map_or_list.kind_of?(::Array) raise ArgumentError, "please specify the enum collection as a Hash or Array" end @data = map_or_list end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
4 5 6 |
# File 'lib/enumeration/collection.rb', line 4 def data @data end |
Instance Method Details
#[](key) ⇒ Object
lookup collection value by a key
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/enumeration/collection.rb', line 14 def [](key) if self.map? && @data.has_key?(key) @data[key] elsif (self.map? && @data.has_value?(key)) || (self.list? && @data.include?(key)) key else nil end end |
#key(value) ⇒ Object
lookup collection key by a value
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/enumeration/collection.rb', line 26 def key(value) if self.map? && @data.has_value?(value) @data.invert[value] elsif (self.map? && @data.has_key?(value)) || (self.list? && @data.include?(value)) value else nil end end |
#list? ⇒ Boolean
37 38 39 |
# File 'lib/enumeration/collection.rb', line 37 def list? @data.kind_of?(::Array) end |
#map? ⇒ Boolean
41 42 43 |
# File 'lib/enumeration/collection.rb', line 41 def map? @data.kind_of?(::Hash) end |
#set ⇒ Object
45 46 47 |
# File 'lib/enumeration/collection.rb', line 45 def set self.map? ? @data.keys : @data end |