promiscuous 0.6.6 → 0.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -29,7 +29,7 @@ Promiscuous::AMQP.configure(:app => 'crowdtap',
29
29
  # publisher
30
30
  class ModelPublisher < Promiscuous::Publisher::Mongoid
31
31
  publish :to => 'crowdtap/model',
32
- :class => Model,
32
+ :class => :Model,
33
33
  :attributes => [:field_1, :field_2, :field_3]
34
34
  end
35
35
  ```
@@ -3,12 +3,8 @@ module Promiscuous
3
3
  def self.load_descriptors(descriptors=[:publishers, :subscribers])
4
4
  [descriptors].flatten.each do |descriptor|
5
5
  dir, file_matcher = case descriptor
6
- when :publishers
7
- # TODO Cleanup publishers
8
- %w(publishers **_publisher.rb)
9
- when :subscribers
10
- Promiscuous::Subscriber.subscribers.clear
11
- %w(subscribers **_subscriber.rb)
6
+ when :publishers then %w(publishers **_publisher.rb)
7
+ when :subscribers then %w(subscribers **_subscriber.rb)
12
8
  end
13
9
 
14
10
  Dir[Rails.root.join('app', dir, file_matcher)].map do |file|
@@ -16,5 +12,14 @@ module Promiscuous
16
12
  end
17
13
  end
18
14
  end
15
+
16
+ def self.unload_descriptors(descriptors=[:publishers, :subscribers])
17
+ [descriptors].flatten.each do |descriptor|
18
+ dir, file_matcher = case descriptor
19
+ when :publishers then # TODO Cleanup publishers
20
+ when :subscribers then Promiscuous::Subscriber.subscribers.clear
21
+ end
22
+ end
23
+ end
19
24
  end
20
25
  end
@@ -2,4 +2,9 @@ module Promiscuous::Publisher
2
2
  autoload :ActiveRecord, 'promiscuous/publisher/active_record'
3
3
  autoload :Mongoid, 'promiscuous/publisher/mongoid'
4
4
  autoload :Mock, 'promiscuous/publisher/mock'
5
+ autoload :Lint, 'promiscuous/publisher/lint'
6
+
7
+ def self.lint(*args)
8
+ Lint.lint(*args)
9
+ end
5
10
  end
@@ -4,11 +4,7 @@ module Promiscuous::Publisher::Attributes
4
4
  def payload
5
5
  return nil unless include_attributes?
6
6
 
7
- Hash[attributes.map do |field|
8
- optional = field.to_s[-1] == '?'
9
- field = field.to_s[0...-1].to_sym if optional
10
- [field, payload_for(field)] if !optional || instance.respond_to?(field)
11
- end]
7
+ Hash[attributes.map { |field| [field, payload_for(field)] }]
12
8
  end
13
9
 
14
10
  def payload_for(field)
@@ -25,4 +21,15 @@ module Promiscuous::Publisher::Attributes
25
21
  end
26
22
 
27
23
  included { use_option :attributes }
24
+
25
+ module ClassMethods
26
+ def publish(options)
27
+ if self.options[:attributes] and options[:attributes]
28
+ options = options.dup
29
+ options[:attributes] = (self.options[:attributes] + options[:attributes]).uniq
30
+ end
31
+
32
+ super(options)
33
+ end
34
+ end
28
35
  end
@@ -1,6 +1,7 @@
1
1
  class Promiscuous::Publisher::Base
2
2
  attr_accessor :options
3
- class_attribute :options
3
+ class_attribute :options, :published
4
+ self.options = {}
4
5
 
5
6
  def initialize(options)
6
7
  self.options = options
@@ -11,7 +12,8 @@ class Promiscuous::Publisher::Base
11
12
  end
12
13
 
13
14
  def self.publish(options)
14
- self.options = options
15
+ self.options = self.options.merge(options)
16
+ self.published = true
15
17
  end
16
18
 
17
19
  def self.use_option(attr)
@@ -5,10 +5,22 @@ module Promiscuous::Publisher::ClassBind
5
5
  def publish(options)
6
6
  super
7
7
 
8
- publisher_class = self
9
- options[:class].class_eval do
10
- class_attribute :promiscuous_publisher
11
- self.promiscuous_publisher = publisher_class
8
+ unless options[:inherited] and options[:class]
9
+ publisher_class = self
10
+ klass.class_eval do
11
+ class_attribute :promiscuous_publisher
12
+ self.promiscuous_publisher = publisher_class
13
+ end
14
+ end
15
+ end
16
+
17
+ def klass
18
+ if options[:class]
19
+ options[:class].to_s.constantize
20
+ else
21
+ class_name = "::#{name.split('::').last}"
22
+ class_name = $1 if class_name =~ /^(.+)Publisher$/
23
+ class_name.constantize
12
24
  end
13
25
  end
14
26
  end
@@ -0,0 +1,29 @@
1
+ module Promiscuous::Publisher::Lint
2
+ autoload :Base, 'promiscuous/publisher/lint/base'
3
+ autoload :ClassBind, 'promiscuous/publisher/lint/class_bind'
4
+ autoload :Attributes, 'promiscuous/publisher/lint/attributes'
5
+ autoload :Polymorphic, 'promiscuous/publisher/lint/polymorphic'
6
+ autoload :AMQP, 'promiscuous/publisher/lint/amqp'
7
+
8
+ def self.get_publisher(klass)
9
+ unless klass.respond_to?(:promiscuous_publisher)
10
+ raise "#{klass} has no publisher"
11
+ end
12
+
13
+ klass.promiscuous_publisher
14
+ end
15
+
16
+ def self.lint(classes)
17
+ classes.each do |klass, to|
18
+ pub = get_publisher(klass)
19
+
20
+ lint = Class.new(Base)
21
+ lint.__send__(:include, ClassBind) if pub.include?(Promiscuous::Publisher::ClassBind)
22
+ lint.__send__(:include, Attributes) if pub.include?(Promiscuous::Publisher::Attributes)
23
+ lint.__send__(:include, Polymorphic) if pub.include?(Promiscuous::Publisher::Polymorphic)
24
+ lint.__send__(:include, AMQP) if pub.include?(Promiscuous::Publisher::AMQP)
25
+ lint.new(:klass => klass, :publisher => pub, :to => to).lint
26
+ end
27
+ true
28
+ end
29
+ end
@@ -0,0 +1,14 @@
1
+ module Promiscuous::Publisher::Lint::AMQP
2
+ extend ActiveSupport::Concern
3
+
4
+ def lint
5
+ super
6
+
7
+ pub_to = publisher_instance.to
8
+ if pub_to != to
9
+ raise "#{publisher} publishes #{klass} to #{pub_to} instead of #{to}"
10
+ end
11
+ end
12
+
13
+ included { use_option(:to) }
14
+ end
@@ -0,0 +1,14 @@
1
+ module Promiscuous::Publisher::Lint::Attributes
2
+ extend ActiveSupport::Concern
3
+
4
+ def lint
5
+ super
6
+
7
+ instance = klass.new
8
+ publisher.options[:attributes].each do |attr|
9
+ instance.respond_to?(attr) or instance.__send__(attr)
10
+ end
11
+ end
12
+
13
+ included { use_option(:klass) }
14
+ end
@@ -0,0 +1,22 @@
1
+ class Promiscuous::Publisher::Lint::Base
2
+ attr_accessor :options
3
+
4
+ def initialize(options)
5
+ self.options = options
6
+ end
7
+
8
+ def publisher_instance
9
+ @publisher_instance ||= publisher.new({})
10
+ end
11
+
12
+ def lint
13
+ end
14
+
15
+ def self.use_option(attr)
16
+ define_method(attr) do
17
+ self.options[attr]
18
+ end
19
+ end
20
+
21
+ use_option(:publisher)
22
+ end
@@ -0,0 +1,14 @@
1
+ module Promiscuous::Publisher::Lint::ClassBind
2
+ extend ActiveSupport::Concern
3
+
4
+ def lint
5
+ super
6
+
7
+ if publisher.klass != klass
8
+ raise "Define a publisher for #{klass}"
9
+ end
10
+ end
11
+
12
+ included { use_option(:klass) }
13
+ end
14
+
@@ -0,0 +1,3 @@
1
+
2
+ # 1. all the dep are covered
3
+ # 2. all the attributes are covered
@@ -0,0 +1,21 @@
1
+ module Promiscuous::Publisher::Lint::Polymorphic
2
+ extend ActiveSupport::Concern
3
+
4
+ def lint
5
+ super
6
+
7
+ unless skip_polymorphic
8
+ klass.descendants.each do |subclass|
9
+ pub = Promiscuous::Publisher::Lint.get_publisher(subclass)
10
+ self.class.new(options.merge(:klass => subclass,
11
+ :publisher => pub,
12
+ :skip_polymorphic => true)).lint
13
+ end
14
+ end
15
+ end
16
+
17
+ included do
18
+ use_option(:klass)
19
+ use_option(:skip_polymorphic)
20
+ end
21
+ end
@@ -16,20 +16,27 @@ module Promiscuous::Publisher::Model
16
16
  operation != :destroy
17
17
  end
18
18
 
19
+ included do
20
+ hook_callbacks if published
21
+ end
22
+
19
23
  module ClassMethods
20
24
  def publish(options)
21
25
  super
26
+ hook_callbacks
27
+ end
22
28
 
23
- options[:class].class_eval do
24
- [:create, :update, :destroy].each do |operation|
25
- __send__("after_#{operation}", "promiscuous_publish_#{operation}".to_sym)
26
-
27
- define_method "promiscuous_publish_#{operation}" do
28
- self.class.promiscuous_publisher.new(:instance => self, :operation => operation).amqp_publish
29
+ def hook_callbacks
30
+ unless respond_to?(:promiscuous_publish_update)
31
+ klass.class_eval do
32
+ [:create, :update, :destroy].each do |operation|
33
+ __send__("after_#{operation}", "promiscuous_publish_#{operation}".to_sym)
34
+ define_method "promiscuous_publish_#{operation}" do
35
+ self.class.promiscuous_publisher.new(:instance => self, :operation => operation).amqp_publish
36
+ end
29
37
  end
30
38
  end
31
39
  end
32
40
  end
33
41
  end
34
42
  end
35
-
@@ -13,16 +13,14 @@ class Promiscuous::Publisher::Mongoid < Promiscuous::Publisher::Base
13
13
  include Promiscuous::Publisher::Envelope
14
14
 
15
15
  def self.publish(options)
16
- return super if options[:mongoid_loaded]
16
+ super
17
17
 
18
- if options[:class].embedded?
18
+ if klass.embedded?
19
19
  require 'promiscuous/publisher/mongoid/embedded'
20
20
  include Promiscuous::Publisher::Mongoid::Embedded
21
21
  else
22
22
  require 'promiscuous/publisher/model'
23
23
  include Promiscuous::Publisher::Model
24
24
  end
25
-
26
- self.publish(options.merge(:mongoid_loaded => true))
27
25
  end
28
26
  end
@@ -5,23 +5,19 @@ module Promiscuous::Publisher::Mongoid::Embedded
5
5
  super.merge(:id => instance.id)
6
6
  end
7
7
 
8
- module ClassMethods
9
- def publish(options)
10
- super
11
-
12
- options[:class].class_eval do
13
- callback = proc do
14
- if _parent.respond_to?(:promiscuous_publish_update)
15
- _parent.save
16
- _parent.reload # mongoid is not that smart, so we need to reload here.
17
- _parent.promiscuous_publish_update
18
- end
8
+ included do
9
+ klass.class_eval do
10
+ callback = proc do
11
+ if _parent.respond_to?(:promiscuous_publish_update)
12
+ _parent.save
13
+ _parent.reload # mongoid is not that smart, so we need to reload here.
14
+ _parent.promiscuous_publish_update
19
15
  end
20
-
21
- after_create callback
22
- after_update callback
23
- after_destroy callback
24
16
  end
17
+
18
+ after_create callback
19
+ after_update callback
20
+ after_destroy callback
25
21
  end
26
22
  end
27
23
  end
@@ -7,4 +7,16 @@ module Promiscuous::Publisher::Polymorphic
7
7
  def payload
8
8
  super.merge(:type => instance.class.to_s)
9
9
  end
10
+
11
+ module ClassMethods
12
+ def publish(options)
13
+ super
14
+ self.descendants.each { |subclass| inherited(subclass) }
15
+ end
16
+
17
+ def inherited(subclass)
18
+ super
19
+ subclass.publish(options.merge(:inherited => true)) if published
20
+ end
21
+ end
10
22
  end
@@ -6,7 +6,10 @@ module Promiscuous
6
6
  config.after_initialize do
7
7
  Promiscuous::Loader.load_descriptors(:publishers)
8
8
  ActionDispatch::Reloader.to_prepare do
9
- Promiscuous::Loader.load_descriptors(:publishers)
9
+ Promiscuous::Loader.load_descriptors
10
+ end
11
+ ActionDispatch::Reloader.to_cleanup do
12
+ Promiscuous::Loader.unload_descriptors
10
13
  end
11
14
  end
12
15
  end
@@ -1,3 +1,3 @@
1
1
  module Promiscuous
2
- VERSION = '0.6.6'
2
+ VERSION = '0.7'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: promiscuous
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.6
4
+ version: '0.7'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-08-16 00:00:00.000000000 Z
13
+ date: 2012-08-19 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: mongoid
@@ -118,8 +118,15 @@ extra_rdoc_files: []
118
118
  files:
119
119
  - lib/promiscuous/version.rb
120
120
  - lib/promiscuous/publisher/class_bind.rb
121
+ - lib/promiscuous/publisher/lint.rb
121
122
  - lib/promiscuous/publisher/attributes.rb
122
123
  - lib/promiscuous/publisher/mongoid.rb
124
+ - lib/promiscuous/publisher/lint/class_bind.rb
125
+ - lib/promiscuous/publisher/lint/attributes.rb
126
+ - lib/promiscuous/publisher/lint/model.rb
127
+ - lib/promiscuous/publisher/lint/amqp.rb
128
+ - lib/promiscuous/publisher/lint/base.rb
129
+ - lib/promiscuous/publisher/lint/polymorphic.rb
123
130
  - lib/promiscuous/publisher/mongoid/embedded.rb
124
131
  - lib/promiscuous/publisher/active_record.rb
125
132
  - lib/promiscuous/publisher/model.rb
@@ -135,8 +142,6 @@ files:
135
142
  - lib/promiscuous/subscriber/attributes.rb
136
143
  - lib/promiscuous/subscriber/mongoid.rb
137
144
  - lib/promiscuous/subscriber/error.rb
138
- - lib/promiscuous/subscriber/generic.rb
139
- - lib/promiscuous/subscriber/mongoid/root.rb
140
145
  - lib/promiscuous/subscriber/mongoid/embedded.rb
141
146
  - lib/promiscuous/subscriber/active_record.rb
142
147
  - lib/promiscuous/subscriber/upsert.rb
@@ -175,7 +180,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
175
180
  version: '0'
176
181
  requirements: []
177
182
  rubyforge_project:
178
- rubygems_version: 1.8.22
183
+ rubygems_version: 1.8.24
179
184
  signing_key:
180
185
  specification_version: 3
181
186
  summary: Model replication over RabbitMQ
@@ -1,14 +0,0 @@
1
- require 'promiscuous/subscriber/custom_class'
2
- require 'promiscuous/subscriber/base'
3
- require 'promiscuous/subscriber/attributes'
4
- require 'promiscuous/subscriber/polymorphic'
5
- require 'promiscuous/subscriber/amqp'
6
- require 'promiscuous/subscriber/envelope'
7
-
8
- class Promiscuous::Subscriber::Generic < Promiscuous::Subscriber::Base
9
- include Promiscuous::Subscriber::CustomClass
10
- include Promiscuous::Subscriber::Attributes
11
- include Promiscuous::Subscriber::Polymorphic
12
- include Promiscuous::Subscriber::AMQP
13
- include Promiscuous::Subscriber::Envelope
14
- end
@@ -1,6 +0,0 @@
1
- require 'promiscuous/publisher/model'
2
-
3
- module Promiscuous::Subscriber::Mongoid::Root
4
- extend ActiveSupport::Concern
5
- include Promiscuous::Subscriber::Model
6
- end