module Strategic
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.
Public Class Methods
# 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 end
Private Class Methods
# File lib/strategic.rb, line 167 def self.classify(text) text.split("_").map {|word| "#{word[0].upcase}#{word[1..-1]}"}.join end
# File lib/strategic.rb, line 171 def self.underscore(text) text.chars.reduce('') {|output,c| !output.empty? && c.match(/[A-Z]/) ? output + '_' + c : output + c}.downcase end
Public Instance Methods
# File lib/strategic.rb, line 149 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.message end end end
# File lib/strategic.rb, line 145 def reload_strategy self.strategy = strategy_name end
# File lib/strategic.rb, line 161 def respond_to?(method_name, *args, &block) strategy&.respond_to?(method_name, *args, &block) || super end
# File lib/strategic.rb, line 141 def strategy @strategy end
# File lib/strategic.rb, line 136 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