class Patriarch::DAOServices::RedisMapperService
This service is in charge of the redis mapping. When wanting to retrieve DAOs needed for a transaction, we may want to retrieve Redis DAO For each situation e.g. (in a given transaction, with a given protagonist that has an unique role), the service will return the configuration needed to achieve DAO build. Hence it enforces conventions
Public Instance Methods
@param [Patriach::Transaction]transaction the transaction being processed @param [Symbol] protagonist_type the type of the protagonist whose DAO is needed @return [Hash] hash with configuration needed to achieve DAO build
# File lib/patriarch/dao_services/redis_mapper_service.rb, line 10 def call(transaction,protagonist_type) # Getting symbols here ... relation_type = transaction.relation_type actor_type = transaction.actor_type target_type = transaction.target_type medium_type = transaction.medium_type relation_type_str = sanitize_relation_type(relation_type.to_s) if transaction.tripartite? if protagonist_type == :actor redis_tripartite_config_for_actor(target_type,medium_type,relation_type_str) elsif protagonist_type == :target redis_tripartite_config_for_target(actor_type,medium_type,relation_type_str) elsif protagonist_type == :medium redis_tripartite_config_for_medium(actor_type,target_type,relation_type_str) end else if protagonist_type == :actor redis_bipartite_config_for_actor(target_type,relation_type_str) elsif protagonist_type == :target redis_bipartite_config_for_target(actor_type,relation_type_str) end end end
Little helper to put behaviour verbs to progressive form, relies on gem 'Verbs' @param [String] behaviour_verb the verb to conjugate @return [String] progressive form of behaviour_verb
# File lib/patriarch/dao_services/redis_mapper_service.rb, line 96 def progressive_present(behaviour_verb) # like becomes => liking (Verbs::Conjugator.conjugate behaviour_verb.to_sym, :aspect => :progressive).split(/ /).last end
@param [String] target_model_name @param [String] relation_type_str @return [Hash] a hash containing configuration to build actor DAO used in bipartite transactions
# File lib/patriarch/dao_services/redis_mapper_service.rb, line 40 def redis_bipartite_config_for_actor(target_model_name,relation_type_str) # example : items_i_like ... { :type => "sorted_set", :key => "patriarch_#{target_model_name.to_s.tableize}_i_#{relation_type_str}" } end
@param [String] target_model_name @param [String] relation_type_str @return [Hash] a hash containing configuration to build target DAO used in bipartite transactions
# File lib/patriarch/dao_services/redis_mapper_service.rb, line 51 def redis_bipartite_config_for_target(actor_model_name,relation_type_str) # example : items_liking_me ... { :type => "sorted_set", :key => "patriarch_#{actor_model_name.to_s.tableize}_#{progressive_present(relation_type_str)}_me" } end
@param [String] target_model_name @param [String] relation_type_str @return [Hash] a hash containing configuration to build actor DAO used in tripartite transactions
# File lib/patriarch/dao_services/redis_mapper_service.rb, line 62 def redis_tripartite_config_for_actor(target_model_name,medium_model_name,relation_type_str) { :type => "sorted_set", :key => "patriarch_#{target_model_name.to_s.tableize}_i_#{relation_type_str}_via_#{medium_model_name.to_s.tableize}", :options => { :marshal => true } } end
@param [String] target_model_name @param [String] relation_type_str @return [Hash] a hash containing configuration to build medium DAO used in tripartite transactions
# File lib/patriarch/dao_services/redis_mapper_service.rb, line 73 def redis_tripartite_config_for_medium(actor_model_name,target_model_name,relation_type_str) { :type => "sorted_set", :key => "patriarch_#{actor_model_name.to_s.tableize}_#{progressive_present(relation_type_str)}_#{target_model_name.to_s.tableize}_via_me", :options => { :marshal => true } } end
@param [String] target_model_name @param [String] relation_type_str @return [Hash] a hash containing configuration to build target DAO used in tripartite transactions
# File lib/patriarch/dao_services/redis_mapper_service.rb, line 84 def redis_tripartite_config_for_target(actor_model_name,medium_model_name,relation_type_str) # example : items_praising_me_via_comments { :type => "sorted_set", :key => "patriarch_#{actor_model_name.to_s.tableize}_#{progressive_present(relation_type_str)}_me_via_#{medium_model_name.to_s.tableize}", :options => { :marshal => true } } end
Helper to sanitize relation type string that is equivalent to behaviour verb with a prefix to its base root. DAO and DB storage are agnostic about what kind of operation is being performed on them, hence we dont need to know if it will be an undo or a 'do'.
# File lib/patriarch/dao_services/redis_mapper_service.rb, line 104 def sanitize_relation_type(relation_type) relation_type.sub(/^undo_/,'') end