Class: Element
- Inherits:
-
Object
- Object
- Element
- Defined in:
- lib/element.rb
Overview
Copyright © 2009 Michael Lang
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Instance Attribute Summary collapse
-
#default ⇒ Object
Returns the value of attribute default.
-
#display ⇒ Object
Returns the value of attribute display.
-
#ordinal ⇒ Object
Returns the value of attribute ordinal.
-
#position ⇒ Object
Returns the value of attribute position.
-
#symbol ⇒ Object
Returns the value of attribute symbol.
Instance Method Summary collapse
-
#<=>(other) ⇒ -1, ...
Will sort on position (which defaults to ordinal if not explicitly set).
-
#default? ⇒ true, false
Returns true if is default.
-
#humanize ⇒ String
Will humanize the Element’s symbolic name by removing underscores and trailing “_id” monikers.
-
#index ⇒ Fixnum
(also: #to_i, #to_int, #ord)
Returns the ordinal value for this element.
-
#initialize(elemental, symbol, ordinal, options = {}) ⇒ Element
constructor
Initializes a new element into the given Elemental Class.
-
#inspect ⇒ String
Generates a reasonable inspect string.
-
#is?(value) ⇒ true, false
Returns true if given value matches this Element’s value.
-
#pred ⇒ Element
Returns the element that precedes this element.
-
#succ ⇒ Element
Returns the element that follows this element.
-
#to_s ⇒ String
Returns the symbol for this element as a string.
-
#to_sym ⇒ Symbol
Returns this element’s symbol.
-
#value ⇒ Fixnum, Symbol
this element.
Constructor Details
#initialize(elemental, symbol, ordinal, options = {}) ⇒ Element
Initializes a new element into the given Elemental Class
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/element.rb', line 36 def initialize(elemental, symbol, ordinal, ={}) @elemental = elemental @symbol = symbol @ordinal = ordinal # Sets all options and optionally creates an accessor method .each do |k, v| eval("def #{k}; @#{k}; end") unless respond_to?(k) v.is_a?(String) ? eval("@#{k} = '#{v}'") : eval("@#{k} = #{v}") end # Force the following values (overrides above looped assignments) @default = [:default] || false @display = [:display] || symbol.to_s @position = [:position] || ordinal end |
Instance Attribute Details
#default ⇒ Object
Returns the value of attribute default.
25 26 27 |
# File 'lib/element.rb', line 25 def default @default end |
#display ⇒ Object
Returns the value of attribute display.
25 26 27 |
# File 'lib/element.rb', line 25 def display @display end |
#ordinal ⇒ Object
Returns the value of attribute ordinal.
25 26 27 |
# File 'lib/element.rb', line 25 def ordinal @ordinal end |
#position ⇒ Object
Returns the value of attribute position.
25 26 27 |
# File 'lib/element.rb', line 25 def position @position end |
#symbol ⇒ Object
Returns the value of attribute symbol.
25 26 27 |
# File 'lib/element.rb', line 25 def symbol @symbol end |
Instance Method Details
#<=>(other) ⇒ -1, ...
Will sort on position (which defaults to ordinal if not explicitly set)
76 77 78 |
# File 'lib/element.rb', line 76 def <=>(other) position <=> other.position end |
#default? ⇒ true, false
Returns true if is default. (useful if populating select lists)
120 121 122 |
# File 'lib/element.rb', line 120 def default? @default end |
#humanize ⇒ String
Will humanize the Element’s symbolic name by removing underscores and trailing “_id” monikers. The words are capitialized.
138 139 140 141 |
# File 'lib/element.rb', line 138 def humanize return @display if @display != to_s return self.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize end |
#index ⇒ Fixnum Also known as: to_i, to_int, ord
Returns the ordinal value for this element
113 114 115 |
# File 'lib/element.rb', line 113 def index @ordinal end |
#inspect ⇒ String
Generates a reasonable inspect string
127 128 129 130 131 |
# File 'lib/element.rb', line 127 def inspect "#<#{self.class}:#{self.object_id} {symbol => :#{@symbol}, " + "ordinal => #{@ordinal}, display => \"#{@display}\", " + "position => #{@position}, default => #{@default}}>" end |
#is?(value) ⇒ true, false
Returns true if given value matches this Element’s value
58 59 60 |
# File 'lib/element.rb', line 58 def is?(value) self == (value.is_a?(Element) ? value : @elemental[value]) end |
#pred ⇒ Element
Returns the element that precedes this element. If this is the first element, returns last element
92 93 94 |
# File 'lib/element.rb', line 92 def pred @elemental.pred(@symbol) end |
#succ ⇒ Element
Returns the element that follows this element. If this is last element, returns first element
84 85 86 |
# File 'lib/element.rb', line 84 def succ @elemental.succ(@symbol) end |
#to_s ⇒ String
Returns the symbol for this element as a string
99 100 101 |
# File 'lib/element.rb', line 99 def to_s @symbol.to_s end |
#to_sym ⇒ Symbol
Returns this element’s symbol
106 107 108 |
# File 'lib/element.rb', line 106 def to_sym @symbol end |
#value ⇒ Fixnum, Symbol
this element. The value type returned is determined by the value_as_ordinal property
66 67 68 |
# File 'lib/element.rb', line 66 def value @elemental.value_as_ordinal ? to_i : to_sym end |