patriarch 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -1,5 +1,8 @@
1
+ spec/dummy/lib
1
2
  *.gem
2
3
  *.rbc
4
+ *.swp
5
+ *.swo
3
6
  .bundle
4
7
  .config
5
8
  .yardoc
@@ -1,69 +1,69 @@
1
- class PatriarchGenerator < Rails::Generators::Base
2
- source_root File.expand_path('../templates', __FILE__)
3
-
4
- desc "Generate files needed to implement the BEHAVIOUR you specified. Don't forget to add declarations into models"
1
+ require "rails/generators"
5
2
 
6
- argument :behaviour, :type => :string
3
+ class PatriarchGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('../templates', __FILE__)
5
+
6
+ desc "Generate files needed to implement the BEHAVIOUR you specified. Don't forget to add declarations into models"
7
7
 
8
- public
9
- def init_directories
10
- empty_directory "lib/patriarch/services/#{behaviour.underscore.downcase}"
11
- empty_directory "lib/patriarch/services/#{undo(behaviour.underscore.downcase)}"
12
- template "empty_behaviour_module.rb", "lib/patriarch/services/#{behaviour.underscore.downcase}.rb"
13
- template "tools_methods.rb", "lib/patriarch/behaviours/#{behaviour.underscore.downcase}/tools_methods.rb"
14
- end
8
+ argument :behaviour, :type => :string
15
9
 
16
- def generate_services
17
- create_services(behaviour)
18
- # implémenter un switch ici, plus zoli ...
19
- self.class.send(:define_method,:class_name) do
20
- undo(behaviour).classify
10
+ public
11
+ def init_directories
12
+ empty_directory "lib/patriarch/services/#{behaviour.underscore.downcase}"
13
+ empty_directory "lib/patriarch/services/#{Patriarch.undo(behaviour.underscore.downcase)}"
14
+ template "empty_behaviour_module.rb", "lib/patriarch/behaviours/#{behaviour.underscore.downcase}.rb"
15
+ template "empty_service_module.rb", "lib/patriarch/services/#{behaviour.underscore.downcase}.rb"
16
+ template "tools_methods.rb", "lib/patriarch/behaviours/#{behaviour.underscore.downcase}/tools_methods.rb"
21
17
  end
22
- create_undo_services(behaviour)
23
- end
24
18
 
25
- private
26
- def class_name
27
- behaviour.classify
19
+ def generate_services
20
+ create_services(behaviour)
21
+ # implémenter un switch ici, plus zoli ...
22
+ self.class.send(:define_method,:class_name) do
23
+ Patriarch.undo(behaviour).classify
24
+ end
25
+ create_undo_services(behaviour)
28
26
  end
29
27
 
30
- def create_services(behaviour)
31
- behaviour_str = behaviour.underscore.downcase
32
- template "authorization_service.rb", "lib/patriarch/services/#{behaviour_str}/authorization_service.rb"
33
-
34
- template "before_manager_service.rb", "lib/patriarch/services/#{behaviour_str}/before_manager_service.rb"
35
- template "before_service.rb", "lib/patriarch/services/#{behaviour_str}/before_service.rb"
28
+ private
29
+ def class_name
30
+ behaviour.classify
31
+ end
36
32
 
37
- template "manager_service.rb", "lib/patriarch/services/#{behaviour_str}/manager_service.rb"
38
-
39
- template "service.rb", "lib/patriarch/services/#{behaviour_str}/service.rb"
33
+ def undo_class_name
34
+ Patriarch.undo(behaviour).classify
35
+ end
40
36
 
37
+ def create_services(behaviour)
38
+ behaviour_str = behaviour.underscore.downcase
39
+ template "authorization_service.rb", "lib/patriarch/services/#{behaviour_str}/authorization_service.rb"
40
+
41
+ template "before_manager_service.rb", "lib/patriarch/services/#{behaviour_str}/before_manager_service.rb"
42
+ template "before_service.rb", "lib/patriarch/services/#{behaviour_str}/before_service.rb"
41
43
 
42
- template "after_manager_service.rb", "lib/patriarch/services/#{behaviour_str}/after_manager_service.rb"
43
- template "after_service.rb", "lib/patriarch/services/#{behaviour_str}/after_service.rb"
44
- end
44
+ template "manager_service.rb", "lib/patriarch/services/#{behaviour_str}/manager_service.rb"
45
+
46
+ template "service.rb", "lib/patriarch/services/#{behaviour_str}/service.rb"
45
47
 
46
- def create_undo_services(behaviour)
47
- undo_behaviour_str = undo(behaviour).underscore.downcase
48
48
 
49
- template "authorization_service.rb", "lib/patriarch/services/#{undo_behaviour_str}/authorization_service.rb"
50
-
51
- template "before_manager_service.rb", "lib/patriarch/services/#{undo_behaviour_str}/before_manager_service.rb"
52
- template "before_service.rb", "lib/patriarch/services/#{undo_behaviour_str}/before_service.rb"
49
+ template "after_manager_service.rb", "lib/patriarch/services/#{behaviour_str}/after_manager_service.rb"
50
+ template "after_service.rb", "lib/patriarch/services/#{behaviour_str}/after_service.rb"
51
+ end
53
52
 
54
- template "manager_service.rb", "lib/patriarch/services/#{undo_behaviour_str}/manager_service.rb"
55
-
56
- template "undo_service.rb", "lib/patriarch/services/#{undo_behaviour_str}/service.rb"
53
+ def create_undo_services(behaviour)
54
+ undo_behaviour_str = Patriarch.undo(behaviour).underscore.downcase
57
55
 
