Module: Strategic
- Defined in:
- lib/strategic.rb,
lib/strategic/strategy.rb
Overview
Copyright © 2020-2021 Andy Maleh
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Defined Under Namespace
Modules: ClassMethods, ExtraRailsMethods, ExtraRubyMethods, Strategy
Class Method Summary collapse
Instance Method Summary collapse
- #method_missing(method_name, *args, &block) ⇒ Object
- #reload_strategy ⇒ Object
- #respond_to?(method_name, *args, &block) ⇒ Boolean
- #strategy ⇒ Object
- #strategy=(string_or_class_or_object) ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/strategic.rb', line 156 def method_missing(method_name, *args, &block) if strategy&.respond_to?(method_name, *args, &block) strategy.send(method_name, *args, &block) else begin super rescue => e raise "No strategy is set to handle the method #{method_name} with args #{args.inspect} and block #{block.inspect} / " + e. end end end |
Class Method Details
.included(klass) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/strategic.rb', line 23 def self.included(klass) klass.extend(ClassMethods) klass.require_strategies rails_mode = klass.respond_to?(:column_names) && klass.column_names.include?('strategy_name') if rails_mode klass.include(ExtraRailsMethods) klass.after_initialize :reload_strategy else klass.include(ExtraRubyMethods) end klass.default_strategy 'default' end |
Instance Method Details
#reload_strategy ⇒ Object
152 153 154 |
# File 'lib/strategic.rb', line 152 def reload_strategy self.strategy = strategy_name end |
#respond_to?(method_name, *args, &block) ⇒ Boolean
168 169 170 |
# File 'lib/strategic.rb', line 168 def respond_to?(method_name, *args, &block) strategy&.respond_to?(method_name, *args, &block) || super end |
#strategy ⇒ Object
148 149 150 |
# File 'lib/strategic.rb', line 148 def strategy @strategy end |
#strategy=(string_or_class_or_object) ⇒ Object
143 144 145 146 |
# File 'lib/strategic.rb', line 143 def strategy=(string_or_class_or_object) strategy_class = self.class.strategy_class_for(string_or_class_or_object) self.strategy_name = strategy_class&.strategy_name end |