Class: CastingAttrAccessor::Parser::Boolean

Inherits:
Base
  • Object
show all
Defined in:
lib/casting_attr_accessor/parser/boolean.rb

Instance Attribute Summary

Attributes inherited from Base

#model, #options

Instance Method Summary collapse

Methods inherited from Base

#initialize, #storage_var, #writers

Constructor Details

This class inherits a constructor from CastingAttrAccessor::Parser::Base

Instance Method Details

#attributeObject



5
6
7
# File 'lib/casting_attr_accessor/parser/boolean.rb', line 5

def attribute
  super.to_s.gsub('?', '').to_sym
end

#default_value(object) ⇒ Object



9
10
11
# File 'lib/casting_attr_accessor/parser/boolean.rb', line 9

def default_value(object)
  super(object) || false
end

#parse(value) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/casting_attr_accessor/parser/boolean.rb', line 17

def parse(value)
  case value
    when ::TrueClass, ::FalseClass
      value
    else
      value = value.to_s.strip.downcase
      %w(t true).include?(value) || value.to_i == 1
  end
end

#readersObject



13
14
15
# File 'lib/casting_attr_accessor/parser/boolean.rb', line 13

def readers
  super << :"#{attribute}?"
end