Module: Elemental
- Includes:
- Enumerable
- Defined in:
- lib/elemental.rb
Constant Summary collapse
- VERSION =
'0.1.2'
Instance Attribute Summary collapse
-
#value_as_ordinal ⇒ true or false
readonly
Affects whether #value returns symbolic name for element or ordinal value.
Instance Method Summary collapse
- #[](value) ⇒ Element
-
#const_missing(const) ⇒ Element
Allows Car::Honda.
-
#defaults ⇒ Array of Element
Returns all elements that are flagged as a default.
-
#each(&block) ⇒ Object
Iterates over the elements, passing each Element to the given block.
-
#first ⇒ Element
Returns the first element in this class.
-
#inspect ⇒ String
Outputs a sensible inspect string.
-
#last ⇒ Element
Returns the last element in this class.
-
#member(symbol, options = {}) ⇒ Object
Adds an element in the order given to the enumerable’s class Order matters if you store ordinal to database or other persistent stores.
-
#method_missing(value) ⇒ Element
Allows Car::honda.
-
#persist_ordinally ⇒ Object
Causes the Element#value method to return ordinal value, which normally returns the ruby symbol.
-
#pred(value) ⇒ Element
Returns the element that precedes the given element.
-
#size ⇒ Fixnum
Returns the number of elements added.
-
#succ(value) ⇒ Element
Returns the element that follows the given element.
-
#synonym(new_symbol, existing_symbol) ⇒ Object
Allows you to define aliases for a given member with adding additional elements to the elemental class.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(value) ⇒ Element
Allows Car::honda
152 153 154 |
# File 'lib/elemental.rb', line 152 def method_missing(value) self[value] end |
Instance Attribute Details
#value_as_ordinal ⇒ true or false (readonly)
Affects whether #value returns symbolic name for element or ordinal value
39 40 41 |
# File 'lib/elemental.rb', line 39 def value_as_ordinal @value_as_ordinal end |
Instance Method Details
#[](value) ⇒ Element
139 140 141 |
# File 'lib/elemental.rb', line 139 def [](value) value.is_a?(Fixnum) ? get_ordered_element(value) : get_unordered_element(value) end |
#const_missing(const) ⇒ Element
Allows Car::Honda
159 160 161 |
# File 'lib/elemental.rb', line 159 def const_missing(const) get_unordered_element(const) end |
#defaults ⇒ Array of Element
Returns all elements that are flagged as a default
166 167 168 |
# File 'lib/elemental.rb', line 166 def defaults @ordered_elements.select{|s| s.default?} end |
#each(&block) ⇒ Object
Iterates over the elements, passing each Element to the given block
145 146 147 |
# File 'lib/elemental.rb', line 145 def each(&block) @ordered_elements.each(&block) end |
#first ⇒ Element
Returns the first element in this class
89 90 91 |
# File 'lib/elemental.rb', line 89 def first @ordered_elements.first end |
#inspect ⇒ String
Outputs a sensible inspect string
173 174 175 |
# File 'lib/elemental.rb', line 173 def inspect "#<#{self}:#{object_id} #{@ordered_elements.inspect}>" end |
#last ⇒ Element
Returns the last element in this class
97 98 99 |
# File 'lib/elemental.rb', line 97 def last @ordered_elements.last end |
#member(symbol, options = {}) ⇒ Object
Adds an element in the order given to the enumerable’s class Order matters if you store ordinal to database or other persistent stores
57 58 59 60 61 62 63 64 65 |
# File 'lib/elemental.rb', line 57 def member(symbol, ={}) @unordered_elements ||= {} @ordered_elements ||= [] symbol = conform_to_symbol(symbol) element = Element.new(self, symbol, @ordered_elements.size, ) @unordered_elements[symbol] = element @ordered_elements << element end |
#persist_ordinally ⇒ Object
If you choose for Element#value to return ordinal and you are
Causes the Element#value method to return ordinal value, which normally returns the ruby symbol
storing value to database, then you should always APPEND new elements to the declared Elemental class. Otherwise, you WILL CHANGE the ordinal values and thus invalidate any persistent stores. If you’re not persistently storing ordinal values, then order of membership is moot.
50 51 52 |
# File 'lib/elemental.rb', line 50 def persist_ordinally @value_as_ordinal = true end |
#pred(value) ⇒ Element
Returns the element that precedes the given element. If given element is the first element, then last element is returned.
126 127 128 129 |
# File 'lib/elemental.rb', line 126 def pred(value) index = self[value].to_i - 1 (index < 0) ? last : @ordered_elements[index] end |
#size ⇒ Fixnum
Returns the number of elements added
105 106 107 |
# File 'lib/elemental.rb', line 105 def size @ordered_elements.size end |
#succ(value) ⇒ Element
Returns the element that follows the given element. If given element is last element, then first element is returned.
115 116 117 118 |
# File 'lib/elemental.rb', line 115 def succ(value) index = self[value].to_i + 1 (index >= size) ? first : @ordered_elements[index] end |
#synonym(new_symbol, existing_symbol) ⇒ Object
Allows you to define aliases for a given member with adding additional elements to the elemental class.
class Fruit
extend Elemental
member :apple
member :kiwi
synonym :machintosh, :apple
end
will yield an Elemental with only two elements (apple and kiwi), but give the ability to retrieve Fruit::apple using Fruit::machintosh
80 81 82 83 |
# File 'lib/elemental.rb', line 80 def synonym(new_symbol, existing_symbol) element = self[existing_symbol] @unordered_elements[conform_to_symbol(new_symbol)] = element end |