Class: ActiveRecordBitmask::BitmaskType
- Inherits:
-
ActiveModel::Type::Value
- Object
- ActiveModel::Type::Value
- ActiveRecordBitmask::BitmaskType
- Defined in:
- lib/active_record_bitmask/bitmask_type.rb
Instance Method Summary collapse
- #cast(value) ⇒ Array<Symbol>
- #changed_in_place?(raw_old_value, new_value) ⇒ boolean
- #deserialize(raw_value) ⇒ Array<Symbol>
-
#initialize(_name, map, sub_type) ⇒ BitmaskType
constructor
A new instance of BitmaskType.
- #serialize(value) ⇒ Integer
- #type ⇒ Symbol
Constructor Details
#initialize(_name, map, sub_type) ⇒ BitmaskType
Returns a new instance of BitmaskType.
8 9 10 11 12 |
# File 'lib/active_record_bitmask/bitmask_type.rb', line 8 def initialize(_name, map, sub_type) super() @map = map @sub_type = sub_type end |
Instance Method Details
#cast(value) ⇒ Array<Symbol>
20 21 22 23 24 25 |
# File 'lib/active_record_bitmask/bitmask_type.rb', line 20 def cast(value) return [] if value.blank? bitmask = @map.bitmask_or_attributes_to_bitmask(value) @map.bitmask_to_attributes(bitmask) end |
#changed_in_place?(raw_old_value, new_value) ⇒ boolean
46 47 48 49 |
# File 'lib/active_record_bitmask/bitmask_type.rb', line 46 def changed_in_place?(raw_old_value, new_value) raw_old_value.nil? != new_value.nil? || cast(raw_old_value) != cast(new_value) end |
#deserialize(raw_value) ⇒ Array<Symbol>
35 36 37 38 39 40 |
# File 'lib/active_record_bitmask/bitmask_type.rb', line 35 def deserialize(raw_value) value = @sub_type.deserialize(raw_value) return [] if value.nil? @map.bitmask_to_attributes(value) end |
#serialize(value) ⇒ Integer
28 29 30 |
# File 'lib/active_record_bitmask/bitmask_type.rb', line 28 def serialize(value) @map.bitmask_or_attributes_to_bitmask(value) || 0 end |
#type ⇒ Symbol
15 16 17 |
# File 'lib/active_record_bitmask/bitmask_type.rb', line 15 def type @sub_type.type end |