Class: StrongJSON::Type::Enum

Inherits:
Object
  • Object
show all
Includes:
Match
Defined in:
lib/strong_json/type.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Match

#===, #=~

Constructor Details

#initialize(types) ⇒ Enum

Returns a new instance of Enum.



198
199
200
# File 'lib/strong_json/type.rb', line 198

def initialize(types)
  @types = types
end

Instance Attribute Details

#typesObject (readonly)

Returns the value of attribute types.



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

def types
  @types
end

Instance Method Details

#coerce(value, path: []) ⇒ Object



206
207
208
209
210
211
212
213
214
# File 'lib/strong_json/type.rb', line 206

def coerce(value, path: [])
  type = types.find {|ty| ty =~ value }

  if type
    type.coerce(value, path: path)
  else
    raise Error.new(path: path, type: self, value: value)
  end
end

#to_sObject



202
203
204
# File 'lib/strong_json/type.rb', line 202

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