Class: MightyJSON::Type::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/mighty_json/type.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ Base

Returns a new instance of Base.



20
21
22
# File 'lib/mighty_json/type.rb', line 20

def initialize(type)
  @type = type
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



18
19
20
# File 'lib/mighty_json/type.rb', line 18

def type
  @type
end

Instance Method Details

#compile(var:, path:) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mighty_json/type.rb', line 24

def compile(var:, path:)
  v = var.cur
  err = "(raise Error.new(value: #{v}, type: #{self.to_s.inspect}, path: #{path.inspect}))"
  case @type
  when :ignored
    if path == []
      'raise IllegalTypeError.new(type: :ignored)'
    else
      'none'
    end
  when :any
    v
  when :number
    "      \#{v}.is_a?(Numeric) ?\n        \#{v} :\n        \#{err}\n    END\n  when :string\n    <<~END\n      \#{v}.is_a?(String) ?\n        \#{v} :\n        \#{err}\n    END\n  when :boolean\n    <<~END\n      \#{v} == true || \#{v} == false ?\n        \#{v} :\n        \#{err}\n    END\n  when :numeric\n    <<~END\n      \#{v}.is_a?(Numeric) || (\#{v}.is_a?(String) && /\\\\A[-+]?[\\\\d.]+\\\\Z/ =~ \#{v}) ?\n        \#{v} :\n        \#{err}\n    END\n  when :symbol\n    <<~END\n      \#{v}.is_a?(Symbol) ?\n        \#{v} :\n        \#{err}\n    END\n  else\n    err\n  end\nend\n"

#to_sObject



71
72
73
# File 'lib/mighty_json/type.rb', line 71

def to_s
  @type.to_s
end