Class: Terror::ErrorBuilder

Inherits:
BasicObject
Defined in:
lib/terror.rb

Instance Method Summary collapse

Constructor Details

#initialize(klass, from) ⇒ ErrorBuilder

Returns a new instance of ErrorBuilder.



13
14
15
16
# File 'lib/terror.rb', line 13

def initialize(klass, from)
  @klass = klass
  @from = from
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, **kwargs, &blk) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/terror.rb', line 18

def method_missing(name, **kwargs, &blk)
  error = ::Class.new(kwargs.fetch(:from, @from), &blk)

  if kwargs.has_key?(:message)
    error.class_eval("def message; '#{kwargs.fetch(:message)}'; end")
  end

  @klass.const_set(name, error)
end