Class: Reaction::TypeBuilder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type = RawType, options = {}) ⇒ TypeBuilder

Returns a new instance of TypeBuilder.



6
7
8
9
# File 'lib/reaction/type_builder.rb', line 6

def initialize(type = RawType, options = {})
  @type = class_for_type!(type)
  @options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/reaction/type_builder.rb', line 4

def options
  @options
end

#typeObject

Returns the value of attribute type.



3
4
5
# File 'lib/reaction/type_builder.rb', line 3

def type
  @type
end

Instance Method Details

#build(action) ⇒ Object



11
12
13
# File 'lib/reaction/type_builder.rb', line 11

def build(action)
  type.new(action, options)
end

#class_for_type(type) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/reaction/type_builder.rb', line 15

def class_for_type(type)
  return type if type.is_a?(Class)
  name = type.to_s.split('_').map(&:capitalize).join
  Module.const_get("#{name}Type")
rescue NameError
  nil
end

#class_for_type!(type) ⇒ Object



23
24
25
# File 'lib/reaction/type_builder.rb', line 23

def class_for_type!(type)
  class_for_type(type) or raise ArgumentError.new("Unknown type: #{type}")
end