module StrategyStore::Implementation

Public Class Methods

new(context_model, strategy_parameters) click to toggle source

TODO : must be private

# File lib/strategy_store/implementation.rb, line 85
def initialize(context_model, strategy_parameters)
  columns         = self.class.columns
  @context_model  = context_model

  # TODO - rpc : Use the defined AccessorHash
  #@strategy_hash = const_get("#{name}_accessor_hash".camelize).new(strategy_parameters, columns)
  @strategy_hash = ::StrategyStore::Strategy::AccessorHash.new(strategy_parameters, columns)
end

Public Instance Methods

columns() click to toggle source

Return the columns of the strategy

# File lib/strategy_store/implementation.rb, line 82
def columns; self.class.columns; end
generate_abstract_strategy_method() click to toggle source

Generate the abstract method of the strategy that are defined in the configuration

# File lib/strategy_store/implementation.rb, line 66
def generate_abstract_strategy_method
  implemented_strategy.strategy_methods.each do |method|
    # TODO : in case of inherited strategy. Do not override the method
    # TODO : Must be tested
    unless self.respond_to?(method)
      self.send(:define_method, method) do |*args|
        raise NotImplementedError.new("Abstract method #{method} not implemented, or unsupported operation")
      end
    end
  end
end
generate_strategy_accessors() click to toggle source

Generate the abstract method of the strategy that are defined in the strategy_columns yield

# File lib/strategy_store/implementation.rb, line 46
def generate_strategy_accessors

  attributes = []
  columns.keys.each do |column_name|
    attributes << column_name

    self.send(:define_attribute_methods, column_name)
    self.send(:define_method, column_name) { @strategy_hash[column_name] }
    self.send(:define_method, "#{column_name}=") do |value|
      send("#{column_name}_will_change!") unless value == instance_variable_get("@_#{column_name}")
      @strategy_hash[column_name] = value
    end
  end

  self.send(:define_method, :attributes) do
    Hash[attributes.map { |name, _| [name, send(name)] }]
  end
end
name() click to toggle source

Return the name of the strategy

# File lib/strategy_store/implementation.rb, line 80
def name;    self.class.to_s;    end
register_as_strategy(strategy_ui_id) click to toggle source
# File lib/strategy_store/implementation.rb, line 19
def register_as_strategy(strategy_ui_id)
  raise StrategyStore::StrategyAlreadyImplemented.new(self) if self.implemented_strategy
  self.implemented_strategy = StrategyStore.fetch_strategy(strategy_ui_id)
  self.implemented_strategy.add_implementation(self)
end
register_columns(attr_columns) click to toggle source
# File lib/strategy_store/implementation.rb, line 37
def register_columns(attr_columns)
  self.columns ||= {}
  self.columns.merge!(attr_columns.index_by(&:name))
  # TODO - rpc : Define a AccessorHash with the columns
  # accessor_hash_class = AccessorHash.create(columns)
  # const_set("#{name}_accessor_hash".camelize, accessor_hash_class)
end
strategy_columns(&block) click to toggle source
# File lib/strategy_store/implementation.rb, line 25
def strategy_columns(&block); strategy_columns_for(:default, &block); end
strategy_columns_for(implemented_strategy, &block) click to toggle source
# File lib/strategy_store/implementation.rb, line 27
def strategy_columns_for(implemented_strategy, &block)
  register_as_strategy(implemented_strategy)
  dsl = ::StrategyStore::Strategy::DSL.new(&block)
  register_columns(dsl.columns)
  generate_strategy_accessors
  generate_abstract_strategy_method
end