Class: Bitmasker::BitmaskAttributes

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::AttributeMethods
Defined in:
lib/bitmasker/bitmask_attributes.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ BitmaskAttributes

Returns a new instance of BitmaskAttributes.



36
37
38
39
# File 'lib/bitmasker/bitmask_attributes.rb', line 36

def initialize(model)
  @model = model
  @bitmask = Bitmask.new(bitmask_attributes, read || defaults)
end

Instance Attribute Details

#bitmaskObject (readonly)

Returns the value of attribute bitmask.



35
36
37
# File 'lib/bitmasker/bitmask_attributes.rb', line 35

def bitmask
  @bitmask
end

#modelObject (readonly)

Returns the value of attribute model.



35
36
37
# File 'lib/bitmasker/bitmask_attributes.rb', line 35

def model
  @model
end

Class Method Details

.make(model_class, field_name, bitmask_attributes, defaults = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/bitmasker/bitmask_attributes.rb', line 14

def self.make(model_class, field_name, bitmask_attributes, defaults = {})
  klass = Class.new(self) do
    define_attribute_methods bitmask_attributes.keys
  end

  klass.model_class = model_class
  klass.bitmask_attributes = bitmask_attributes.stringify_keys
  klass.defaults = defaults.stringify_keys
  klass.field_name = field_name

  def klass.to_s
    "#{superclass}(#{model_class}##{field_name})"
  end

  klass
end

.value_to_boolean(value) ⇒ Object



31
32
33
# File 'lib/bitmasker/bitmask_attributes.rb', line 31

def self.value_to_boolean(value)
  model_class.value_to_boolean(value)
end

Instance Method Details

#attribute(attribute) ⇒ Object Also known as: attribute?



41
42
43
# File 'lib/bitmasker/bitmask_attributes.rb', line 41

def attribute(attribute)
  bitmask.get attribute
end

#attribute=(attribute, value) ⇒ Object



46
47
48
49
# File 'lib/bitmasker/bitmask_attributes.rb', line 46

def attribute=(attribute, value)
  bitmask.set attribute, self.class.value_to_boolean(value)
  write
end

#attribute_was(attribute) ⇒ Object



51
52
53
# File 'lib/bitmasker/bitmask_attributes.rb', line 51

def attribute_was(attribute)
  Bitmask.new(bitmask_attributes, was).get attribute
end

#readObject

Methods for the model



61
62
63
# File 'lib/bitmasker/bitmask_attributes.rb', line 61

def read
  model[field_name]
end

#to_aObject



55
56
57
# File 'lib/bitmasker/bitmask_attributes.rb', line 55

def to_a
  bitmask.to_a
end

#wasObject



69
70
71
# File 'lib/bitmasker/bitmask_attributes.rb', line 69

def was
  model.attribute_was field_name
end

#writeObject



65
66
67
# File 'lib/bitmasker/bitmask_attributes.rb', line 65

def write
  model[field_name] = bitmask.to_i
end