Class: StrongJSON::Type::Base
- Inherits:
-
Object
- Object
- StrongJSON::Type::Base
- Defined in:
- lib/strong_json/type.rb
Instance Method Summary collapse
- #coerce(value, path: []) ⇒ Object
-
#initialize(type) ⇒ Base
constructor
A new instance of Base.
- #test(value) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(type) ⇒ Base
Returns a new instance of Base.
4 5 6 |
# File 'lib/strong_json/type.rb', line 4 def initialize(type) @type = type end |
Instance Method Details
#coerce(value, path: []) ⇒ Object
27 28 29 30 |
# File 'lib/strong_json/type.rb', line 27 def coerce(value, path: []) raise Error.new(value: value, type: self, path: path) unless test(value) value end |
#test(value) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/strong_json/type.rb', line 8 def test(value) case @type when :prohibited false when :any true when :number value.is_a?(Numeric) when :string value.is_a?(String) when :boolean value == true || value == false when :numeric value.is_a?(Numeric) || value.is_a?(String) && /\A[\d.]+\Z/ =~ value else false end end |
#to_s ⇒ Object
32 33 34 |
# File 'lib/strong_json/type.rb', line 32 def to_s @type.to_s end |