58
- template "after_manager_service.rb", "lib/patriarch/services/#{undo_behaviour_str}/after_manager_service.rb"
59
- template "after_service.rb", "lib/patriarch/services/#{undo_behaviour_str}/after_service.rb"
60
- end
56
+ template "authorization_service.rb", "lib/patriarch/services/#{undo_behaviour_str}/authorization_service.rb"
57
+
58
+ template "before_manager_service.rb", "lib/patriarch/services/#{undo_behaviour_str}/before_manager_service.rb"
59
+ template "before_service.rb", "lib/patriarch/services/#{undo_behaviour_str}/before_service.rb"
61
60
 
61
+ template "manager_service.rb", "lib/patriarch/services/#{undo_behaviour_str}/manager_service.rb"
62
+
63
+ template "undo_service.rb", "lib/patriarch/services/#{undo_behaviour_str}/service.rb"
62
64
 
63
- def undo(str)
64
- undo = "undo_"
65
- undo << str
66
- undo
67
- end
65
+ template "after_manager_service.rb", "lib/patriarch/services/#{undo_behaviour_str}/after_manager_service.rb"
66
+ template "after_service.rb", "lib/patriarch/services/#{undo_behaviour_str}/after_service.rb"
67
+ end
68
68
 
69
- end
69
+ end # PatriarchGenerator
@@ -1,2 +1,7 @@
1
- module Patriarch::Services::<%= class_name %>
1
+ module Patriarch
2
+ module Behaviours
3
+ module <%= class_name %>
4
+ autoload :ToolsMethods, 'patriarch/behaviours/<%= class_name.underscore %>/tools_methods.rb'
5
+ end
6
+ end
2
7
  end
@@ -0,0 +1,24 @@
1
+ module Patriarch
2
+ module Services
3
+ module <%= class_name %>
4
+ autoload :AfterManagerService, 'patriarch/services/<%= class_name.underscore %>/after_manager_service.rb'
5
+ autoload :AfterService, 'patriarch/services/<%= class_name.underscore %>/after_service.rb'
6
+ autoload :AuthorizationService, 'patriarch/services/<%= class_name.underscore %>/authorization_service.rb'
7
+ autoload :BeforeManagerService, 'patriarch/services/<%= class_name.underscore %>/before_manager_service.rb'
8
+ autoload :BeforeService, 'patriarch/services/<%= class_name.underscore %>/before_service.rb'
9
+ autoload :ManagerService, 'patriarch/services/<%= class_name.underscore %>/manager_service.rb'
10
+ autoload :Service, 'patriarch/services/<%= class_name.underscore %>/service.rb'
11
+ end
12
+
13
+ module <%= undo_class_name %>
14
+ autoload :AfterManagerService, 'patriarch/services/<%= undo_class_name.underscore %>/after_manager_service.rb'
15
+ autoload :AfterService, 'patriarch/services/<%= undo_class_name.underscore %>/after_service.rb'
16
+ autoload :AuthorizationService, 'patriarch/services/<%= undo_class_name.underscore %>/authorization_service.rb'
17
+ autoload :BeforeManagerService, 'patriarch/services/<%= undo_class_name.underscore %>/before_manager_service.rb'
18
+ autoload :BeforeService, 'patriarch/services/<%= undo_class_name.underscore %>/before_service.rb'
19
+ autoload :ManagerService, 'patriarch/services/<%= undo_class_name.underscore %>/manager_service.rb'
20
+ autoload :Service, 'patriarch/services/<%= undo_class_name.underscore %>/service.rb'
21
+ end
22
+ end
23
+ end
24
+
@@ -1,10 +1,12 @@
1
1
  require 'singleton'
2
2
 
3
- class Patriarch::AuthorizationService
4
- include Singleton
3
+ module Patriarch
4
+ class AuthorizationService
5
+ include Singleton
5
6
 
6
- # override if necessary
7
- def grant?(*args)
8
- true
7
+ # override if necessary
8
+ def grant?(*args)
9
+ true
10
+ end
9
11
  end
10
12
  end
@@ -1,7 +1,5 @@
1
- #require 'patriarch/behaviours/own'
2
- #require 'patriarch/behaviours/subscribe'
3
- #require 'patriarch/behaviours/tagging'
4
- #require 'patriarch/behaviours/join'
1
+ require 'verbs'
2
+ require 'patriarch'
5
3
 
6
4
  module Patriarch
7
5
  module Behaviours
@@ -61,12 +59,6 @@ module Patriarch
61
59
  end
62
60
  end
63
61
 
64
- def undo(str)
65
- undo = "undo_"
66
- undo << str
67
- undo
68
- end
69
-
70
62
  def included(klass)
71
63
  klass.extend ClassMethods
72
64
  klass.extend ActiveModel::Callbacks
@@ -77,8 +69,6 @@ module Patriarch
77
69
  end
78
70
 
79
71
  module ClassMethods
80
- def add_behaviours(*behaviours)
81
- end
82
72
 
83
73
  def add_behaviour(behaviour,options)
84
74
  # add_behaviour :like, :by => bla, :on => [:item,:user] || :community, :as => :aimer, :reverse_as => :haïr ...
@@ -102,7 +92,7 @@ module Patriarch
102
92
  # TODO
103
93
  def self.included(klass)
104
94
  klass.extend ActiveModel::Callbacks
105
- klass.send(:define_model_callbacks, self.const_get(:Behaviour).to_sym, Patriarch::Behaviours.undo(self.const_get(:Behaviour)).to_sym)
95
+ klass.send(:define_model_callbacks, self.const_get(:Behaviour).to_sym, Patriarch.undo(self.const_get(:Behaviour)).to_sym)
106
96
  end
107
97
 
108
98
  # like
@@ -113,9 +103,9 @@ module Patriarch
113
103
  end
114
104
 
115
105
  # undo_like
