Module: Enum::Core

Defined in:
lib/iron/enum/core.rb

Instance Method Summary collapse

Instance Method Details

#enum_listObject

List of enum data used for all other enum methods



5
6
7
8
# File 'lib/iron/enum/core.rb', line 5

def enum_list
  @enum_list ||= []
  @enum_list
end

#inspectObject

Override inspect on the enum module to give a pretty listing



61
62
63
64
65
# 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) ⇒ Object

Key for a given key or value



27
28
29
30
# File 'lib/iron/enum/core.rb', line 27

def key(key)
  return nil if key.nil?
  row_for(key)[KEY_IDX]
end

#keys(*included) ⇒ Object

Convert an array of values into an array of keys



33
34
35
# File 'lib/iron/enum/core.rb', line 33

def keys(*included)
  rows_for(*included).collect {|row| row[KEY_IDX]}
end

#name(key = :_no_value_) ⇒ Object

Name for a given key/value



38
39
40
41
42
# 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) ⇒ Object

Convert arrays of keys/values into an array of names



45
46
47
# File 'lib/iron/enum/core.rb', line 45

def names(*included)
  rows_for(*included).collect {|row| row[NAME_IDX]}
end

#option_for(key) ⇒ Object

Array in order required by select fields



56
57
58
# File 'lib/iron/enum/core.rb', line 56

def option_for(key)
  opt = [name(key), value(key)]
end

#options(*included) ⇒ Object

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.



51
52
53
# 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) ⇒ Boolean

True if a valid value (not key!), false if not

Returns:

  • (Boolean)


22
23
24
# File 'lib/iron/enum/core.rb', line 22

def valid_value?(val)
  return values.include?(val)
end

#value(key) ⇒ Object

Value for a given key or value



11
12
13
14
# File 'lib/iron/enum/core.rb', line 11

def value(key)
  return nil if key.nil?
  row_for(key)[VALUE_IDX]
end

#values(*included) ⇒ Object

Convert array of keys to an array of values



17
18
19
# File 'lib/iron/enum/core.rb', line 17

def values(*included)
  rows_for(*included).collect {|row| row[VALUE_IDX]}
end