module Enum::Core
Public Instance Methods
enum_list()
click to toggle source
List of enum data used for all other enum methods
# File lib/iron/enum/core.rb, line 5 def enum_list @enum_list ||= [] @enum_list end
inspect()
click to toggle source
Override inspect on the enum module to give a pretty listing
# File lib/iron/enum/core.rb, line 61 def inspect values.collect do |v| "#{self.to_s}::#{key(v).to_s.upcase} => #{v}" end.join("\n") end
key(key)
click to toggle source
Key for a given key or value
# File lib/iron/enum/core.rb, line 27 def key(key) return nil if key.nil? row_for(key)[KEY_IDX] end
keys(*included)
click to toggle source
Convert an array of values into an array of keys
# File lib/iron/enum/core.rb, line 33 def keys(*included) rows_for(*included).collect {|row| row[KEY_IDX]} end
name(key = :_no_value_)
click to toggle source
Name for a given key/value
Calls superclass method
# File lib/iron/enum/core.rb, line 38 def name(key = :_no_value_) return nil if key.nil? return super() if key == :_no_value_ row_for(key)[NAME_IDX] end
names(*included)
click to toggle source
Convert arrays of keys/values into an array of names
# File lib/iron/enum/core.rb, line 45 def names(*included) rows_for(*included).collect {|row| row[NAME_IDX]} end
option_for(key)
click to toggle source
Array in order required by select fields
# File lib/iron/enum/core.rb, line 56 def option_for(key) opt = [name(key), value(key)] end
options(*included)
click to toggle source
Used for select tag options, optionally pass set of keys/ids to include, eg if there are only a subset that would be valid for selection in a given context.
# File lib/iron/enum/core.rb, line 51 def options(*included) rows_for(*included).collect {|row| option_for(row[KEY_IDX])} end
valid_value?(val)
click to toggle source
True if a valid value (not key!), false if not
# File lib/iron/enum/core.rb, line 22 def valid_value?(val) return values.include?(val) end
value(key)
click to toggle source
Value for a given key or value
# File lib/iron/enum/core.rb, line 11 def value(key) return nil if key.nil? row_for(key)[VALUE_IDX] end
values(*included)
click to toggle source
Convert array of keys to an array of values
# File lib/iron/enum/core.rb, line 17 def values(*included) rows_for(*included).collect {|row| row[VALUE_IDX]} end
Private Instance Methods
row_for(in_key)
click to toggle source
# File lib/iron/enum/core.rb, line 86 def row_for(in_key) key = to_key(in_key) row = enum_list.find {|r| r[KEY_IDX] == key} raise RuntimeError.new("Unknown key or value [#{in_key.inspect}] in enum #{self}") unless row row end
rows_for(*included)
click to toggle source
# File lib/iron/enum/core.rb, line 93 def rows_for(*included) return [] if included.count == 1 && (included.first == [] || included.first == nil) included.flatten! if included.empty? # All enums enum_list else # Only the specified ones included.collect {|key| row_for(key)}.compact end end
to_key(id)
click to toggle source
# File lib/iron/enum/core.rb, line 69 def to_key(id) return nil if id.nil? return id if id.is_a?(Symbol) if id.is_a?(String) # Check for "15" style ids - common in web usage a la Rails where params come in as text if id.to_i.to_s == id # Yup, convert id = id.to_i else # No, so invalid return nil end end row = enum_list.find {|row| row[VALUE_IDX] == id} row.nil? ? nil : row[KEY_IDX] end