116
- define_method(Patriarch::Behaviours.undo(methods_mod.const_get(:Behaviour)).to_sym) do |entity,options={}|
117
- run_callbacks Patriarch::Behaviours.undo(methods_mod.const_get(:Behaviour)).to_sym do
118
- "Patriarch::Services::#{(Patriarch::Behaviours.undo(methods_mod.const_get(:Behaviour))).classify}::ManagerService".constantize.instance.resolve(self,entity)
106
+ define_method(Patriarch.undo(methods_mod.const_get(:Behaviour)).to_sym) do |entity,options={}|
107
+ run_callbacks Patriarch.undo(methods_mod.const_get(:Behaviour)).to_sym do
108
+ "Patriarch::Services::#{(Patriarch.undo(methods_mod.const_get(:Behaviour))).classify}::ManagerService".constantize.instance.resolve(self,entity)
119
109
  end
120
110
  end
121
111
 
@@ -25,10 +25,10 @@ class Patriarch::DAOServices::RetrieverService < Patriarch::Service
25
25
  actor_id = transaction_item.actor_id
26
26
  actor_type = transaction_item.actor_type
27
27
 
28
- if Redis.constants.map(&:to_s).include?("#{dao_info[:type].camelize}")
28
+ if Redis::Objects.constants.map(&:to_s).include?("#{dao_info[:type].pluralize.camelize}")
29
29
  redis_dao_class = "Redis::#{dao_info[:type].camelize}".constantize
30
30
  else
31
- raise InvalidRedisTypeException
31
+ raise InvalidRedisTypeException, "Redis constants available were #{Redis::Objects.constants} and supplied type was #{dao_info[:type]}"
32
32
  end
33
33
 
34
34
  redis_dao_class.new("#{actor_type}:#{actor_id}:#{dao_info[:key]}")
@@ -40,10 +40,10 @@ class Patriarch::DAOServices::RetrieverService < Patriarch::Service
40
40
  target_id = transaction_item.target_id
41
41
  target_type = transaction_item.target_type
42
42
 
43
- if Redis.constants.map(&:to_s).include?("#{dao_info[:type].camelize}")
43
+ if Redis::Objects.constants.map(&:to_s).include?("#{dao_info[:type].pluralize.camelize}")
44
44
  redis_dao_class = "Redis::#{dao_info[:type].camelize}".constantize
45
45
  else
46
- raise InvalidRedisTypeException
46
+ raise InvalidRedisTypeException, "Redis constants available were #{Redis::Objects.constants} and supplied type was #{dao_info[:type]}"
47
47
  end
48
48
  redis_dao_class.new("#{target_type}:#{target_id}:#{dao_info[:key]}")
49
49
  end
@@ -1,15 +1,16 @@
1
1
  require 'singleton'
2
2
 
3
- # Interface avec les couches les plus hautes
4
- class Patriarch::ManagerService
5
- include Singleton
3
+ module Patriarch
4
+ class ManagerService
5
+ include Singleton
6
6
 
7
- # /!\ Changer l'architecture ici, l'idée est là
8
- #def resolve(*args, options={})
9
- # Traitement des options
10
- # ...
7
+ # /!\ Changer l'architecture ici, l'idée est là
8
+ #def resolve(*args, options={})
9
+ # Traitement des options
10
+ # ...
11
11
 
12
- # Traitement normal
13
- # after_resolve(*args)
14
- #end
15
- end
12
+ # Traitement normal
13
+ # after_resolve(*args)
14
+ #end
15
+ end
16
+ end
@@ -1,5 +1,5 @@
1
1
  require 'singleton'
2
2
 
3
- class Judge::Service
3
+ class Patriarch::Service
4
4
  include Singleton
5
5
  end
@@ -1,68 +1,70 @@
1
- class Patriarch::Transaction
2
- attr_reader :id, :steps, :current_step_number
3
- attr_reader :type
1
+ module Patriarch
2
+ class Transaction
3
+ attr_reader :id, :steps, :current_step_number
4
+ attr_reader :type
4
5
 
5
- forwarded_methods_syms = [:relation_type,:actor_type,:target_type,:actor_id,:target_id,
6
- :context,:actor,:target]
6
+ forwarded_methods_syms = [:relation_type,:actor_type,:target_type,:actor_id,:target_id,
7
+ :context,:actor,:target]
7
8
 
8
- # Forward methods that are not this transaction's job to step object
9
- forwarded_methods_syms.each do |method_sym|
10
- define_method(method_sym) do
11
- current_step.send(method_sym)
9
+ # Forward methods that are not this transaction's job to step object
10
+ forwarded_methods_syms.each do |method_sym|
11
+ define_method(method_sym) do
12
+ current_step.send(method_sym)
13
+ end
12
14
  end
13
- end
14
15
 
15
16
 
16
- # id => go $redis.incr
17
- def initialize(type,id) #relation_type,actor,target,
18
- @type = type
19
- @id = id
20
- @steps = []
21
- @current_step_number = 0
22
- end
17
+ # id => go $redis.incr
18
+ def initialize(type,id) #relation_type,actor,target,
19
+ @type = type
20
+ @id = id
21
+ @steps = []
22
+ @current_step_number = 0
23
+ end
23
24
 
24
- # Initializes a new step and stores it in the steps array right away
25
- # A step matches a "like", "follow", "own", etc.
26
- # Register that we went a step further into the transaction processus
27
- # Medium is nil by default (bipartite transactions does not require more than target/actor)
28
- def add_step(relation_type,actor,target,medium=nil)
29
25
  # Initializes a new step and stores it in the steps array right away
30
- new_step = Patriarch::TransactionStep.new(relation_type,actor,target,medium)
31
-
32
- # if initilization failed we should not move forward ...
33
- raise PatriarchTransactionStepInstanciationException unless new_step
34
-
26
+ # A step matches a "like", "follow", "own", etc.
35
27
  # Register that we went a step further into the transaction processus
