Class: Bitmasker::BitmaskAttributes
- Inherits:
-
Object
- Object
- Bitmasker::BitmaskAttributes
- Includes:
- ActiveModel::AttributeMethods
- Defined in:
- lib/bitmasker/bitmask_attributes.rb
Instance Attribute Summary collapse
-
#bitmask ⇒ Object
readonly
Returns the value of attribute bitmask.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Class Method Summary collapse
- .make(model_class, field_name, bitmask_attributes, defaults = {}) ⇒ Object
- .value_to_boolean(value) ⇒ Object
Instance Method Summary collapse
- #attribute(attribute) ⇒ Object (also: #attribute?)
- #attribute=(attribute, value) ⇒ Object
- #attribute_was(attribute) ⇒ Object
-
#initialize(model) ⇒ BitmaskAttributes
constructor
A new instance of BitmaskAttributes.
-
#read ⇒ Object
Methods for the model.
- #to_a ⇒ Object
- #was ⇒ Object
- #write ⇒ Object
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
#bitmask ⇒ Object (readonly)
Returns the value of attribute bitmask.
35 36 37 |
# File 'lib/bitmasker/bitmask_attributes.rb', line 35 def bitmask @bitmask end |
#model ⇒ Object (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 |
#read ⇒ Object
Methods for the model
61 62 63 |
# File 'lib/bitmasker/bitmask_attributes.rb', line 61 def read model[field_name] end |
#to_a ⇒ Object
55 56 57 |
# File 'lib/bitmasker/bitmask_attributes.rb', line 55 def to_a bitmask.to_a end |
#was ⇒ Object
69 70 71 |
# File 'lib/bitmasker/bitmask_attributes.rb', line 69 def was model.attribute_was field_name end |
#write ⇒ Object
65 66 67 |
# File 'lib/bitmasker/bitmask_attributes.rb', line 65 def write model[field_name] = bitmask.to_i end |