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.



177
178
179
# File 'lib/mighty_json/type.rb', line 177

def initialize(types)
  @types = types
end

Instance Attribute Details

#typesObject (readonly)

Returns the value of attribute types.



175
176
177
# File 'lib/mighty_json/type.rb', line 175

def types
  @types
end

Instance Method Details

#compile(var:, path:) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/mighty_json/type.rb', line 181

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



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

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