36
- @steps << new_step
37
- @current_step_number += 1
38
- end
28
+ # Medium is nil by default (bipartite transactions does not require more than target/actor)
29
+ def add_step(relation_type,actor,target,medium=nil)
30
+ # Initializes a new step and stores it in the steps array right away
31
+ new_step = Patriarch::TransactionStep.new(relation_type,actor,target,medium)
32
+
33
+ # if initilization failed we should not move forward ...
34
+ raise PatriarchTransactionStepInstanciationException unless new_step
39
35
 
40
- # Executes the calls to redis in one block here.
41
- def execute
42
- $redis.multi do
43
- steps.each do |step|
44
- step.execute
36
+ # Register that we went a step further into the transaction processus
37
+ @steps << new_step
38
+ @current_step_number += 1
39
+ end
40
+
41
+ # Executes the calls to redis in one block here.
42
+ def execute
43
+ $redis.multi do
44
+ steps.each do |step|
45
+ step.execute
46
+ end
45
47
  end
46
48
  end
47
- end
48
49
 
49
- def transaction_queue
50
- transaction_queue = []
51
- steps.each do |step|
52
- transaction_queue.concat(step.queue)
50
+ def transaction_queue
51
+ transaction_queue = []
52
+ steps.each do |step|
53
+ transaction_queue.concat(step.queue)
54
+ end
55
+ transaction_queue
53
56
  end
54
- transaction_queue
55
- end
56
57
 
57
- def add_to_queue(proc)
58
- current_step.add_to_queue(proc)
59
- end
58
+ def add_to_queue(proc)
59
+ current_step.add_to_queue(proc)
60
+ end
60
61
 
61
- alias :queue :transaction_queue
62
+ alias :queue :transaction_queue
62
63
 
63
- protected
64
+ protected
64
65
 
65
- def current_step
66
- steps[current_step_number-1]
66
+ def current_step
67
+ steps[current_step_number-1]
68
+ end
67
69
  end
68
- end
70
+ end
@@ -1,39 +1,41 @@
1
- class Patriarch::TransactionStep
2
- attr_accessor :context, :queue
1
+ module Patriarch
2
+ class TransactionStep
3
+ attr_accessor :context, :queue
3
4
 
4
- def initialize(relation_type,actor,target,medium)
5
- @context = {
6
- :relation_type => relation_type,
7
- :actor_type => actor.class.name.underscore.to_sym,
8
- :target_type => target.class.name.underscore.to_sym,
9
- :actor_id => actor.id,
10
- :target_id => target.id,
11
- }
12
- @queue = []
13
- end
5
+ def initialize(relation_type,actor,target,medium)
6
+ @context = {
7
+ :relation_type => relation_type,
8
+ :actor_type => actor.class.name.underscore.to_sym,
9
+ :target_type => target.class.name.underscore.to_sym,
10
+ :actor_id => actor.id,
11
+ :target_id => target.id,
12
+ }
13
+ @queue = []
14
+ end
14
15
 
15
- # defines access methods to context fields letting it be encapsulated nicely
16
- [:relation_type,:actor_type,:target_type,:actor_id,:target_id].each do |context_key|
17
- define_method(context_key) do
18
- context[context_key]
16
+ # defines access methods to context fields letting it be encapsulated nicely
17
+ [:relation_type,:actor_type,:target_type,:actor_id,:target_id].each do |context_key|
18
+ define_method(context_key) do
19
+ context[context_key]
20
+ end
19
21
  end
20
- end
21
22
 
22
- def actor
23
- actor_type.to_s.camelize.constantize.find actor_id
24
- end
23
+ def actor
24
+ actor_type.to_s.camelize.constantize.find actor_id
25
+ end
25
26
 
26
- def target
27
- target_type.to_s.camelize.constantize.find target_id
28
- end
27
+ def target
28
+ target_type.to_s.camelize.constantize.find target_id
29
+ end
29
30
 
30
- def execute
31
- queue.each do |redis_instruction|
32
- redis_instruction.call
31
+ def execute
32
+ queue.each do |redis_instruction|
33
+ redis_instruction.call
34
+ end
33
35
  end
34
- end
35
36
 
36
- def add_to_queue(proc)
37
- queue << proc
37
+ def add_to_queue(proc)
38
+ queue << proc
39
+ end
38
40
  end
39
- end
41
+ end
@@ -1,3 +1,3 @@
1
1
  module Patriarch
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/patriarch.rb CHANGED
@@ -1,5 +1,38 @@
1
1
  require "patriarch/version"
2
+ require "redis"
3
+ require "redis/objects"
4
+ require "rails/generators"
5
+
6
+
7
+ require File.expand_path('../generators/patriarch/patriarch_generator',__FILE__)
2
8
 
3
9
  module Patriarch
4
- # Your code goes here...
5
- end
10
+ autoload :Service, 'patriarch/service'
11
+ autoload :Transaction, 'patriarch/transaction'
12
+ autoload :TransactionStep, 'patriarch/transaction_step'
13
+ autoload :AuthorizationService, 'patriarch/authorization_service'
14
+ autoload :ManagerService, 'patriarch/manager_service'
15
+ autoload :Behaviours, 'patriarch/behaviours'
16
+
17
+ module DAOServices
18
+ autoload :BipartiteRelationshipBuilderService, 'patriarch/dao_services/bipartite_relationship_builder_service'
19
+ autoload :RedisMapperService, 'patriarch/dao_services/redis_mapper_service'
20
+ autoload :RetrieverService, 'patriarch/dao_services/retriever_service'
21
+ end
22
+
23
+ module ToolServices
24
+ autoload :RedisCleaner, 'patriarch/tool_services/redis_cleaner'
25
+ autoload :RedisExtractorService, 'patriarch/tool_services/redis_extractor_service'
26
+ end
27
+
28
+ module TransactionServices
29
+ autoload :TransactionManagerService, 'patriarch/transaction_services/transaction_manager_service'
30
+ end
31
+
32
+ def self.undo(str)
33
+ undo = "undo_"
34
+ undo << str
35
+ undo
36
+ end
37
+
38
+ end # Patriarch
data/patriarch.gemspec CHANGED
@@ -10,10 +10,22 @@ Gem::Specification.new do |spec|
10
10
 
