Module: Crystalline::Enum::ClassMethods

Defined in:
lib/crystalline/types.rb

Instance Method Summary collapse

Instance Method Details

#deserialize(val) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/crystalline/types.rb', line 70

def deserialize(val)
  if @mapping.include? val
    @mapping[val]
  else
    raise "Invalid value for enum: #{val}"
  end
end

#enums(&blk) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/crystalline/types.rb', line 57

def enums(&blk)
  @mapping = {}

  yield
  constants(false).each do |const_name|
    instance = const_get(const_name, false)
    unless instance.is_a? self
      raise 'Enum constants must be instances of the Enum class (e.g. `Foo = new`)'
    end
    @mapping[instance.serialize] = instance
  end
end