promiscuous 0.7.6 → 0.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -45,8 +45,9 @@ Promiscuous::AMQP.configure(:app => 'sniper',
45
45
  # subscriber
46
46
  class ModelSubscriber < Promiscuous::Subscriber::Mongoid
47
47
  subscribe :from => 'crowdtap/model',
48
- :class => Model,
49
- :attributes => [:field_1, :field_2, :field_3]
48
+ :attributes => [:field_1, :field_2, :field_3],
49
+ :class => Model, # optional
50
+ :foreign_key => :publisher_id # optional
50
51
  end
51
52
  ```
52
53
 
@@ -83,7 +84,6 @@ WARNING/TODO
83
84
  Promiscuous does **not** handle:
84
85
  - ActiveRecord polymorphism.
85
86
  - Any of the Mongoid atomic operatiors, such as inc, or add_to_set.
86
- - Mixing databases (ActiveRecord <=> Mongoid) because of the id format mismatch.
87
87
  - Association magic. Example:
88
88
  ```ruby
89
89
  # This will NOT replicate particiation_ids:
@@ -2,10 +2,12 @@ module Promiscuous::Common::Options
2
2
  extend ActiveSupport::Concern
3
3
 
4
4
  included do
5
- class_attribute :options, :options_mappings,
5
+ class_attribute :raw_options, :options, :options_mappings,
6
6
  :instance_reader => false,
7
7
  :instance_writer => false
8
- self.options = self.options_mappings = {}
8
+ self.raw_options = {}
9
+ self.options = {}
10
+ self.options_mappings = {}
9
11
  end
10
12
 
11
13
  module ClassMethods
@@ -32,9 +34,14 @@ module Promiscuous::Common::Options
32
34
  if instance_reader
33
35
  define_method("#{attr_alias}") { self.class.__send__("#{attr_alias}") }
34
36
  end
37
+
38
+ self.__send__("#{attr_alias}=", raw_options[attr]) if raw_options[attr]
35
39
  end
36
40
 
37
41
  def load_options(options)
42
+ self.raw_options = self.raw_options.dup
43
+ self.raw_options.merge!(options)
44
+
38
45
  options.each do |attr, value|
39
46
  attr_alias = self.options_mappings[attr]
40
47
  self.__send__("#{attr_alias}=", value) if attr_alias
@@ -5,9 +5,10 @@ module Promiscuous::Subscriber::Lint::Attributes
5
5
  super
6
6
 
7
7
  instance = subscriber.klass.new
8
- subscriber.attributes.each do |attr|
9
- instance.respond_to?("#{attr}=") or instance.__send__("#{attr}=")
10
- end
8
+ attributes = subscriber.attributes
9
+ attributes += [subscriber.foreign_key] if subscriber.foreign_key
10
+
11
+ attributes.each { |attr| instance.respond_to?("#{attr}=") or instance.__send__("#{attr}=") }
11
12
 
12
13
  if check_publisher
13
14
  missing_attributes = subscriber.attributes - publisher.attributes
@@ -2,17 +2,38 @@ module Promiscuous::Subscriber::Model
2
2
  extend ActiveSupport::Concern
3
3
  include Promiscuous::Subscriber::Envelope
4
4
 
5
- def fetch
6
- case operation
7
- when :create
5
+ def fetch_new
6
+ if foreign_key
7
+ klass.new(foreign_key => id)
8
+ else
8
9
  klass.new.tap { |o| o.id = id }
9
- when :update
10
- klass.find(id)
11
- when :destroy
10
+ end
11
+ end
12
+
13
+ def fetch_existing
14
+ if foreign_key
15
+ if klass.respond_to?("find_by_#{foreign_key}!")
16
+ klass.__send__("find_by_#{foreign_key}!", id)
17
+ elsif klass.respond_to?("find_by")
18
+ klass.find_by(foreign_key => id)
19
+ else
20
+ record = klass.where(foreign_key => id).first
21
+ raise self.class.missing_record_exception.new(klass, id) if record.nil?
22
+ record
23
+ end
24
+ else
12
25
  klass.find(id)
13
26
  end
14
27
  end
15
28
 
29
+ def fetch
30
+ case operation
31
+ when :create then fetch_new
32
+ when :update then fetch_existing
33
+ when :destroy then fetch_existing
34
+ end
35
+ end
36
+
16
37
  def process_attributes?
17
38
  operation != :destroy
18
39
  end
@@ -20,16 +41,15 @@ module Promiscuous::Subscriber::Model
20
41
  def process
21
42
  super
22
43
  case operation
23
- when :create
24
- instance.save!
25
- when :update
26
- instance.save!
27
- when :destroy
28
- instance.destroy
44
+ when :create then instance.save!
45
+ when :update then instance.save!
46
+ when :destroy then instance.destroy
29
47
  end
30
48
  end
31
49
 
32
50
  included do
51
+ use_option :foreign_key
52
+
33
53
  use_payload_attribute :id
34
54
  use_payload_attribute :operation, :symbolize => true
35
55
  end
@@ -6,7 +6,7 @@ module Promiscuous::Subscriber::Upsert
6
6
  super
7
7
  rescue self.class.missing_record_exception
8
8
  Promiscuous.warn "[receive] upserting #{payload}"
9
- klass.new.tap { |o| o.id = id }
9
+ fetch_new
10
10
  end
11
11
  end
12
12
  end
@@ -1,3 +1,3 @@
1
1
  module Promiscuous
2
- VERSION = '0.7.6'
2
+ VERSION = '0.8'
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.7.6
4
+ version: '0.8'
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-20 00:00:00.000000000 Z
13
+ date: 2012-08-28 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: mongoid
@@ -116,57 +116,57 @@ executables: []
116
116
  extensions: []
117
117
  extra_rdoc_files: []
118
118
  files:
119
- - lib/promiscuous/version.rb
120
- - lib/promiscuous/common.rb
121
- - lib/promiscuous/publisher/lint.rb
119
+ - lib/promiscuous/amqp/bunny.rb
120
+ - lib/promiscuous/amqp/null.rb
121
+ - lib/promiscuous/amqp/rubyamqp.rb
122
+ - lib/promiscuous/publisher/envelope.rb
123
+ - lib/promiscuous/publisher/mongoid/embedded.rb
124
+ - lib/promiscuous/publisher/active_record.rb
125
+ - lib/promiscuous/publisher/amqp.rb
122
126
  - lib/promiscuous/publisher/attributes.rb
127
+ - lib/promiscuous/publisher/base.rb
123
128
  - lib/promiscuous/publisher/class.rb
124
- - lib/promiscuous/publisher/mongoid.rb
125
- - lib/promiscuous/publisher/lint/attributes.rb
126
- - lib/promiscuous/publisher/lint/class.rb
129
+ - lib/promiscuous/publisher/lint.rb
127
130
  - lib/promiscuous/publisher/lint/amqp.rb
131
+ - lib/promiscuous/publisher/lint/attributes.rb
128
132
  - lib/promiscuous/publisher/lint/base.rb
133
+ - lib/promiscuous/publisher/lint/class.rb
129
134
  - lib/promiscuous/publisher/lint/polymorphic.rb
130
- - lib/promiscuous/publisher/mongoid/embedded.rb
131
- - lib/promiscuous/publisher/active_record.rb
132
- - lib/promiscuous/publisher/model.rb
133
- - lib/promiscuous/publisher/envelope.rb
134
135
  - lib/promiscuous/publisher/mock.rb
135
- - lib/promiscuous/publisher/amqp.rb
136
- - lib/promiscuous/publisher/base.rb
136
+ - lib/promiscuous/publisher/model.rb
137
+ - lib/promiscuous/publisher/mongoid.rb
137
138
  - lib/promiscuous/publisher/polymorphic.rb
138
- - lib/promiscuous/amqp/null.rb
139
- - lib/promiscuous/amqp/rubyamqp.rb
140
- - lib/promiscuous/amqp/bunny.rb
141
- - lib/promiscuous/publisher.rb
142
- - lib/promiscuous/subscriber/lint.rb
139
+ - lib/promiscuous/railtie/replicate.rake
140
+ - lib/promiscuous/subscriber/envelope.rb
141
+ - lib/promiscuous/subscriber/error.rb
142
+ - lib/promiscuous/subscriber/mongoid/embedded.rb
143
+ - lib/promiscuous/subscriber/active_record.rb
144
+ - lib/promiscuous/subscriber/amqp.rb
143
145
  - lib/promiscuous/subscriber/attributes.rb
144
146
  - lib/promiscuous/subscriber/class.rb
145
- - lib/promiscuous/subscriber/mongoid.rb
146
- - lib/promiscuous/subscriber/error.rb
147
- - lib/promiscuous/subscriber/lint/attributes.rb
148
- - lib/promiscuous/subscriber/lint/class.rb
147
+ - lib/promiscuous/subscriber/lint.rb
149
148
  - lib/promiscuous/subscriber/lint/amqp.rb
150
149
  - lib/promiscuous/subscriber/lint/base.rb
150
+ - lib/promiscuous/subscriber/lint/class.rb
151
151
  - lib/promiscuous/subscriber/lint/polymorphic.rb
152
- - lib/promiscuous/subscriber/mongoid/embedded.rb
153
- - lib/promiscuous/subscriber/active_record.rb
154
- - lib/promiscuous/subscriber/upsert.rb
155
- - lib/promiscuous/subscriber/model.rb
156
- - lib/promiscuous/subscriber/envelope.rb
157
- - lib/promiscuous/subscriber/amqp.rb
158
- - lib/promiscuous/subscriber/base.rb
152
+ - lib/promiscuous/subscriber/lint/attributes.rb
159
153
  - lib/promiscuous/subscriber/polymorphic.rb
154
+ - lib/promiscuous/subscriber/base.rb
155
+ - lib/promiscuous/subscriber/mongoid.rb
156
+ - lib/promiscuous/subscriber/model.rb
157
+ - lib/promiscuous/subscriber/upsert.rb
158
+ - lib/promiscuous/config.rb
159
+ - lib/promiscuous/amqp.rb
160
+ - lib/promiscuous/common.rb
160
161
  - lib/promiscuous/common/lint.rb
161
- - lib/promiscuous/common/options.rb
162
162
  - lib/promiscuous/common/lint/base.rb
163
- - lib/promiscuous/worker.rb
164
- - lib/promiscuous/railtie/replicate.rake
165
- - lib/promiscuous/railtie.rb
163
+ - lib/promiscuous/common/options.rb
166
164
  - lib/promiscuous/loader.rb
167
- - lib/promiscuous/amqp.rb
168
- - lib/promiscuous/config.rb
165
+ - lib/promiscuous/publisher.rb
166
+ - lib/promiscuous/railtie.rb
169
167
  - lib/promiscuous/subscriber.rb
168
+ - lib/promiscuous/worker.rb
169
+ - lib/promiscuous/version.rb
170
170
  - lib/promiscuous.rb
171
171
  - README.md
172
172
  homepage: http://github.com/crowdtap/promiscuous