11
11
  spec.platform = Gem::Platform::RUBY
12
12
  spec.licenses = ['MIT']
13
-
13
+ spec.post_install_message = "Thanks for installing!"
14
+
15
+ # Main dependencies
16
+ spec.add_dependency "redis-objects"
17
+ spec.add_runtime_dependency "verbs"
18
+ spec.add_runtime_dependency "rails"
19
+ spec.add_runtime_dependency "railties"
20
+ spec.add_runtime_dependency "redis-objects"
21
+
22
+ # Dependencies needed for testing
14
23
  spec.add_development_dependency "rspec"
15
- spec.add_runtime_dependency "redis-objects"
24
+ spec.add_development_dependency "fakeredis"
25
+ spec.add_development_dependency "autotest-rails"
26
+ spec.add_development_dependency "ZenTest"
16
27
 
28
+ # Other options ...
17
29
  spec.files = `git ls-files`.split($\)
18
30
  spec.executables = spec.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
19
31
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
@@ -1,72 +1,59 @@
1
1
  require 'spec_helper'
2
2
 
3
-
4
3
  describe Patriarch::Behaviours do
5
4
 
6
- # setup with empty fake klasses here
7
-
8
- class ModelClass;
9
- include Patriarch::Behaviours
10
- end
11
-
12
- class ModelKlass;
13
- include Patriarch::Behaviours
14
- end
15
-
16
- ModelClass.add_behaviour :like, :on => [:modelKlass]
17
- ModelKlass.add_behaviour :like, :by => [:modelClass]
5
+ Monster.add_behaviour :like, :on => [:fallenAngel]
6
+ FallenAngel.add_behaviour :like, :by => [:monster]
18
7
 
19
8
 
20
9
  context "when adding a behaviour" do
10
+ before(:all) do
11
+ @monster = new_monster
12
+ @fallen_angel = new_fallen_angel
13
+ end
14
+
21
15
  it "should implement basic tool methods" do
22
16
  progressive_like = (Verbs::Conjugator.conjugate :like, :aspect => :progressive).split(/ /).last
23
- ModelClass.new.should respond_to("#{"model_klass".pluralize}_i_like")
24
- ModelClass.new.should respond_to("#{"model_klass".pluralize}_i_like_ids")
25
- ModelKlass.new.should respond_to("#{"model_class".pluralize}_#{progressive_like}_me")
26
- ModelKlass.new.should respond_to("#{"model_class".pluralize}_#{progressive_like}_me_ids")
17
+ @monster.should respond_to("#{"fallen_angel".pluralize}_i_like")
18
+ @monster.should respond_to("#{"fallen_angel".pluralize}_i_like_ids")
19
+ @fallen_angel.should respond_to("#{"monster".pluralize}_#{progressive_like}_me")
20
+ @fallen_angel.should respond_to("#{"monster".pluralize}_#{progressive_like}_me_ids")
27
21
  end
28
22
 
29
23
  it "should implement actions only on model being added behaviours with ':on' options" do
30
- mc = ModelClass.new
31
- mk = ModelKlass.new
32
- mc.should respond_to(:like)
33
- mc.should respond_to(:undo_like)
34
- mk.should_not respond_to(:like)
35
- mk.should_not respond_to(:undo_like)
24
+ @monster.should respond_to(:like)
25
+ @monster.should respond_to(:undo_like)
26
+ @fallen_angel.should_not respond_to(:like)
27
+ @fallen_angel.should_not respond_to(:undo_like)
36
28
  end
37
- end
29
+ end # context "when adding a behaviour"
38
30
 
39
31
  context "when using an added behaviour" do
40
- User.add_behaviour :smoke, :on => [:User]
41
- User.add_behaviour :smoke, :by => [:User]
42
-
43
32
  before(:all) do
44
- @userA = create_user
45
- @userB = create_user
46
- @userA.smoke @userB
33
+ @monster = new_monster
34
+ @fallen_angel = new_fallen_angel
35
+ @monster.like @fallen_angel
47
36
  end
48
37
 
49
38
  it "shall play the transaction nicely" do
50
- @userA.users_i_smoke.should == [@userB]
51
- @userB.users_smoking_me.should == [@userA]
39
+ @monster.fallen_angels_i_like.should == [@fallen_angel]
40
+ @fallen_angel.monsters_liking_me.should == [@monster]
52
41
 
53
- @userA.users_i_smoke_ids.should == [@userB.id]
54
- @userB.users_smoking_me_ids.should == [@userA.id]
42
+ @monster.fallen_angels_i_like_ids.should == [@fallen_angel.id]
43
+ @fallen_angel.monsters_liking_me_ids.should == [@monster.id]
55
44
  end
56
45
 
57
46
  it "shall play reverse transaction nicely" do
58
- @userA.undo_smoke @userB
59
- @userA.users_i_smoke.include?(@userB).should be_false
60
- @userB.users_smoking_me.include?(@userA).should be_false
47
+ @monster.undo_like @fallen_angel
48
+ @monster.fallen_angels_i_like.include?(@fallen_angel).should be_false
49
+ @fallen_angel.monsters_liking_me.include?(@monster).should be_false
61
50
 
