Class: Element

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(elemental, symbol, ordinal, options = {}) ⇒ Element

Initializes a new element into the given Elemental Class

Parameters:

  • elemental (Elemental)

    the “owning container” class

  • symbol (Symbol)

    the symbolic name for the element

  • ordinal (Fixnum)

    the desired ordinal value - defaults to position in list

  • options (Hash) (defaults to: {})

    A hash of additional options to associate with the element



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, options={})
  @elemental = elemental
  @symbol = symbol
  @ordinal = ordinal
  
  # Sets all options and optionally creates an accessor method
  options.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 = options[:default] || false
  @display = options[:display] || symbol.to_s
  @position = options[:position] || ordinal
end

Instance Attribute Details

#defaultObject

Returns the value of attribute default.



25
26
27
# File 'lib/element.rb', line 25

def default
  @default
end

#displayObject

Returns the value of attribute display.



25
26
27
# File 'lib/element.rb', line 25

def display
  @display
end

#ordinalObject

Returns the value of attribute ordinal.



25
26
27
# File 'lib/element.rb', line 25

def ordinal
  @ordinal
end

#positionObject

Returns the value of attribute position.



25
26
27
# File 'lib/element.rb', line 25

def position
  @position
end

#symbolObject

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)

Parameters:

  • other (Element)

    the other Element to compare against

Returns:

  • (-1, 0, +1)


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)

Returns:

  • (true, false)


120
121
122
# File 'lib/element.rb', line 120

def default?
  @default
end

#humanizeString

Will humanize the Element’s symbolic name by removing underscores and trailing “_id” monikers. The words are capitialized.

Returns:

  • (String)

Author:

  • swiped from Rails…



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

#indexFixnum Also known as: to_i, to_int, ord

Returns the ordinal value for this element

Returns:

  • (Fixnum)


113
114
115
# File 'lib/element.rb', line 113

def index
  @ordinal
end

#inspectString

Generates a reasonable inspect string

Returns:

  • (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

Parameters:

  • value (Symbol or Element)

    the value we’re are comparing against this element

Returns:

  • (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

#predElement

Returns the element that precedes this element. If this is the first element, returns last element

Returns:



92
93
94
# File 'lib/element.rb', line 92

def pred
  @elemental.pred(@symbol)
end

#succElement

Returns the element that follows this element. If this is last element, returns first element

Returns:



84
85
86
# File 'lib/element.rb', line 84

def succ
  @elemental.succ(@symbol)
end

#to_sString

Returns the symbol for this element as a string

Returns:

  • (String)


99
100
101
# File 'lib/element.rb', line 99

def to_s
  @symbol.to_s
end

#to_symSymbol

Returns this element’s symbol

Returns:

  • (Symbol)


106
107
108
# File 'lib/element.rb', line 106

def to_sym
  @symbol
end

#valueFixnum, Symbol

this element. The value type returned is determined by the value_as_ordinal property

Returns:

  • (Fixnum, Symbol)

    returns either the ordinal value or the symbol for



66
67
68
# File 'lib/element.rb', line 66

def value
  @elemental.value_as_ordinal ? to_i : to_sym
end