Class: StrongJSON::Type::Base
- Inherits:
-
Object
- Object
- StrongJSON::Type::Base
- Defined in:
- lib/strong_json/type.rb
Instance Attribute Summary collapse
-
#type ⇒ Object
readonly
Returns the value of attribute type.
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.
8 9 10 |
# File 'lib/strong_json/type.rb', line 8 def initialize(type) @type = type end |
Instance Attribute Details
#type ⇒ Object (readonly)
Returns the value of attribute type.
6 7 8 |
# File 'lib/strong_json/type.rb', line 6 def type @type end |
Instance Method Details
#coerce(value, path: []) ⇒ Object
31 32 33 34 35 |
# File 'lib/strong_json/type.rb', line 31 def coerce(value, path: []) raise Error.new(value: value, type: self, path: path) unless test(value) raise IllegalTypeError.new(type: self) if path == [] && @type == :ignored @type != :ignored ? value : NONE end |
#test(value) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/strong_json/type.rb', line 12 def test(value) case @type when :ignored true 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
37 38 39 |
# File 'lib/strong_json/type.rb', line 37 def to_s @type.to_s end |