module Patriarch::Behaviours
Patriarch::Behaviours
is meant to be included into any class we want to enhance with Patriarch
handled behaviours Thus it defines the included
hook so we enhance the class in which it is included freely and add to it some class methods defined into Patriarch::Behaviours::ClassMethods
We define there the add_behaviour
method that does three things :
-
it generates an anonymous module containing tool methods defined ad-hoc in function of the arguments supplied
-
it includes another module that lives into a generated file in lib/ allowing us to override existing methods or add new ones if we wish
-
it defines instance methods for the class allowing instances to call behaviour actions
Public Class Methods
Helper function to complete the anonymous module we include later in the process in the enhanced model class @param [Module] anon_module refers to the anonymous module to be completed @param [String] behaviour the behaviour for which we complete the module with tools method @param [Array] acted_on_model_list the list of model classes that i can target with the behaviour @param [Hash] options options can serve to build custom alias for tool methods
# File lib/patriarch/behaviours.rb, line 27 def complete_custom_active_module_bipartite(anon_module,behaviour,acted_on_model_list,options={}) anon_module.class_eval do acted_on_model_list.each do |acted_on_model| # Compute names based on conventions classic_tool_method_name = "#{acted_on_model.to_s.tableize}_i_#{behaviour}" raw_tool_method_name = classic_tool_method_name + "_ids" redis_key = "patriarch_" + classic_tool_method_name # Define methods with the pattern : items_i_like that returns models and items_i_like_ids that return # Redis key has the same radical as the method in class by convention (but has a patriarch_ prefix to avoid collisions with other gems) define_method(classic_tool_method_name) do |options={}| Patriarch::ToolServices::RedisExtractorService.instance. get_models_from_ids(self,acted_on_model.to_s.classify.constantize,raw_tool_method_name,options) end if options[:as] alias_for_classic = classic_tool_method_name.sub(behaviour, options[:as].to_s) alias_method alias_for_classic, classic_tool_method_name end define_method(raw_tool_method_name) do |options={}| Patriarch::ToolServices::RedisExtractorService.instance.get_ids_from_sorted_set(self,redis_key,options) end if options[:as] alias_for_raw = raw_tool_method_name.sub(behaviour, options[:as].to_s) alias_method alias_for_raw, raw_tool_method_name end end end end
Helper function to complete the anonymous module we include later in the process in the enhanced model class @param [Module] anon_module refers to the anonymous module to be completed @param [String] behaviour the behaviour for which we complete the module with tools method @param [Array] acted_on_model_list the list of model classes that i can target with the behaviour @param [Array] via_model_list the list of model classes that the behaviour use to act through @param [Hash] options options can serve to build custom alias for tool methods
# File lib/patriarch/behaviours.rb, line 108 def complete_custom_active_module_tripartite(anon_module,behaviour,acted_on_model_list,via_model_list,options={}) anon_module.class_eval do acted_on_model_list.each do |acted_on_model| via_model_list.each do |via_model| # Compute names based on conventions # fallen_angels_i_praise_via_love_letters target_classic_tool_method_name = "#{acted_on_model.to_s.tableize}_i_#{behaviour}_via_#{via_model.to_s.tableize}" target_raw_tool_method_name = target_classic_tool_method_name + "_ids" # love_letters_i_use_to_praise_fallen_angels medium_classic_tool_method_name = "#{via_model.to_s.tableize}_i_use_to_#{behaviour}_#{acted_on_model.to_s.tableize}" medium_raw_tool_method_name = medium_classic_tool_method_name + "_ids" redis_key = "patriarch_" + target_classic_tool_method_name # Define methods with the pattern : items_i_like that returns models and items_i_like_ids that return # Redis key has the same radical as the method in class by convention (but has a patriarch_ prefix to avoid collisions with other gems) define_method(target_classic_tool_method_name) do |options={}| Patriarch::ToolServices::RedisExtractorService.instance. get_models_from_ids(self,acted_on_model.to_s.classify.constantize,target_raw_tool_method_name,options.merge({:tripartite => true})) end if options[:as] alias_for_classic = target_classic_tool_method_name.sub(behaviour, options[:as].to_s) alias_method alias_for_classic, target_classic_tool_method_name end define_method(target_raw_tool_method_name) do |options={}| Patriarch::ToolServices::RedisExtractorService.instance.get_ids_from_sorted_set(self,redis_key,options.merge({:tripartite => true, :protagonist_type => :target})) end if options[:as] alias_for_target_raw = target_raw_tool_method_name.sub(behaviour, options[:as].to_s) alias_method alias_for_target_raw, target_raw_tool_method_name end # define_method(medium_classic_tool_method_name) do |options={}| Patriarch::ToolServices::RedisExtractorService.instance. get_models_from_ids(self,via_model.to_s.classify.constantize,medium_raw_tool_method_name,options.merge({:tripartite => true})) end if options[:as] alias_for_medium_classic = medium_classic_tool_method_name.sub(behaviour, options[:as].to_s) alias_method alias_for_medium_classic, medium_classic_tool_method_name end # define_method(medium_raw_tool_method_name) do |options={}| Patriarch::ToolServices::RedisExtractorService.instance.get_ids_from_sorted_set(self,redis_key,options.merge({:tripartite => true, :protagonist_type => :medium})) end if options[:as] alias_for_medium_raw = medium_raw_tool_method_name.sub(behaviour, options[:as].to_s) alias_method alias_for_medium_raw, medium_raw_tool_method_name end end end end end
Helper function to complete the anonymous module we include later in the process in the enhanced model class @param [Module] anon_module refers to the anonymous module to be completed @param [String] behaviour the behaviour for which we complete the module with tools method @param [Array] targeted_by_model_list the list of model classes that can target me with the behaviour @param [Hash] options options can serve to build custom alias for tool methods
# File lib/patriarch/behaviours.rb, line 65 def complete_custom_passive_module_bipartite(anon_module,behaviour,targeted_by_model_list,options={}) anon_module.class_eval do targeted_by_model_list.each do |targeted_by_model| # Compute names based on conventions progressive_present_relation_type = (Verbs::Conjugator.conjugate behaviour.to_sym, :aspect => :progressive).split(/ /).last classic_tool_method_name = "#{targeted_by_model.to_s.tableize}_#{progressive_present_relation_type}_me" raw_tool_method_name = classic_tool_method_name + "_ids" redis_key = "patriarch_" + classic_tool_method_name # Define methods with the pattern : items_i_like that returns models and items_i_like_ids that return # Redis key has the same radical as the method in class by convention (but has a patriarch_ prefix to avoid collisions with other gems) define_method(classic_tool_method_name) do |options={}| Patriarch::ToolServices::RedisExtractorService.instance. get_models_from_ids(self,targeted_by_model.to_s.classify.constantize,raw_tool_method_name,options) end if options[:as] progressive_present_relation_type_alias = (Verbs::Conjugator.conjugate options[:as], :aspect => :progressive).split(/ /).last alias_for_classic = classic_tool_method_name.sub(progressive_present_relation_type,progressive_present_relation_type_alias) alias_method alias_for_classic, classic_tool_method_name end define_method(raw_tool_method_name) do |options={}| Patriarch::ToolServices::RedisExtractorService.instance.get_ids_from_sorted_set(self,redis_key,options) end if options[:as] progressive_present_relation_type_alias = (Verbs::Conjugator.conjugate options[:as], :aspect => :progressive).split(/ /).last alias_for_raw = raw_tool_method_name.sub(progressive_present_relation_type,progressive_present_relation_type_alias) alias_method alias_for_raw, raw_tool_method_name end end end end
Helper function to complete the anonymous module we include later in the process in the enhanced model class @param [Module] anon_module refers to the anonymous module to be completed @param [String] behaviour the behaviour for which we complete the module with tools method @param [Array] targeted_by_model_list the list of model classes that can target me with the behaviour @param [Array] via_model_list the list of model classes that the behaviour use to act through @param [Hash] options options can serve to build custom alias for tool methods
# File lib/patriarch/behaviours.rb, line 175 def complete_custom_passive_module_tripartite(anon_module,behaviour,targeted_by_model_list,via_model_list,options={}) anon_module.class_eval do targeted_by_model_list.each do |targeted_by_model| via_model_list.each do |via_model| # Compute names based on conventions progressive_present_relation_type = (Verbs::Conjugator.conjugate behaviour.to_sym, :aspect => :progressive).split(/ /).last actor_classic_tool_method_name = "#{targeted_by_model.to_s.tableize}_#{progressive_present_relation_type}_me_via_#{via_model.to_s.tableize}" actor_raw_tool_method_name = actor_classic_tool_method_name + "_ids" #"love_letters_used_by_monsters_to_praise_me" medium_classic_tool_method_name = "#{via_model.to_s.tableize}_used_by_#{targeted_by_model.to_s.tableize}_to_#{behaviour}_me" medium_raw_tool_method_name = medium_classic_tool_method_name + "_ids" redis_key = "patriarch_" + actor_classic_tool_method_name define_method(actor_classic_tool_method_name) do |options={}| Patriarch::ToolServices::RedisExtractorService.instance. get_models_from_ids(self,targeted_by_model.to_s.classify.constantize,actor_raw_tool_method_name,options.merge({:tripartite => true})) end if options[:as] progressive_present_relation_type_alias = (Verbs::Conjugator.conjugate options[:as], :aspect => :progressive).split(/ /).last alias_for_actor_classic = actor_classic_tool_method_name.sub(progressive_present_relation_type,progressive_present_relation_type_alias) alias_method alias_for_actor_classic, actor_classic_tool_method_name end define_method(actor_raw_tool_method_name) do |options={}| Patriarch::ToolServices::RedisExtractorService.instance.get_ids_from_sorted_set(self,redis_key,options.merge({:tripartite => true , :protagonist_type => :actor})) end if options[:as] progressive_present_relation_type_alias = (Verbs::Conjugator.conjugate options[:as], :aspect => :progressive).split(/ /).last alias_for_actor_raw = actor_raw_tool_method_name.sub(progressive_present_relation_type,progressive_present_relation_type_alias) alias_method alias_for_actor_raw, actor_raw_tool_method_name end define_method(medium_classic_tool_method_name) do |options={}| Patriarch::ToolServices::RedisExtractorService.instance. get_models_from_ids(self,via_model.to_s.classify.constantize,medium_raw_tool_method_name,options.merge({:tripartite => true})) end #TODO add alias there # FIXME id in it is wrong ... define_method(medium_raw_tool_method_name) do |options={}| Patriarch::ToolServices::RedisExtractorService.instance.get_ids_from_sorted_set(self,redis_key,options.merge({:tripartite => true , :protagonist_type => :medium})) end end end end end