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
Classes: ENViedString
Constant Summary collapse
- UnsupportedCoercion =
Class.new(StandardError)
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.
- #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.
38 39 40 |
# File 'lib/envied/coercer.rb', line 38 def self.supported_type?(type) supported_types.include?(type.to_sym.downcase) end |
.supported_types ⇒ Object
23 24 25 26 27 |
# 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 |
Instance Method Details
#coerce(string, type) ⇒ type
Coerce strings to specific type.
16 17 18 19 20 21 |
# 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) ⇒ Boolean
54 55 56 |
# File 'lib/envied/coercer.rb', line 54 def coerced?(value) !value.kind_of?(String) end |
#coercer ⇒ Object
50 51 52 |
# File 'lib/envied/coercer.rb', line 50 def coercer @coercer ||= ENViedString.new end |
#coercible?(string, type) ⇒ Boolean
58 59 60 61 62 63 64 |
# 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) ⇒ Boolean
42 43 44 |
# File 'lib/envied/coercer.rb', line 42 def supported_type?(type) self.class.supported_type?(type) end |
#supported_types ⇒ Object
46 47 48 |
# File 'lib/envied/coercer.rb', line 46 def supported_types self.class.supported_types end |