Class: DeclarativeEnum::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/declarative_enum.rb

Constant Summary collapse

KeyCollisionError =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(definition, block) ⇒ Builder

Returns a new instance of Builder.


94
95
96
97
# File 'lib/declarative_enum.rb', line 94

def initialize(definition, block)
  @definition = definition
  @block = block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *arguments, value: nil, description: nil, &block) ⇒ Object (private)

Raises:


107
108
109
110
111
112
113
114
115
# File 'lib/declarative_enum.rb', line 107

def method_missing(name, *arguments, value: nil, description: nil, &block)
  key = name.downcase.to_sym
  raise KeyCollisionError, "'#{key}' collides with an existing enum key!" if @definition[key]

  @definition[key] = {
    value: value,
    description: description
  }
end

Instance Method Details

#buildObject


99
100
101
102
103
# File 'lib/declarative_enum.rb', line 99

def build
  instance_exec(&@block)

  @definition
end