module Strategic::ClassMethods

Public Instance Methods

default_strategy(string_or_class_or_object = nil) click to toggle source
# File lib/strategic.rb, line 62
def default_strategy(string_or_class_or_object = nil)
  if string_or_class_or_object.nil?
    @default_strategy
  else
    @default_strategy = strategy_class_for(string_or_class_or_object)
  end
end
new_with_strategy(string_or_class_or_object, *args, &block) click to toggle source
# File lib/strategic.rb, line 114
def new_with_strategy(string_or_class_or_object, *args, &block)
  new(*args, &block).tap do |model|
    model.strategy = string_or_class_or_object
  end
end
require_strategies() click to toggle source
# File lib/strategic.rb, line 74
def require_strategies
  klass_path = caller[1].split(':').first
  strategy_path = File.expand_path(File.join(klass_path, '..', Strategic.underscore(self.name), '**', '*.rb'))
  Dir.glob(strategy_path) do |strategy|
    Object.const_defined?(:Rails) ? require_dependency(strategy) : require(strategy)
  end
end
strategies() click to toggle source
# File lib/strategic.rb, line 120
def strategies
  constants.map do |constant_symbol|
    const_get(constant_symbol)
  end.select do |constant|
    constant.respond_to?(:ancestors)
  end.select do |constant|
    constant.ancestors.include?(Strategic::Strategy) && constant.name.split('::').last.end_with?('Strategy') && constant.name.split('::').last != 'Strategy' # has to be something like PrefixStrategy
  end.sort_by(&:strategy_name)
end
strategy_class_for(string_or_class_or_object) click to toggle source
# File lib/strategic.rb, line 82
def strategy_class_for(string_or_class_or_object)
  strategy_class = strategy_matcher_for_any_strategy? ? strategy_class_with_strategy_matcher(string_or_class_or_object) : strategy_class_without_strategy_matcher(string_or_class_or_object)
  strategy_class ||= strategies.detect { |strategy| strategy.strategy_aliases.include?(string_or_class_or_object) }
  strategy_class ||= default_strategy
end
strategy_class_with_strategy_matcher(string_or_class_or_object) click to toggle source
# File lib/strategic.rb, line 88
def strategy_class_with_strategy_matcher(string_or_class_or_object)
  strategies.detect do |strategy|
    match = strategy.strategy_aliases.include?(string_or_class_or_object)
    match ||= strategy&.strategy_matcher&.call(string_or_class_or_object) || (strategy_matcher && strategy.instance_exec(string_or_class_or_object, &strategy_matcher))
    # match unless excluded or included by another strategy as an alias
    match unless strategy.strategy_exclusions.include?(string_or_class_or_object) || (strategies - [strategy]).map(&:strategy_aliases).flatten.include?(string_or_class_or_object)
  end
end
strategy_class_without_strategy_matcher(string_or_class_or_object) click to toggle source
# File lib/strategic.rb, line 97
def strategy_class_without_strategy_matcher(string_or_class_or_object)
  if string_or_class_or_object.is_a?(String)
    strategy_class_name = string_or_class_or_object.downcase
  elsif string_or_class_or_object.is_a?(Class)
    strategy_class_name = string_or_class_or_object.name
  else
    strategy_class_name = string_or_class_or_object.class.name
  end
  return nil if strategy_class_name.to_s.strip.empty?
  begin
    class_name = "::#{self.name}::#{Strategic.classify(strategy_class_name)}Strategy"
    class_eval(class_name)
  rescue NameError
    # No Op
  end
end
strategy_matcher(&matcher_block) click to toggle source
# File lib/strategic.rb, line 54
def strategy_matcher(&matcher_block)
  if matcher_block.nil?
    @strategy_matcher
  else
    @strategy_matcher = matcher_block
  end
end
strategy_matcher_for_any_strategy?() click to toggle source
# File lib/strategic.rb, line 70
def strategy_matcher_for_any_strategy?
  !!(strategy_matcher || strategies.any?(&:strategy_matcher))
end
strategy_names() click to toggle source
# File lib/strategic.rb, line 130
def strategy_names
  strategies.map(&:strategy_name)
end