Class: Usine::OperationWrapper

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode, operation, attributes = {}) ⇒ OperationWrapper

Returns a new instance of OperationWrapper.



3
4
5
6
7
8
9
# File 'lib/usine/operation_wrapper.rb', line 3

def initialize(mode, operation, attributes = {})
  @mode = mode
  @operation = operation
  @operation_factory = find_operation_factory(operation)
  @attributes = attributes
  @factory_attributes = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_symbol, factory_name = nil, attributes = {}, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/usine/operation_wrapper.rb', line 30

def method_missing(method_symbol, factory_name = nil, attributes = {}, &block)
  if factory_name
    case factory_name
    when Symbol, String
      @factory_attributes[method_symbol] = attributes_for_factory_name(factory_name)
    else
      merged_attributes = Utils.merge_hashes(attributes, @attributes)
      @factory_attributes[method_symbol] = factory_name.call(merged_attributes).model
    end
  else
    @factory_attributes[method_symbol] = attributes_for_factory_name(method_symbol)
  end
end

Class Method Details

.call(mode, operation, attributes = {}) ⇒ Object



19
20
21
22
# File 'lib/usine/operation_wrapper.rb', line 19

def self.call(mode, operation, attributes = {})
  wrapper = new(mode, operation, attributes)
  wrapper.process
end

Instance Method Details

#operationsObject



15
16
17
# File 'lib/usine/operation_wrapper.rb', line 15

def operations
  @operations ||= Usine.operations
end

#operations=(operations) ⇒ Object



11
12
13
# File 'lib/usine/operation_wrapper.rb', line 11

def operations=(operations)
  @operations = operations
end

#processObject



24
25
26
27
28
# File 'lib/usine/operation_wrapper.rb', line 24

def process
  self.instance_eval(&@operation_factory.block)
  merged_attributes = Utils.merge_hashes(@factory_attributes, @attributes)
  @operation.send(@mode, merged_attributes)
end