Module: Strategic::ClassMethods
- Defined in:
- lib/strategic.rb
Instance Method Summary collapse
- #default_strategy(string_or_class_or_object = nil) ⇒ Object
- #new_with_default_strategy(*args, &block) ⇒ 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
63 64 65 66 67 68 69 |
# File 'lib/strategic.rb', line 63 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_default_strategy(*args, &block) ⇒ Object
115 116 117 118 119 |
# File 'lib/strategic.rb', line 115 def new_with_default_strategy(*args, &block) new(*args, &block).tap do |model| model.strategy = nil end end |
#new_with_strategy(string_or_class_or_object, *args, &block) ⇒ Object
121 122 123 124 125 |
# File 'lib/strategic.rb', line 121 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
75 76 77 78 79 80 81 |
# File 'lib/strategic.rb', line 75 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
127 128 129 130 131 132 133 134 135 |
# File 'lib/strategic.rb', line 127 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
83 84 85 86 87 |
# File 'lib/strategic.rb', line 83 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
89 90 91 92 93 94 95 96 |
# File 'lib/strategic.rb', line 89 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
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/strategic.rb', line 98 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
55 56 57 58 59 60 61 |
# File 'lib/strategic.rb', line 55 def strategy_matcher(&matcher_block) if matcher_block.nil? @strategy_matcher else @strategy_matcher = matcher_block end end |
#strategy_matcher_for_any_strategy? ⇒ Boolean
71 72 73 |
# File 'lib/strategic.rb', line 71 def strategy_matcher_for_any_strategy? !!(strategy_matcher || strategies.any?(&:strategy_matcher)) end |
#strategy_names ⇒ Object
137 138 139 |
# File 'lib/strategic.rb', line 137 def strategy_names strategies.map(&:strategy_name) end |