promiscuous 0.6.4 → 0.6.5

Sign up to get free protection for your applications and to get access to all the features.
data/lib/promiscuous.rb CHANGED
@@ -4,16 +4,10 @@ require 'promiscuous/amqp'
4
4
  require 'promiscuous/loader'
5
5
  require 'promiscuous/railtie' if defined?(Rails)
6
6
 
7
- begin
8
- require 'mongoid'
9
- require 'active_record'
10
- rescue LoadError
11
- end
12
-
13
- require 'promiscuous/publisher'
14
- require 'promiscuous/subscriber'
15
-
16
7
  module Promiscuous
8
+ autoload :Publisher, 'promiscuous/publisher'
9
+ autoload :Subscriber, 'promiscuous/subscriber'
10
+
17
11
  class << self
18
12
  def configure(&block)
19
13
  Config.configure(&block)
@@ -1,15 +1,13 @@
1
- require 'promiscuous/amqp/bunny'
2
- require 'promiscuous/amqp/rubyamqp'
3
- require 'promiscuous/amqp/null'
1
+ module Promiscuous::AMQP
2
+ autoload :Bunny, 'promiscuous/amqp/bunny'
3
+ autoload :RubyAMQP, 'promiscuous/amqp/rubyamqp'
4
+ autoload :Null, 'promiscuous/amqp/null'
4
5
 
5
- module Promiscuous
6
- module AMQP
7
- class << self
8
- def backend
9
- Promiscuous::Config.backend
10
- end
11
-
12
- delegate :connect, :disconnect, :publish, :subscribe, :to => :backend
6
+ class << self
7
+ def backend
8
+ Promiscuous::Config.backend
13
9
  end
10
+
11
+ delegate :connect, :disconnect, :publish, :subscribe, :to => :backend
14
12
  end
15
13
  end
@@ -1,4 +1,5 @@
1
1
  module Promiscuous::Publisher
2
- require 'promiscuous/publisher/active_record' if defined?(ActiveRecord)
3
- require 'promiscuous/publisher/mongoid' if defined?(Mongoid)
2
+ autoload :ActiveRecord, 'promiscuous/publisher/active_record'
3
+ autoload :Mongoid, 'promiscuous/publisher/mongoid'
4
+ autoload :Mock, 'promiscuous/publisher/mock'
4
5
  end
@@ -0,0 +1,59 @@
1
+ class Promiscuous::Publisher::Mock
2
+ def self.publish(options)
3
+ if defined?(attributes)
4
+ if options[:attributes]
5
+ raise "Do not specify the 'to' field in childrens" if options[:to]
6
+ self.attributes = self.attributes + options[:attributes]
7
+ end
8
+ else
9
+ class_attribute :attributes, :to
10
+
11
+ self.to = options[:to]
12
+ self.attributes = options[:attributes].to_a
13
+
14
+ attr_accessor :id, :new_record
15
+ end
16
+ attr_accessor *attributes
17
+ end
18
+
19
+ def self.class_name
20
+ name.split('::').last
21
+ end
22
+
23
+ def initialize
24
+ self.id = BSON::ObjectId.new
25
+ self.new_record = true
26
+ end
27
+
28
+ def save
29
+ Promiscuous::Subscriber.process(payload)
30
+ self.new_record = false
31
+ true
32
+ end
33
+ alias :save! :save
34
+
35
+ def payload
36
+ {
37
+ '__amqp__' => self.class.to,
38
+ 'id' => self.id,
39
+ 'type' => self.class.class_name,
40
+ 'operation' => self.new_record ? 'create' : 'update',
41
+ 'payload' => Hash[self.class.attributes.map { |attr| [attr.to_s, payload_for(attr)] }]
42
+ }
43
+ end
44
+
45
+ def payload_for(attr)
46
+ value = __send__(attr)
47
+ value = value.payload if value.is_a?(Promiscuous::Publisher::Mock)
48
+ value
49
+ end
50
+
51
+ def destroy
52
+ Promiscuous::Subscriber.process(
53
+ '__amqp__' => self.class.to,
54
+ 'id' => self.id,
55
+ 'type' => self.class.class_name,
56
+ 'operation' => 'destroy'
57
+ )
58
+ end
59
+ end
@@ -1,8 +1,8 @@
1
1
  module Promiscuous::Subscriber
