Module: Attribool
- Defined in:
- lib/attribool.rb,
 lib/attribool/value.rb,
 lib/attribool/version.rb,
 lib/attribool/attribute.rb,
 lib/attribool/validators.rb,
 lib/attribool/reader_name.rb,
 lib/attribool/attribute_list.rb,
 lib/attribool/validator_service.rb
Overview
Adds macros for dealing with boolean attributes.
Defined Under Namespace
Modules: Validators, Version Classes: Attribute, AttributeList, ReaderName, ValidatorService, Value
Constant Summary collapse
Instance Method Summary collapse
- 
  
    
      #bool_accessor(*attributes)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Creates a simple reader and writer for booleans. 
- 
  
    
      #bool_reader(*attributes, allow_nil: true, method_name: nil, condition: nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Creates methods that return a boolean for attributes that may or may not be booleans themselves. 
- 
  
    
      #bool_writer(*attributes, strict: false)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Creates a writer for boolean attributes. 
Instance Method Details
#bool_accessor(*attributes) ⇒ Object
Creates a simple reader and writer for booleans. This should only be used when the attribute should only ever be true or false.
| 82 83 84 85 | # File 'lib/attribool.rb', line 82 def bool_accessor(*attributes) bool_reader(*attributes) bool_writer(*attributes) end | 
#bool_reader(*attributes, allow_nil: true, method_name: nil, condition: nil) ⇒ Object
Creates methods that return a boolean for attributes that may or may not be booleans themselves. Multiple readers can be created at once
| 46 47 48 49 50 51 52 53 54 55 56 57 58 | # File 'lib/attribool.rb', line 46 def bool_reader(*attributes, allow_nil: true, method_name: nil, condition: nil) ValidatorService.call(:method_name, method_name, attributes.size) AttributeList.build(*attributes, method_name: method_name).each do |attribute| define_method(attribute.reader) do instance_variable_get(attribute.ivar).then do |value| ValidatorService.call(:nil_attribute, attribute.ivar, value, allow_nil) Value.new(value, condition).to_boolean end end end end | 
#bool_writer(*attributes, strict: false) ⇒ Object
Creates a writer for boolean attributes. Always coerces to boolean based on truthiness.
| 67 68 69 70 71 72 73 74 75 | # File 'lib/attribool.rb', line 67 def bool_writer(*attributes, strict: false) AttributeList.build(*attributes).each do |attribute| define_method(attribute.writer) do |value| ValidatorService.call(:strict_boolean, value, strict) instance_variable_set(attribute.ivar, Value.new(value).to_boolean) end end end |