class EnumTable::Reflection
Attributes
id_name[RW]
name[R]
type[RW]
Public Class Methods
new(name, options={})
click to toggle source
# File lib/enum_table/reflection.rb, line 3 def initialize(name, options={}) @name = name @id_name = options[:id_name] || :"#{name}_id" @type = options[:type] || :symbol @type == :string || @type == :symbol or raise ArgumentError, "invalid type: #{type.inspect}" @strings_to_ids = {} @values_to_ids = {} @ids_to_values = {} @populate_procs = [] @populated = false end
Public Instance Methods
add_value(id, value)
click to toggle source
# File lib/enum_table/reflection.rb, line 36 def add_value(id, value) @strings_to_ids[value.to_s] = id cast_value = @type == :string ? value.to_s : value.to_sym @values_to_ids[cast_value] = id @ids_to_values[id] = cast_value end
id(value)
click to toggle source
# File lib/enum_table/reflection.rb, line 44 def id(value) ensure_populated if value.is_a?(String) || type == :string @strings_to_ids[value.to_s.strip] else @values_to_ids[value] end end
initialize_copy(other)
click to toggle source
# File lib/enum_table/reflection.rb, line 17 def initialize_copy(other) @name = other.name @id_name = other.id_name @type = other.type @strings_to_ids = other.instance_variable_get(:@strings_to_ids).dup @values_to_ids = other.instance_variable_get(:@values_to_ids).dup @ids_to_values = other.instance_variable_get(:@ids_to_values).dup @populate_procs = other.instance_variable_get(:@populate_procs).dup @populated = false end
to_populate(&block)
click to toggle source
# File lib/enum_table/reflection.rb, line 32 def to_populate(&block) @populate_procs << block end
value(id)
click to toggle source
# File lib/enum_table/reflection.rb, line 53 def value(id) ensure_populated @ids_to_values[id] end
values()
click to toggle source
# File lib/enum_table/reflection.rb, line 58 def values ensure_populated @values_to_ids.keys end
Private Instance Methods
ensure_populated()
click to toggle source
# File lib/enum_table/reflection.rb, line 65 def ensure_populated return if @populated @populate_procs.each { |p| p.call(self) } @populate_procs.clear @populated = true end