Sha256: cdd87ee54c66fe0bb9cecc23d1fc31b7e06515428cac4b2c2aff4585e3a54ae2
Contents?: true
Size: 1.3 KB
Versions: 1
Compression:
Stored size: 1.3 KB
Contents
module ShallowAttributes module Type # Abstract class for typecast object to Boolean type. # # @abstract # # @since 0.1.0 class Boolean # Array of true values # # @private # # @since 0.1.0 TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE', 'on', 'ON'].freeze # Array of false values # # @private # # @since 0.1.0 FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF', nil].freeze # Convert value to Boolean type # # @private # # @param [Object] value # @param [Hash] option # # @example Convert integer to boolean value # ShallowAttributes::Type::Boolean.new.coerce(1) # # => true # # ShallowAttributes::Type::Boolean.new.coerce(0) # # => false # # @raise [InvalidValueError] if values is not included in true and false arrays # # @return [boolean] # # @since 0.1.0 def coerce(value, _options = {}) if TRUE_VALUES.include?(value) true elsif FALSE_VALUES.include?(value) false else raise ShallowAttributes::Type::InvalidValueError, %(Invalid value "#{value}" for type "Boolean") end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
shallow_attributes-0.9.2 | lib/shallow_attributes/type/boolean.rb |