Class: MightyJSON::Type::Enum

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(types) ⇒ Enum

Returns a new instance of Enum.



196
197
198
# File 'lib/mighty_json/type.rb', line 196

def initialize(types)
  @types = types
end

Instance Attribute Details

#typesObject (readonly)

Returns the value of attribute types.



194
195
196
# File 'lib/mighty_json/type.rb', line 194

def types
  @types
end

Instance Method Details

#compile(var:, path:) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/mighty_json/type.rb', line 200

def compile(var:, path:)
  v = var.cur
  "    \#{@types.map do |type|\n      <<~END2.chomp\n        begin\n          \#{type.compile(var: var, path: path)}\n        rescue Error, UnexpectedFieldError, IllegalTypeError\n        end\n      END2\n    end.join(' || ')} || (raise Error.new(path: \#{path.inspect}, type: \#{self.to_s.inspect}, value: \#{v}))\n  END\nend\n"

#to_sObject



214
215
216
# File 'lib/mighty_json/type.rb', line 214

def to_s
  "enum(#{types.map(&:to_s).join(", ")})"
end