Class: Module
- Inherits:
-
Object
- Object
- Module
- Defined in:
- lib/module_extensions.rb
Overview
Extend module with attr_boolean
Instance Method Summary collapse
-
#battr_accessor(*attributes, default: false, raw: false, reader: raw, bang: true, writer: bang) ⇒ Object
(also: #attr_boolean)
battr_accessor(names, …, options) attr_boolean(names, …, options).
Instance Method Details
#battr_accessor(*attributes, default: false, raw: false, reader: raw, bang: true, writer: bang) ⇒ Object Also known as: attr_boolean
battr_accessor(names, …, options) attr_boolean(names, …, options)
Defines a boolean style attribute for the module or class with a query method (attribute?) and optional writers and defaults If the name does not end with a question mark (?) then it is added
Options
-
:default- Sets the default value for the attributes(defaults to false). -
:raw- Raw mode stores actual values instead of coercing values to booleans.(defaults to false). -
:reader- Creates a reader method for the attribute (attribute)(By default this is enabled when raw mode is enabled). -
:bang- Creates a bang method to set the attribute to true (attribute!)(defaults to true). -
:writer- Creates a write method for the attribute (attribute = vale)(defaults to the same value as bang).
Examples
class Base
attr_boolean :test? # read-write value
attr_boolean :active?, writer: false # read only boolean
end
Base.new.test? Base.new.test=(false) Base.new.test! Base.new.active? Base.new.active=(false) # NoMethodFound Base.new.active! # NoMethodFound
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/module_extensions.rb', line 40 def battr_accessor(*attributes, # rubocop:disable Metrics/ParameterLists default: false, # default attribute value raw: false, # treat this as a raw attribute allowing non-boolean values reader: raw, # create a reader method (attribute) bang: true, # create a bang method to set the attribute (attribute!) writer: bang) # create a writer method (attribute=) impl = BoolAttrAccessor::Implementation.new(self, raw, reader, writer, bang) attributes.map do |attribute| impl.create(attribute, default) end end |