Class: MightyJSON::Type::Base

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

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ Base

Returns a new instance of Base.



6
7
8
# File 'lib/mighty_json/type.rb', line 6

def initialize(type)
  @type = type
end

Instance Method Details

#compile(var:, path:) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
# File 'lib/mighty_json/type.rb', line 10

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



57
58
59
# File 'lib/mighty_json/type.rb', line 57

def to_s
  @type.to_s
end