Class: MightyJSON::Type::Enum
- Inherits:
-
Object
- Object
- MightyJSON::Type::Enum
- Defined in:
- lib/mighty_json/type.rb
Instance Attribute Summary collapse
-
#types ⇒ Object
readonly
Returns the value of attribute types.
Instance Method Summary collapse
- #compile(var:, path:) ⇒ Object
-
#initialize(types) ⇒ Enum
constructor
A new instance of Enum.
- #to_s ⇒ Object
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
#types ⇒ Object (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_s ⇒ Object
195 196 197 |
# File 'lib/mighty_json/type.rb', line 195 def to_s "enum(#{types.map(&:to_s).join(", ")})" end |