Class: ActiveRecordBitmask::BitmaskType

Inherits:
ActiveModel::Type::Value
  • Object
show all
Defined in:
lib/active_record_bitmask/bitmask_type.rb

Instance Method Summary collapse

Constructor Details

#initialize(_name, map, sub_type) ⇒ BitmaskType

Returns a new instance of BitmaskType.

Parameters:



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>

Returns:

  • (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

Parameters:

  • raw_old_value (Integer)
  • new_value (Array<Symbol>)

Returns:

  • (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>

Parameters:

  • raw_value (Integer, nil)

Returns:

  • (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

Returns:

  • (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

#typeSymbol

Returns:

  • (Symbol)


15
16
17
# File 'lib/active_record_bitmask/bitmask_type.rb', line 15

def type
  @sub_type.type
end