Class: ActiveMocker::AttributeTypes::Enum
- Inherits:
-
Virtus::Attribute
- Object
- Virtus::Attribute
- ActiveMocker::AttributeTypes::Enum
- Defined in:
- lib/active_mocker/attribute_types/enum.rb
Class Attribute Summary collapse
-
.attribute ⇒ Object
Returns the value of attribute attribute.
-
.db_value_type ⇒ Object
Returns the value of attribute db_value_type.
-
.enums ⇒ Object
Returns the value of attribute enums.
-
.ignore_value ⇒ Object
Returns the value of attribute ignore_value.
-
.key_type ⇒ Object
Returns the value of attribute key_type.
-
.table_name ⇒ Object
Returns the value of attribute table_name.
Class Method Summary collapse
- .build(db_value_type:, table_name:, attribute:, enums:, ignore_value: false) ⇒ Object
- .to_s ⇒ Object
Instance Method Summary collapse
- #coerce(key) ⇒ Object
- #db_value_type ⇒ Object
- #get_key(value) ⇒ Object
- #get_value(key) ⇒ Object
- #key_type ⇒ Object
Class Attribute Details
.attribute ⇒ Object
Returns the value of attribute attribute.
31 32 33 |
# File 'lib/active_mocker/attribute_types/enum.rb', line 31 def attribute @attribute end |
.db_value_type ⇒ Object
Returns the value of attribute db_value_type.
31 32 33 |
# File 'lib/active_mocker/attribute_types/enum.rb', line 31 def db_value_type @db_value_type end |
.enums ⇒ Object
Returns the value of attribute enums.
31 32 33 |
# File 'lib/active_mocker/attribute_types/enum.rb', line 31 def enums @enums end |
.ignore_value ⇒ Object
Returns the value of attribute ignore_value.
31 32 33 |
# File 'lib/active_mocker/attribute_types/enum.rb', line 31 def ignore_value @ignore_value end |
.key_type ⇒ Object
Returns the value of attribute key_type.
31 32 33 |
# File 'lib/active_mocker/attribute_types/enum.rb', line 31 def key_type @key_type end |
.table_name ⇒ Object
Returns the value of attribute table_name.
31 32 33 |
# File 'lib/active_mocker/attribute_types/enum.rb', line 31 def table_name @table_name end |
Class Method Details
.build(db_value_type:, table_name:, attribute:, enums:, ignore_value: false) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/active_mocker/attribute_types/enum.rb', line 8 def build(db_value_type:, table_name:, attribute:, enums:, ignore_value: false) klass = Class.new(ActiveMocker::AttributeTypes::Enum) klass.table_name = table_name.to_sym klass.attribute = attribute.to_sym klass.ignore_value = ignore_value enums = if enums.is_a?(Array) enums.each_with_object({}).with_index { |(k, h), i| h[k] = i } else enums end klass.key_type = String klass.db_value_type = db_value_type klass.enums = Hash[enums.map do |k, v| [Virtus::Attribute.build(klass.key_type).coerce(k), Virtus::Attribute.build(klass.db_value_type).coerce(v)] end] klass end |
.to_s ⇒ Object
27 28 29 |
# File 'lib/active_mocker/attribute_types/enum.rb', line 27 def to_s "ActiveMocker::AttributeTypes::Enum.build(ignore_value: #{ignore_value}, db_value_type: #{db_value_type}, table_name: :#{table_name}, attribute: :#{attribute}, enums: #{enums.inspect})" end |
Instance Method Details
#coerce(key) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/active_mocker/attribute_types/enum.rb', line 34 def coerce(key) return if key.nil? coerced_key = key_type.coerce(key) if key && self.class.enums.key?(coerced_key) if self.class.ignore_value coerced_key else get_value(key) end else raise ArgumentError, "'#{coerced_key}' is not a valid #{self.class.attribute}" end end |
#db_value_type ⇒ Object
60 61 62 |
# File 'lib/active_mocker/attribute_types/enum.rb', line 60 def db_value_type Virtus::Attribute.build(self.class.db_value_type) end |
#get_key(value) ⇒ Object
48 49 50 |
# File 'lib/active_mocker/attribute_types/enum.rb', line 48 def get_key(value) self.class.enums.invert[db_value_type.coerce(value)] end |
#get_value(key) ⇒ Object
52 53 54 |
# File 'lib/active_mocker/attribute_types/enum.rb', line 52 def get_value(key) self.class.enums[key_type.coerce(key)] end |
#key_type ⇒ Object
56 57 58 |
# File 'lib/active_mocker/attribute_types/enum.rb', line 56 def key_type Virtus::Attribute.build(self.class.key_type) end |