Class: Patriarch::ToolServices::RedisCleanerService

Inherits:
Service
  • Object
show all
Defined in:
lib/patriarch/tool_services/redis_cleaner_service.rb

Overview

Adding behaviours to classes through Patriarch induces writes in Redis Database What happens when we want to destroy an item is that it fires ActiveRecord::Callbacks to deal with this kind of stuff. We build a transaction item whose execute would clean redis stuff but do nothing until commit happens in case SQL aborts. This service takes care of building clean items

Instance Method Summary collapse

Instance Method Details

#clean_all(calling_entity, options = {}) ⇒ Patriarch::Transaction

Returns a transaction item that is either built from scratch or was passed through options and has been completed

Parameters:

  • calling_entity (Object)

    the entity calling the clean upon itself

  • options (Object) (defaults to: {})

    various options we may want to pass, used here to transmit a transaction item if a clean transaction wraps all the clean

Returns:

  • (Patriarch::Transaction)

    returns a transaction item that is either built from scratch or was passed through options and has been completed



11
12
13
14
15
16
17
18
19
20
# File 'lib/patriarch/tool_services/redis_cleaner_service.rb', line 11

def clean_all(calling_entity,options={})
  transac = options[:transac] || Patriarch::TransactionServices::TransactionManagerService.instance.new_transaction("clean_all".to_sym)
  options = { :transac => transac }

  calling_entity.class.patriarch_behaviours.keys.each do |behaviour|
    transac.steps << clean_behaviour(calling_entity,behaviour)
  end

  transac
end

#clean_behaviour(calling_entity, behaviour, options = {}) ⇒ Patriarch::Transaction

behaviours so it runs once and not multiple transactions.

Parameters:

  • calling_entity (Object)

    the entity calling the clean upon itself

  • behaviour (Object)

    the behaviour the entity wants to clean

  • options (Object) (defaults to: {})

    various options we may want to pass, used here to transmit a transaction item if a clean transaction wraps all the clean

Returns:

  • (Patriarch::Transaction)

    returns a transaction item that is either built from scratch or was passed through options and has been completed



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/patriarch/tool_services/redis_cleaner_service.rb', line 27

def clean_behaviour(calling_entity,behaviour,options={})
  my_behaviours = calling_entity.class.patriarch_behaviours
  behaviour_symbol = behaviour.to_s.underscore.to_sym

  if my_behaviours.include? behaviour_symbol
    if my_behaviours[behaviour_symbol].first.size == 3
      clean_tripartite_behaviour(calling_entity,behaviour,options)
    elsif my_behaviours[behaviour_symbol].first.size == 2
      clean_bipartite_behaviour(calling_entity,behaviour,options)
    else
      raise "Bad Behaviour declaration occured, no doublet or triplet are registered"
    end
  end
end

#clean_bipartite_behaviour(calling_entity, behaviour, options = {}) ⇒ Object

:nodoc:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/patriarch/tool_services/redis_cleaner_service.rb', line 61

def clean_bipartite_behaviour(calling_entity,behaviour,options={})
  my_class = calling_entity.class
  bipartite_behaviour = my_class.patriarch_behaviours[behaviour.to_s.underscore.to_sym]

  transac = options[:transac] || Patriarch::TransactionServices::TransactionManagerService.instance.new_transaction("clean_#{behaviour.to_s}".to_sym)
  options = { :transac => transac, :stop_execution => true}

  bipartite_behaviour.each do |protagonists|
    clean_bipartite_behaviour_for_actor(calling_entity,behaviour,protagonists,options)  if protagonists.first  == my_class
    clean_bipartite_behaviour_for_target(calling_entity,behaviour,protagonists,options) if protagonists.second == my_class
  end  

  transac
end

#clean_tripartite_behaviour(calling_entity, behaviour, options = {}) ⇒ Object

:nodoc:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/patriarch/tool_services/redis_cleaner_service.rb', line 44

def clean_tripartite_behaviour(calling_entity,behaviour,options={})
  my_class = calling_entity.class
  tripartite_behaviour = my_class.patriarch_behaviours[behaviour.to_s.underscore.to_sym]

  transac = options[:transac] || Patriarch::TransactionServices::TransactionManagerService.instance.new_transaction("clean_#{behaviour.to_s}".to_sym)
  options = { :transac => transac, :stop_execution => true}
    
  tripartite_behaviour.each do |protagonists|
    clean_tripartite_behaviour_for_actor(calling_entity,behaviour,protagonists,options)  if protagonists.first  == my_class
    clean_tripartite_behaviour_for_target(calling_entity,behaviour,protagonists,options) if protagonists.second == my_class
    clean_tripartite_behaviour_for_medium(calling_entity,behaviour,protagonists,options) if protagonists.third  == my_class
  end      

  transac
end