62
-
63
-
64
- # @userA.users_i_smoke_ids.should be_empty
65
- #@userB.users_smoking_me_ids.should be_empty
51
+ @monster.fallen_angels_i_like_ids.should be_empty
52
+ @fallen_angel.monsters_liking_me_ids.should be_empty
66
53
  end
67
54
 
68
55
  after(:all) do
69
- @userA.undo_smoke @userB
56
+ @monster.undo_like @fallen_angel
70
57
  end
71
- end
72
- end
58
+ end # context "when using an added behaviour"
59
+ end # describe
@@ -3,19 +3,19 @@ require 'spec_helper'
3
3
  describe Patriarch::DAOServices::BipartiteRelationshipBuilderService do
4
4
  before(:each) do
5
5
  @instance = Patriarch::DAOServices::BipartiteRelationshipBuilderService.instance
6
- @user = create_user
7
- @item = create_item(@user)
6
+ @monster = new_monster
7
+ @fallen_angel = new_fallen_angel
8
8
  @transac ||= Patriarch::TransactionServices::TransactionManagerService.instance.new_transaction(:like)
9
- @transac.add_step(:like,@user,@item)
9
+ @transac.add_step(:like,@monster,@fallen_angel)
10
10
  end
11
11
 
12
12
  it "shall insert processable lambdas into queues when create is called" do
13
13
  # Stubs the DAO retriever service so it gives DAO arrays back
14
14
  dao_sample = {
15
- :actor => Redis::SortedSet.new("user:1:redis_likes"),
16
- :target => Redis::Set.new("item:1:redis_liker_users")
15
+ :actor => Redis::SortedSet.new("monster:1:patriarch_fallen_angels_i_like"),
16
+ :target => Redis::SortedSet.new("fallen_angel:1:patriarch_monsters_liking_me")
17
17
  }
18
- Patriarch::DAOServices::RetrieverService.instance.stub(:call).with(any_args()).and_return(dao_sample)
18
+ Patriarch::DAOServices::RetrieverService.instance.stub(:call).twice.with(any_args()).and_return(dao_sample)
19
19
 
