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
- #==(other) ⇒ Object
- #coerce(value, path: ErrorPath.root(self)) ⇒ Object
-
#initialize(type) ⇒ Base
constructor
A new instance of Base.
- #test(value) ⇒ Object
- #to_s ⇒ Object
Methods included from WithAlias
Methods included from Match
Constructor Details
#initialize(type) ⇒ Base
Returns a new instance of Base.
37 38 39 |
# File 'lib/strong_json/type.rb', line 37 def initialize(type) @type = type end |
Instance Attribute Details
#type ⇒ Object (readonly)
Returns the value of attribute type.
35 36 37 |
# File 'lib/strong_json/type.rb', line 35 def type @type end |
Instance Method Details
#==(other) ⇒ Object
77 78 79 80 81 82 |
# File 'lib/strong_json/type.rb', line 77 def ==(other) if other.is_a?(Base) # @type var other: Base<any> other.type == type end end |
#coerce(value, path: ErrorPath.root(self)) ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/strong_json/type.rb', line 62 def coerce(value, path: ErrorPath.root(self)) raise TypeError.new(value: value, path: path) unless test(value) case type when :symbol value.to_sym else value end end |
#test(value) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/strong_json/type.rb', line 41 def test(value) case @type when :any true when :number value.is_a?(Numeric) when :integer value.is_a?(Integer) 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 when :symbol value.is_a?(String) || value.is_a?(Symbol) else false end end |
#to_s ⇒ Object
73 74 75 |
# File 'lib/strong_json/type.rb', line 73 def to_s self.alias&.to_s || @type.to_s end |