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.
7 8 9 10 11 12 |
# File 'lib/enumeration/collection.rb', line 7 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.
5 6 7 |
# File 'lib/enumeration/collection.rb', line 5 def data @data end |
Instance Method Details
#[](key) ⇒ Object
lookup collection value by a key
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/enumeration/collection.rb', line 15 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
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/enumeration/collection.rb', line 27 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
38 39 40 |
# File 'lib/enumeration/collection.rb', line 38 def list? @data.kind_of?(::Array) end |
#map? ⇒ Boolean
42 43 44 |
# File 'lib/enumeration/collection.rb', line 42 def map? @data.kind_of?(::Hash) end |
#set ⇒ Object
46 47 48 |
# File 'lib/enumeration/collection.rb', line 46 def set self.map? ? @data.keys : @data end |