Module: Strategic::ClassMethods
- Defined in:
- lib/strategic.rb
Instance Method Summary collapse
- #default_strategy(string_or_class_or_object = nil) ⇒ Object
- #new_with_strategy(string_or_class_or_object, *args, &block) ⇒ Object
- #require_strategies ⇒ Object
- #strategies ⇒ Object
- #strategy_class_for(string_or_class_or_object) ⇒ Object
- #strategy_class_with_strategy_matcher(string_or_class_or_object) ⇒ Object
- #strategy_class_without_strategy_matcher(string_or_class_or_object) ⇒ Object
- #strategy_matcher(&matcher_block) ⇒ Object
- #strategy_matcher_for_any_strategy? ⇒ Boolean
- #strategy_names ⇒ Object
Instance Method Details
#default_strategy(string_or_class_or_object = nil) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/strategic.rb', line 37 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) ⇒ Object
89 90 91 92 93 |
# File 'lib/strategic.rb', line 89 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 ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/strategic.rb', line 49 def require_strategies klass_path = caller[1].split(':').first strategy_path = File.(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 ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/strategic.rb', line 95 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) ⇒ Object
57 58 59 60 61 |
# File 'lib/strategic.rb', line 57 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) ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/strategic.rb', line 63 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) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/strategic.rb', line 72 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) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/strategic.rb', line 29 def strategy_matcher(&matcher_block) if matcher_block.nil? @strategy_matcher else @strategy_matcher = matcher_block end end |
#strategy_matcher_for_any_strategy? ⇒ Boolean
45 46 47 |
# File 'lib/strategic.rb', line 45 def strategy_matcher_for_any_strategy? !!(strategy_matcher || strategies.any?(&:strategy_matcher)) end |
#strategy_names ⇒ Object
105 106 107 |
# File 'lib/strategic.rb', line 105 def strategy_names strategies.map(&:strategy_name) end |