20
20
  expect{
21
21
  @instance.create(@transac)
@@ -29,12 +29,11 @@ describe Patriarch::DAOServices::BipartiteRelationshipBuilderService do
29
29
  it "shall insert processable lambdas into queues when destroy is called" do
30
30
  # Stubs the DAO retriever service so it gives DAO arrays back
31
31
  dao_sample = {
32
- :actor => Redis::SortedSet.new("user:1:redis_likes"),
33
- :target => Redis::Set.new("item:1:redis_liker_users")
32
+ :actor => Redis::SortedSet.new("monster:1:patriarch_fallen_angels_i_like"),
33
+ :target => Redis::SortedSet.new("fallen_angel:1:patriarch_monsters_liking_me")
34
34
  }
35
- Patriarch::DAOServices::RetrieverService.instance.stub(:call).with(any_args()).and_return(dao_sample)
35
+ Patriarch::DAOServices::RetrieverService.instance.stub(:call).twice.with(any_args()).and_return(dao_sample)
36
36
 
37
-
38
37
  expect{
39
38
  @instance.destroy(@transac)
40
39
  }.to change{ @transac.transaction_queue.select{ |queue_element| queue_element.is_a? Proc}.size }.by(2)
@@ -45,7 +44,7 @@ describe Patriarch::DAOServices::BipartiteRelationshipBuilderService do
45
44
  end
46
45
 
47
46
  after(:each) do
48
- $redis.del "user:#{@user.id}:redis_likes"
49
- $redis.del "item:#{@item.id}:redis_liker_users"
47
+ $redis.del "monster:#{@monster.id}:patriarch_fallen_angels_i_like"
48
+ $redis.del "fallen_angel:#{@fallen_angel.id}:patriarch_monsters_liking_me"
50
49
  end
51
50
  end
@@ -2,16 +2,17 @@ require 'spec_helper'
2
2
 
3
3
  describe Patriarch::Services do
4
4
  before (:each) do
5
- @user = create_user
6
- @item = create_item(@user)
5
+ @monster = new_monster
6
+ @fallen_angel = new_fallen_angel
7
7
  end
8
8
 
9
9
  context "LikeService" do
10
10
  it "should register like informations properly" do
11
11
  # Wrong way of doing tests, test should not know about what's inside ...
12
- Patriarch::Services::Like::ManagerService.instance.resolve(@user,@item)
13
- Redis::SortedSet.new("user:#{@user.id}:redis_likes").members.include?(@item.id.to_s).should be_true
14
- Redis::SortedSet.new("item:#{@item.id}:redis_liker_users").members.include?(@user.id.to_s).should be_true
12
+ pending "redo this test ffs"
13
+ # Patriarch::Services::Like::ManagerService.instance.resolve(@mc,@mk)
14
+ # Redis::SortedSet.new("mc:#{@mc.id}:redis_likes").members.include?(@mk.id.to_s).should be_true
15
+ # Redis::SortedSet.new("mk:#{@mk.id}:redis_liker_mcs").members.include?(@mc.id.to_s).should be_true
15
16
  end
16
17
  end
17
18
 
@@ -3,15 +3,17 @@ require 'spec_helper'
3
3
  describe Patriarch::DAOServices::RedisMapperService do
4
4
  before(:each) do
5
5
  @instance = Patriarch::DAOServices::RedisMapperService.instance
6
- @context = {:relation_type => :like, :actor_type => :user, :target_type => :item,
6
+ @context = {:relation_type => :like, :actor_type => :monster, :target_type => :fallen_angel,
7
7
  :actor_id => 1, :target_id => 1}
8
8
  @transaction_item = double()
9
- @transaction_item.stub(:context).and_return(@context)
9
+ @transaction_item.stub(:relation_type).and_return(@context[:relation_type])
10
+ @transaction_item.stub(:actor_type).and_return(@context[:actor_type])
11
+ @transaction_item.stub(:target_type).and_return(@context[:target_type])
10
12
  end
11
13
 
12
14
  it "shall retrieve right DAO information" do
13
- @instance.call(@transaction_item,:actor).should eq({:type => "sorted_set", :key => "redis_likes"})
14
- @instance.call(@transaction_item,:target).should eq({:type => "sorted_set", :key => "redis_liker_users"})
15
+ @instance.call(@transaction_item,:actor).should eq({:type => "sorted_set", :key => "patriarch_fallen_angels_i_like"})
16
+ @instance.call(@transaction_item,:target).should eq({:type => "sorted_set", :key => "patriarch_monsters_liking_me"})
15
17
  end
16
18
  end
17
19
 
@@ -0,0 +1,117 @@
1
+ require 'redis'
2
+ require 'redis/objects'
3
+
4
+ # Beware of poor autoload in redis_objects ...
5
+ # TODO ask nateware
6
+ require 'redis/objects/hashes'
7
+ require 'redis/objects/sorted_sets'
8
+
9
+ require 'patriarch'
10
+ require 'active_model'
11
+ require 'action_view'
12
+ require 'rails/generators'
13
+ require 'rspec/autorun'
14
+
15
+ require File.expand_path("../../lib/generators/patriarch/patriarch_generator", __FILE__)
16
+
17
+ RSpec.configure do |config|
18
+ # ## Mock Framework
19
+ #
20
+ # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
21
+ #
22
+ # config.mock_with :mocha
23
+ # config.mock_with :flexmock
24
+ # config.mock_with :rr
25
+ end
26
+
27
+ $redis=Redis.new(:host => 'localhost', :port => 6379, :db => 8)
28
+ $redis.flushdb
29
+
30
+ dummy_location = File.expand_path("../../spec/dummy", __FILE__)
31
+
32
+ puts "Cleaning previous dummy"
33
+ # refactor with elegant code here
34
+ `yes|rm -r #{dummy_location + "/*"}`
35
+
36
+ # Destroy previously initialized files in dummy, clean up !
37
+ puts "Generating files to be tested in a dummy"
38
+
39
+ # Initialize files in dummy :)
40
+ # Capture prevents stdout outputs from code between curly braces to be displayed
41
+ # This avoid polluting user interface
42
+ capture(:stdout) { PatriarchGenerator.start(%w(like),{:destination_root => dummy_location}) }
43
+
44
+ #Enhance load_path with dummy located files ...
45
+ $LOAD_PATH << File.expand_path("../../spec/dummy/lib", __FILE__)
46
+ $LOAD_PATH << File.expand_path("../../lib", __FILE__)
47
+
48
+ # load files living in the dummy ...
49
+ require File.expand_path("../../spec/dummy/lib/patriarch/services/like", __FILE__)
50
+ require File.expand_path("../../spec/dummy/lib/patriarch/behaviours/like", __FILE__)
51
+
52
+ class Monster
53
+ attr_reader :id
54
+
55
+ class << self
56
+ attr_accessor :count, :object_mapping
57
+ end
58
+
59
+ self.count = 1
60
+ self.object_mapping = {}
61
+
62
+ def initialize
63
+ @id = self.class.count
64
+ self.class.count += 1
65
+ self.class.object_mapping.merge!({@id => self.object_id})
66
+ end
67
+
68
+ def self.find(ids)
69
+ ids = Array(ids)
70
+ result = []
71
+ ids.each do |id|
72
+ object_ref = self.object_mapping[id]
73
+ record = ObjectSpace._id2ref(object_ref)
74
+ result << record unless record.nil?
75
+ end
76
+ result
77
+ end
78
+ end
79
+
80
+ class FallenAngel
81
+ attr_reader :id
82
+
83
+ class << self
84
+ attr_accessor :count, :object_mapping
85
+ end
86
+
87
+ self.count = 1
88
+ self.object_mapping = {}
89
+
90
+ def initialize
91
+ @id = self.class.count
92
+ self.class.count += 1
93
+ self.class.object_mapping.merge!({@id => self.object_id})
94
+ end
95
+
96
+ def self.find(ids)
97
+ ids = Array(ids)
98
+ result = []
99
+ ids.each do |id|
100
+ object_ref = self.object_mapping[id]
101
+ record = ObjectSpace._id2ref(object_ref)
102
+ result << record unless record.nil?
103
+ end
104
+ result
105
+ end
106
+ end
107
+
108
+ Monster.send(:include,Patriarch::Behaviours)
109
+ FallenAngel.send(:include,Patriarch::Behaviours)
110
+
111
+ def new_monster
112
+ Monster.new
113
+ end
114
+
115
+ def new_fallen_angel
116
+ FallenAngel.new
117
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: patriarch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,22 +9,55 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-25 00:00:00.000000000 Z
12
+ date: 2012-11-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: rspec
16
- requirement: &23131200 !ruby/object:Gem::Requirement
15
+ name: redis-objects
16
+ requirement: &12695180 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
21
  version: '0'
22
- type: :development
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *12695180
25
+ - !ruby/object:Gem::Dependency
26
+ name: verbs
27
+ requirement: &12694700 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
23
34
  prerelease: false
24
- version_requirements: *23131200
35
+ version_requirements: *12694700
36
+ - !ruby/object:Gem::Dependency
37
+ name: rails
38
+ requirement: &12694280 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *12694280
47
+ - !ruby/object:Gem::Dependency
48
+ name: railties
49
+ requirement: &12693800 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *12693800
25
58
  - !ruby/object:Gem::Dependency
26
59
  name: redis-objects
27
- requirement: &23130660 !ruby/object:Gem::Requirement
60
+ requirement: &12693300 !ruby/object:Gem::Requirement
28
61
  none: false
29
62
  requirements:
30
63
  - - ! '>='
@@ -32,7 +65,51 @@ dependencies:
32
65
  version: '0'
33
66
  type: :runtime
34
67
  prerelease: false
35
- version_requirements: *23130660
68
+ version_requirements: *12693300
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: &12692620 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *12692620
80
+ - !ruby/object:Gem::Dependency
81
+ name: fakeredis
82
+ requirement: &12691360 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *12691360
91
+ - !ruby/object:Gem::Dependency
92
+ name: autotest-rails
93
+ requirement: &12690940 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: *12690940
102
+ - !ruby/object:Gem::Dependency
103
+ name: ZenTest
104
+ requirement: &12690500 !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: *12690500
36
113
  description: Patriach is about adding behaviours on the fly to good old active record
37
114
  models.
38
115
  email:
@@ -54,6 +131,7 @@ files:
54
131
  - lib/generators/patriarch/templates/before_manager_service.rb
55
132
  - lib/generators/patriarch/templates/before_service.rb
56
133
  - lib/generators/patriarch/templates/empty_behaviour_module.rb
134
+ - lib/generators/patriarch/templates/empty_service_module.rb
57
135
  - lib/generators/patriarch/templates/manager_service.rb
58
136
  - lib/generators/patriarch/templates/service.rb
59
137
  - lib/generators/patriarch/templates/tools_methods.rb
@@ -76,15 +154,14 @@ files:
76
154
  - patriarch.gemspec
77
155
  - spec/lib/patriarch/add_behaviour_spec.rb
78
156
  - spec/lib/patriarch/bipartite_relationship_builder_spec.rb
79
- - spec/lib/patriarch/clean_reclip_spec.rb
80
157
  - spec/lib/patriarch/like_service_spec.rb
81
- - spec/lib/patriarch/reclip_spec.rb
82
158
  - spec/lib/patriarch/redis_mapper_service_spec.rb
83
159
  - spec/lib/patriarch/retriever_service_spec.rb
160
+ - spec/spec_helper.rb
84
161
  homepage: https://github.com/giglemad/patriarch
85
162
  licenses:
86
163
  - MIT
87
- post_install_message:
164
+ post_install_message: Thanks for installing!
88
165
  rdoc_options: []
89
166
  require_paths:
90
167
  - lib
@@ -109,8 +186,7 @@ summary: Manage relationships between instances of your models easily
109
186
  test_files:
110
187
  - spec/lib/patriarch/add_behaviour_spec.rb
111
188
  - spec/lib/patriarch/bipartite_relationship_builder_spec.rb
112
- - spec/lib/patriarch/clean_reclip_spec.rb
113
189
  - spec/lib/patriarch/like_service_spec.rb
114
- - spec/lib/patriarch/reclip_spec.rb
115
190
  - spec/lib/patriarch/redis_mapper_service_spec.rb
116
191
  - spec/lib/patriarch/retriever_service_spec.rb
192
+ - spec/spec_helper.rb
@@ -1,31 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "reclip cleaner" do
4
- before (:each) do
5
- @recliper_user = create_user
6
- @item = create_item(create_user)
7
- @recliped_on_user = create_user
8
- @recliper_user.reclip(@item,@recliped_on_user)
9
- @reclip_id = @item.reclips_of_me_reclips_ids.first
10
- end
11
-
12
- it "shall clean reclips everywhere when given a single item" do
13
- Patriarch::Behaviours::Reclip.clean_reclip_id(@reclip_id)
14
-
15
- @recliped_on_user.reclipeds_on_me_reclips_ids.include?(@reclip_id).should be_false
16
- @recliper_user.reclipeds_by_me_reclips_ids.include?(@reclip_id).should be_false
17
- @item.reclips_of_me_reclips_ids.include?(@reclip_id).should be_false
18
- end
19
-
20
- it "shall clean all the reclip stuff (on him, by him) of a given user properly" do
21
- pending('like the test above but in the adequate loop')
22
- end
23
-
24
- it "shall clean all the reclip stuff (on him, by him) of a given group properly" do
25
- pending('like the test above but in the adequate loop')
26
- end
27
-
28
- it "shall clean all the reclip stuff of a given item properly" do
29
- pending('like the test above but in the adequate loop')
30
- end
31
- end
@@ -1,39 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "reclip" do
4
- before (:each) do
5
- @recliper_user = create_user
6
- @item = create_item(create_user)
7
- @recliped_on_user = create_user
8
- end
9
-
10
- it "stores informations in redis properly" do
11
- @recliper_user.reclip(@item,@recliped_on_user)
12
-
13
- # Guy being recliped on remembers it
14
- @recliped_on_user.reclipeds_on_me.should == [@item]
15
- # Recliper remembers the reclip it did
16
- @recliper_user.reclipeds_by_me.should == [@item]
17
-
18
- # Item
19
- @recliper_user.reclipeds_by_me.should have(1).reclip_hash
20
- end
21
-
22
- it "only updates information when clipped twice" do
23
- @recliper_user.reclip(@item,@recliped_on_user)
24
- old_time = @recliper_user.reclipeds_by_me(:with_scores => true).first[1]
25
- @recliper_user.reclip(@item,@recliped_on_user)
26
- new_time = @recliper_user.reclipeds_by_me(:with_scores => true).first[1]
27
-
28
- new_time.should_not eq(old_time)
29
-
30
- # Guy being recliped on remembers it
31
- @recliped_on_user.reclipeds_on_me.should == [@item]
32
- # Recliper remembers the reclip it did
33
- @recliper_user.reclipeds_by_me.should == [@item]
34
-
35
- # Item
36
- @recliper_user.reclipeds_by_me.should have(1).reclip_hash
37
- end
38
-
39
- end