2
- require 'promiscuous/subscriber/active_record' if defined?(ActiveRecord)
3
- require 'promiscuous/subscriber/mongoid' if defined?(Mongoid)
4
- require 'promiscuous/subscriber/error'
5
- require 'promiscuous/subscriber/amqp'
2
+ autoload :ActiveRecord, 'promiscuous/subscriber/active_record'
3
+ autoload :Mongoid, 'promiscuous/subscriber/mongoid'
4
+ autoload :Error, 'promiscuous/subscriber/error'
5
+ autoload :AMQP, 'promiscuous/subscriber/amqp'
6
6
 
7
7
  mattr_accessor :subscribers
8
8
  self.subscribers = {}
@@ -5,7 +5,7 @@ module Promiscuous::Subscriber::CustomClass
5
5
 
6
6
  def klass
7
7
  unless subscribe_options[:class]
8
- raise "I don't want to be rude or anything, "
8
+ raise "I don't want to be rude or anything, " +
9
9
  "but have you defined the class to deserialize?"
10
10
  end
11
11
  subscribe_options[:class]
@@ -1,3 +1,3 @@
1
1
  module Promiscuous
2
- VERSION = '0.6.4'
2
+ VERSION = '0.6.5'
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.4
4
+ version: 0.6.5
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-14 00:00:00.000000000 Z
13
+ date: 2012-08-15 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: mongoid
@@ -100,13 +100,10 @@ executables: []
100
100
  extensions: []
101
101
  extra_rdoc_files: []
102
102
  files:
103
- - lib/promiscuous.rb
104
- - lib/promiscuous/amqp.rb
105
103
  - lib/promiscuous/amqp/bunny.rb
106
104
  - lib/promiscuous/amqp/null.rb
107
105
  - lib/promiscuous/amqp/rubyamqp.rb
108
106
  - lib/promiscuous/loader.rb
109
- - lib/promiscuous/publisher.rb
110
107
  - lib/promiscuous/publisher/active_record.rb
111
108
  - lib/promiscuous/publisher/amqp.rb
112
109
  - lib/promiscuous/publisher/attributes.rb
@@ -117,13 +114,12 @@ files:
117
114
  - lib/promiscuous/publisher/mongoid.rb
118
115
  - lib/promiscuous/publisher/mongoid/embedded.rb
119
116
  - lib/promiscuous/publisher/polymorphic.rb
117
+ - lib/promiscuous/publisher/mock.rb
120
118
  - lib/promiscuous/railtie.rb
121
119
  - lib/promiscuous/railtie/replicate.rake
122
- - lib/promiscuous/subscriber.rb
123
120
  - lib/promiscuous/subscriber/amqp.rb
124
121
  - lib/promiscuous/subscriber/attributes.rb
125
122
  - lib/promiscuous/subscriber/base.rb
126
- - lib/promiscuous/subscriber/custom_class.rb
127
123
  - lib/promiscuous/subscriber/envelope.rb
128
124
  - lib/promiscuous/subscriber/error.rb
129
125
  - lib/promiscuous/subscriber/generic.rb
@@ -134,9 +130,14 @@ files:
134
130
  - lib/promiscuous/subscriber/active_record.rb
135
131
  - lib/promiscuous/subscriber/mongoid.rb
136
132
  - lib/promiscuous/subscriber/upsert.rb
133
+ - lib/promiscuous/subscriber/custom_class.rb
137
134
  - lib/promiscuous/config.rb
138
135
  - lib/promiscuous/worker.rb
136
+ - lib/promiscuous/publisher.rb
137
+ - lib/promiscuous/subscriber.rb
138
+ - lib/promiscuous/amqp.rb
139
139
  - lib/promiscuous/version.rb
140
+ - lib/promiscuous.rb
140
141
  - README.md
141
142
  homepage: http://github.com/crowdtap/promiscuous
142
143
  licenses: []