Module: Elemental

Includes:
Enumerable
Defined in:
lib/elemental.rb

Constant Summary collapse

VERSION =
'0.1.2'

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(value) ⇒ Element

Allows Car::honda

Returns:



152
153
154
# File 'lib/elemental.rb', line 152

def method_missing(value)
  self[value]
end

Instance Attribute Details

#value_as_ordinaltrue or false (readonly)

Affects whether #value returns symbolic name for element or ordinal value

Returns:

  • (true or false)

    value is ordinal value when true



39
40
41
# File 'lib/elemental.rb', line 39

def value_as_ordinal
  @value_as_ordinal
end

Instance Method Details

#[](value) ⇒ Element

Shortcut to getting to a specific element Can get by symbol or constant (i.e. Color or Color)

position or by symbolic representation for the element.

Parameters:

  • value (Symbol, Fixnum)

    the Element to retrieve, referenced either by ordinal

Returns:



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

Returns:



159
160
161
# File 'lib/elemental.rb', line 159

def const_missing(const)
  get_unordered_element(const)
end

#defaultsArray of Element

Returns all elements that are flagged as a default

Returns:



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

#firstElement

Returns the first element in this class

Returns:



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

def first
  @ordered_elements.first
end

#inspectString

Outputs a sensible inspect string

Returns:

  • (String)


173
174
175
# File 'lib/elemental.rb', line 173

def inspect
  "#<#{self}:#{object_id} #{@ordered_elements.inspect}>"
end

#lastElement

Returns the last element in this class

Returns:



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, options={})
  @unordered_elements ||= {}
  @ordered_elements ||= []
  symbol = conform_to_symbol(symbol)
  element = Element.new(self, symbol, @ordered_elements.size, options)

  @unordered_elements[symbol] = element
  @ordered_elements << element
end

#persist_ordinallyObject

Note:

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.

Parameters:

  • value (Symbol)

    the symbolic reference to the element

Returns:



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

#sizeFixnum

Returns the number of elements added

Returns:

  • (Fixnum)


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.

Parameters:

  • value (Symbol)

    the symbolic reference to the element

Returns:



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