Class: EnumTable::Reflection
- Inherits:
-
Object
- Object
- EnumTable::Reflection
- Defined in:
- lib/enum_table/reflection.rb
Instance Attribute Summary collapse
-
#id_name ⇒ Object
Returns the value of attribute id_name.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #add_value(id, value) ⇒ Object
- #id(value) ⇒ Object
-
#initialize(name, options = {}) ⇒ Reflection
constructor
A new instance of Reflection.
- #initialize_copy(other) ⇒ Object
- #to_populate(&block) ⇒ Object
- #value(id) ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize(name, options = {}) ⇒ Reflection
Returns a new instance of Reflection.
3 4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/enum_table/reflection.rb', line 3 def initialize(name, ={}) @name = name @id_name = [:id_name] || :"#{name}_id" @type = [: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 |
Instance Attribute Details
#id_name ⇒ Object
Returns the value of attribute id_name.
30 31 32 |
# File 'lib/enum_table/reflection.rb', line 30 def id_name @id_name end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
29 30 31 |
# File 'lib/enum_table/reflection.rb', line 29 def name @name end |
#type ⇒ Object
Returns the value of attribute type.
30 31 32 |
# File 'lib/enum_table/reflection.rb', line 30 def type @type end |
Instance Method Details
#add_value(id, value) ⇒ Object
36 37 38 39 40 41 42 |
# 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) ⇒ Object
44 45 46 47 48 49 50 51 |
# 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) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# 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) ⇒ Object
32 33 34 |
# File 'lib/enum_table/reflection.rb', line 32 def to_populate(&block) @populate_procs << block end |
#value(id) ⇒ Object
53 54 55 56 |
# File 'lib/enum_table/reflection.rb', line 53 def value(id) ensure_populated @ids_to_values[id] end |
#values ⇒ Object
58 59 60 61 |
# File 'lib/enum_table/reflection.rb', line 58 def values ensure_populated @values_to_ids.keys end |