Class: Patriarch::DAOServices::RedisMapperService

Inherits:
Service
  • Object
show all
Defined in:
lib/patriarch/dao_services/redis_mapper_service.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#call(transaction, protagonist_type) ⇒ Hash

Returns hash with configuration needed to achieve DAO build.

Parameters:

  • transaction (Patriach::Transaction)

    the transaction being processed

  • protagonist_type (Symbol)

    the type of the protagonist whose DAO is needed

Returns:

  • (Hash)

    hash with configuration needed to achieve DAO build



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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

#progressive_present(behaviour_verb) ⇒ String

Little helper to put behaviour verbs to progressive form, relies on gem ‘Verbs’

Parameters:

  • behaviour_verb (String)

    the verb to conjugate

Returns:

  • (String)

    progressive form of behaviour_verb



96
97
98
99
# 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

#redis_bipartite_config_for_actor(target_model_name, relation_type_str) ⇒ Hash

Returns a hash containing configuration to build actor DAO used in bipartite transactions.

Parameters:

  • target_model_name (String)
  • relation_type_str (String)

Returns:

  • (Hash)

    a hash containing configuration to build actor DAO used in bipartite transactions



40
41
42
43
44
45
46
# 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

#redis_bipartite_config_for_target(actor_model_name, relation_type_str) ⇒ Hash

Returns a hash containing configuration to build target DAO used in bipartite transactions.

Parameters:

  • target_model_name (String)
  • relation_type_str (String)

Returns:

  • (Hash)

    a hash containing configuration to build target DAO used in bipartite transactions



51
52
53
54
55
56
57
# 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

#redis_tripartite_config_for_actor(target_model_name, medium_model_name, relation_type_str) ⇒ Hash

Returns a hash containing configuration to build actor DAO used in tripartite transactions.

Parameters:

  • target_model_name (String)
  • relation_type_str (String)

Returns:

  • (Hash)

    a hash containing configuration to build actor DAO used in tripartite transactions



62
63
64
65
66
67
68
# 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

#redis_tripartite_config_for_medium(actor_model_name, target_model_name, relation_type_str) ⇒ Hash

Returns a hash containing configuration to build medium DAO used in tripartite transactions.

Parameters:

  • target_model_name (String)
  • relation_type_str (String)

Returns:

  • (Hash)

    a hash containing configuration to build medium DAO used in tripartite transactions



73
74
75
76
77
78
79
# 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

#redis_tripartite_config_for_target(actor_model_name, medium_model_name, relation_type_str) ⇒ Hash

Returns a hash containing configuration to build target DAO used in tripartite transactions.

Parameters:

  • target_model_name (String)
  • relation_type_str (String)

Returns:

  • (Hash)

    a hash containing configuration to build target DAO used in tripartite transactions



84
85
86
87
88
89
90
91
# 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

#sanitize_relation_type(relation_type) ⇒ Object

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’.



104
105
106
# File 'lib/patriarch/dao_services/redis_mapper_service.rb', line 104

def sanitize_relation_type(relation_type)
  relation_type.sub(/^undo_/,'')
end