Class: CastingAttributes::TypeCaster::Boolean

Inherits:
Base
  • Object
show all
Defined in:
lib/casting_attributes/type_caster/boolean.rb

Instance Attribute Summary

Attributes inherited from Base

#model

Instance Method Summary collapse

Methods inherited from Base

#initialize, #storage_var, #writers

Constructor Details

This class inherits a constructor from CastingAttributes::TypeCaster::Base

Instance Method Details

#attributeObject



5
6
7
# File 'lib/casting_attributes/type_caster/boolean.rb', line 5

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

#readersObject



9
10
11
# File 'lib/casting_attributes/type_caster/boolean.rb', line 9

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

#typecast(value) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/casting_attributes/type_caster/boolean.rb', line 13

def typecast(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