Class: ENVied::Coercer
- Inherits:
-
Object
- Object
- ENVied::Coercer
- Defined in:
- lib/envied/coercer.rb
Overview
Responsible for all string to type coercions.
Defined Under Namespace
Modules: CoercerExts
Class Method Summary collapse
-
.supported_type?(type) ⇒ Boolean
Whether or not Coercer can coerce strings to the provided type.
- .supported_types ⇒ Object
Instance Method Summary collapse
-
#coerce(string, type) ⇒ type
Coerce strings to specific type.
- #coerce_method_for(type) ⇒ Object
- #coerced?(value) ⇒ Boolean
- #coercer ⇒ Object
- #coercible?(string, type) ⇒ Boolean
- #supported_type?(type) ⇒ Boolean
- #supported_types ⇒ Object
Class Method Details
.supported_type?(type) ⇒ Boolean
Whether or not Coercer can coerce strings to the provided type.
54 55 56 |
# File 'lib/envied/coercer.rb', line 54 def self.supported_type?(type) supported_types.include?(type.to_sym.downcase) end |
.supported_types ⇒ Object
39 40 41 42 43 |
# File 'lib/envied/coercer.rb', line 39 def self.supported_types @supported_types ||= begin [:hash, :array, :time, :date, :symbol, :boolean, :integer, :string].sort end end |
Instance Method Details
#coerce(string, type) ⇒ type
Coerce strings to specific type.
27 28 29 30 31 32 |
# File 'lib/envied/coercer.rb', line 27 def coerce(string, type) unless supported_type?(type) raise ArgumentError, "#{type.inspect} is not supported type" end coerce_method_for(type.to_sym)[string] end |
#coerce_method_for(type) ⇒ Object
34 35 36 37 |
# File 'lib/envied/coercer.rb', line 34 def coerce_method_for(type) return nil unless supported_type?(type) coercer.method("to_#{type.downcase}") end |
#coerced?(value) ⇒ Boolean
70 71 72 |
# File 'lib/envied/coercer.rb', line 70 def coerced?(value) !value.kind_of?(String) end |
#coercer ⇒ Object
66 67 68 |
# File 'lib/envied/coercer.rb', line 66 def coercer @coercer ||= Coercible::Coercer.new[String] end |
#coercible?(string, type) ⇒ Boolean
74 75 76 77 78 79 80 |
# File 'lib/envied/coercer.rb', line 74 def coercible?(string, type) return false unless supported_type?(type) coerce(string, type) true rescue Coercible::UnsupportedCoercion false end |
#supported_type?(type) ⇒ Boolean
58 59 60 |
# File 'lib/envied/coercer.rb', line 58 def supported_type?(type) self.class.supported_type?(type) end |
#supported_types ⇒ Object
62 63 64 |
# File 'lib/envied/coercer.rb', line 62 def supported_types self.class.supported_types end |