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
  <<~END
    #{@types.map do |type|
      <<~END2.chomp
        begin
          #{type.compile(var: var, path: path)}
        rescue Error, UnexpectedFieldError, IllegalTypeError
        end
      END2
    end.join(' || ')} || (raise Error.new(path: #{path.inspect}, type: #{self.to_s.inspect}, value: #{v}))
  END
end

#to_sObject



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

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