Module: BitmaskAttributesHelpers::ClassMethods
- Defined in:
- lib/bitmask_attributes_helpers.rb
Instance Method Summary collapse
-
#bitmask_helpers(bitmask) ⇒ Object
Set up all bitmask helpers: Virtual Attributes, scopes, and presence checkers.
-
#bitmask_presence_methods(bitmask) ⇒ Object
Setup presence checking methods for Bitmask attributes.
-
#bitmask_scopes(bitmask) ⇒ Object
Setup scopes for Bitmask attributes.
-
#bitmask_virtual_attributes(bitmask) ⇒ Object
Setup virtual attributes for Bitmask attributes.
Instance Method Details
#bitmask_helpers(bitmask) ⇒ Object
Set up all bitmask helpers: Virtual Attributes, scopes, and presence checkers
49 50 51 52 53 |
# File 'lib/bitmask_attributes_helpers.rb', line 49 def bitmask_helpers(bitmask) bitmask_presence_methods(bitmask) bitmask_scopes(bitmask) bitmask_virtual_attributes(bitmask) end |
#bitmask_presence_methods(bitmask) ⇒ Object
Setup presence checking methods for Bitmask attributes
Presence attributes are setup as the possible values suffixed by a question mark, and return true or false
12 13 14 15 16 17 18 |
# File 'lib/bitmask_attributes_helpers.rb', line 12 def bitmask_presence_methods(bitmask) send("values_for_#{bitmask}").each do |value| define_method "#{value}?" do send("#{bitmask}?", value) end end end |
#bitmask_scopes(bitmask) ⇒ Object
Setup scopes for Bitmask attributes
Scopes are setup with the same name as the value, and include both .value and .not_value versions
25 26 27 28 29 30 |
# File 'lib/bitmask_attributes_helpers.rb', line 25 def bitmask_scopes(bitmask) send("values_for_#{bitmask}").each do |value| scope value, send("with_#{bitmask}", value) scope "not_#{value}", send("without_#{bitmask}", value) end end |
#bitmask_virtual_attributes(bitmask) ⇒ Object
Setup virtual attributes for Bitmask attributes
Allows you to set and read Bitmask attributes using #value= and #value methods
37 38 39 40 41 42 |
# File 'lib/bitmask_attributes_helpers.rb', line 37 def bitmask_virtual_attributes(bitmask) send("values_for_#{bitmask}").each do |value| define_method("#{value}") { send("#{bitmask}?", value) } define_method("#{value}=") { |arg| send("#{bitmask}=", arg.blank? || arg == "0" ? send("#{bitmask}") - [value] : send("#{bitmask}") << value) } end end |