class Usine::OperationWrapper

Public Class Methods

call(mode, operation, attributes = {}) click to toggle source
# File lib/usine/operation_wrapper.rb, line 19
def self.call(mode, operation, attributes = {})
  wrapper = new(mode, operation, attributes)
  wrapper.process
end
new(mode, operation, attributes = {}) click to toggle source
# 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

Public Instance Methods

method_missing(method_symbol, factory_name = nil, attributes = {}, &block) click to toggle source
# 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
operations() click to toggle source
# File lib/usine/operation_wrapper.rb, line 15
def operations
  @operations ||= Usine.operations
end
operations=(operations) click to toggle source
# File lib/usine/operation_wrapper.rb, line 11
def operations=(operations)
  @operations = operations
end
process() click to toggle source
# 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

Protected Instance Methods

attributes_for_factory_name(name) click to toggle source
# File lib/usine/operation_wrapper.rb, line 46
def attributes_for_factory_name(name)
  factory = FactoryGirl.factory_by_name(name)
  factory.run(FactoryGirl::Strategy::AttributesFor, {})
end
find_operation_factory(operation_class) click to toggle source
# File lib/usine/operation_wrapper.rb, line 51
    def find_operation_factory(operation_class)
      operation_factory = operations.detect do |op|
        op.operation_class == operation_class
      end

      unless operation_factory
        message = <<-MSG
Couldn’t find an operation factory for: #{operation_class}

Please define it with:
Usine.operation(#{operation_class}) {}
MSG
        raise(ArgumentError::NotFoundOperationFactory, message)
      end

      operation_factory
    end