class ENVied::Coercer
Responsible for all string to type coercions.
Constants
- UnsupportedCoercion
Public Class Methods
supported_type?(type)
click to toggle source
Whether or not Coercer
can coerce strings to the provided type.
@param type [#to_sym] the type (case insensitive)
@example
ENVied::Coercer.supported_type?('string') # => true
@return [Boolean] whether type is supported.
# File lib/envied/coercer.rb, line 38 def self.supported_type?(type) supported_types.include?(type.to_sym.downcase) end
supported_types()
click to toggle source
# File lib/envied/coercer.rb, line 23 def self.supported_types @supported_types ||= begin [:hash, :array, :time, :date, :symbol, :boolean, :integer, :string, :uri, :float].sort end end
Public Instance Methods
coerce(string, type)
click to toggle source
Coerce strings to specific type.
@param string [String] the string to be coerced @param type [#to_sym] the type to coerce to
@example
ENVied::Coercer.new.coerce('1', :Integer) # => 1
@return [type] the coerced string.
# File lib/envied/coercer.rb, line 16 def coerce(string, type) unless supported_type?(type) raise ArgumentError, "The type `#{type.inspect}` is not supported." end coercer.public_send("to_#{type.downcase}", string) end
coerced?(value)
click to toggle source
# File lib/envied/coercer.rb, line 54 def coerced?(value) !value.kind_of?(String) end
coercer()
click to toggle source
# File lib/envied/coercer.rb, line 50 def coercer @coercer ||= ENViedString.new end
coercible?(string, type)
click to toggle source
# File lib/envied/coercer.rb, line 58 def coercible?(string, type) return false unless supported_type?(type) coerce(string, type) true rescue UnsupportedCoercion false end
supported_type?(type)
click to toggle source
# File lib/envied/coercer.rb, line 42 def supported_type?(type) self.class.supported_type?(type) end
supported_types()
click to toggle source
# File lib/envied/coercer.rb, line 46 def supported_types self.class.supported_types end