multiple_man 0.1.4 → 0.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c2a4b260906b99133630620edf54de759b89cebe
4
- data.tar.gz: 3c34667e49a1e021b6da5c5121ffdbefb72da4db
3
+ metadata.gz: 9e1c4aa897f1db298c404d9f6d9a511d497f8ca1
4
+ data.tar.gz: 68d3af66452856d5683f3dd9893aa77216347bad
5
5
  SHA512:
6
- metadata.gz: c2415f3bf346b9ffdd1922023b9eb7ce68c07bb6ceaf70ca4b4791b7af197303b719ce212f1fbfa1178e21a17343a1fac74ba04f994d7851c69e86f4dc46f6e8
7
- data.tar.gz: 92dc26d2dae07a016b4e00f002639dc83864fbd1b7e72b01df793cba52ab37e9c6f5cd55dfff573e2f7d933934b92311e203c7d86d16615a3c7b6200a19c0545
6
+ metadata.gz: fa34e065547d1e52973f77dd36d907054177a36a858311a91b576fed1405a4dc0d3173cf0384a0c600a190ada4a73b11bdd085bc5e487345b8a3ad799c19c621
7
+ data.tar.gz: 776d1610bdce6abdcc08af4dd730802ba5b34722ce07d9866acab98a50dcaa357987175d96f7c1b76c6a27a24df13aa2064c61e9a90e4277abda0fd7d541ceb9
data/lib/multiple_man.rb CHANGED
@@ -24,4 +24,4 @@ module MultipleMan
24
24
  def self.enable!
25
25
  MultipleMan.configuration.enabled = true
26
26
  end
27
- end
27
+ end
@@ -3,17 +3,32 @@ require 'json'
3
3
  module MultipleMan
4
4
  class AttributeExtractor
5
5
 
6
- def initialize(record, fields)
6
+ def initialize(record, fields, identifier)
7
7
  raise "Fields must be specified" unless fields
8
8
 
9
9
  self.record = record
10
10
  self.fields = fields
11
+ self.identifier = identifier
11
12
  end
12
13
 
13
14
  def data
14
- Hash[fields.map do |field|
15
- [field, record.send(field)]
16
- end]
15
+ {
16
+ id: identifier_for_record,
17
+ data: Hash[fields.map do |field|
18
+ [field, record.send(field)]
19
+ end]
20
+ }
21
+ end
22
+
23
+ def identifier_for_record
24
+ puts identifier.class
25
+ if identifier.class == Symbol
26
+ record.send(identifier)
27
+ elsif identifier.class == Proc
28
+ identifier.call(record)
29
+ else
30
+ record.id
31
+ end
17
32
  end
18
33
 
19
34
  def to_json
@@ -22,7 +37,7 @@ module MultipleMan
22
37
 
23
38
  private
24
39
 
25
- attr_accessor :record, :fields
40
+ attr_accessor :record, :fields, :identifier
26
41
 
27
42
  end
28
43
  end
@@ -11,7 +11,8 @@ module MultipleMan
11
11
  @logger ||= defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
12
12
  end
13
13
 
14
- attr_accessor :topic_name, :app_name, :connection, :logger, :enabled
14
+ attr_accessor :topic_name, :app_name, :connection, :enabled
15
+ attr_writer :logger
15
16
  end
16
17
 
17
18
  def self.configuration
@@ -27,13 +27,16 @@ module MultipleMan
27
27
  end
28
28
 
29
29
  def record_data(record)
30
- AttributeExtractor.new(record, fields).to_json
30
+ AttributeExtractor.new(record, fields, identifier).to_json
31
31
  end
32
32
 
33
33
  def fields
34
34
  options[:fields]
35
35
  end
36
36
 
37
+ def identifier
38
+ options[:identifier]
39
+ end
37
40
 
38
41
  end
39
42
  end
@@ -16,17 +16,16 @@ module MultipleMan
16
16
 
17
17
  attr_reader :klass
18
18
 
19
- def create(data)
20
- model = find_model(data)
21
- model.remote_id = data.delete(:id)
22
- model.attributes = data
19
+ def create(payload)
20
+ model = find_model(payload[:id])
21
+ model.attributes = payload[:data]
23
22
  model.save!
24
23
  end
25
24
 
26
25
  alias_method :update, :create
27
26
 
28
- def destroy(data)
29
- model = find_model(data)
27
+ def destroy(payload)
28
+ model = find_model(payload[:id])
30
29
  model.destroy!
31
30
  end
32
31
 
@@ -40,8 +39,8 @@ module MultipleMan
40
39
 
41
40
  private
42
41
 
43
- def find_model(data)
44
- klass.find_by_remote_id(data[:id]) || klass.new
42
+ def find_model(id)
43
+ klass.find_or_initialize_by(multiple_man_identifier: id)
45
44
  end
46
45
 
47
46
  attr_writer :klass
@@ -1,3 +1,3 @@
1
1
  module MultipleMan
2
- VERSION = "0.1.4"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -2,9 +2,11 @@ require 'spec_helper'
2
2
 
3
3
  describe MultipleMan::AttributeExtractor do
4
4
 
5
- MockClass = Struct.new(:a, :b, :c)
6
- let(:object) { MockClass.new(1,2,3) }
7
- subject { described_class.new(object, fields) }
5
+ MockClass = Struct.new(:a, :b, :c, :id)
6
+ let(:object) { MockClass.new(1,2,3,10) }
7
+ subject { described_class.new(object, fields, identifier) }
8
+ let(:fields) { nil }
9
+ let(:identifier) { nil }
8
10
 
9
11
  context "without fields" do
10
12
  it "should not be allowed" do
@@ -14,8 +16,22 @@ describe MultipleMan::AttributeExtractor do
14
16
 
15
17
  context "with fields" do
16
18
  let(:fields) { [:a, :c] }
17
- its(:data) { should == {a: 1, c: 3}}
18
- its(:to_json) { should == '{"a":1,"c":3}'}
19
+ its(:data) { should == {id:10, data:{a: 1, c: 3}}}
20
+ its(:to_json) { should == '{"id":10,"data":{"a":1,"c":3}}'}
21
+ end
22
+
23
+ context "with an identifier" do
24
+ let(:fields) { [:a] }
25
+ context "symbol" do
26
+ let(:identifier) { :a }
27
+ its(:data) { should == {id:1, data:{a: 1}}}
28
+ its(:to_json) { should == '{"id":1,"data":{"a":1}}'}
29
+ end
30
+ context "proc" do
31
+ let(:identifier) { lambda{|record| record.b } }
32
+ its(:data) { should == {id:2, data:{a: 1}}}
33
+ its(:to_json) { should == '{"id":2,"data":{"a":1}}'}
34
+ end
19
35
  end
20
36
 
21
37
  end
@@ -17,6 +17,10 @@ describe MultipleMan::ModelPublisher do
17
17
  "bar"
18
18
  end
19
19
 
20
+ def id
21
+ 10
22
+ end
23
+
20
24
  def model_name
21
25
  OpenStruct.new(singular: "mock_object")
22
26
  end
@@ -30,8 +34,8 @@ describe MultipleMan::ModelPublisher do
30
34
  described_class.new(:create, fields: [:foo]).after_commit(MockObject.new)
31
35
  end
32
36
  it "should send the jsonified version of the model to the correct routing key" do
33
- MultipleMan::AttributeExtractor.any_instance.should_receive(:to_json).and_return('{"foo": "bar"}')
34
- topic_stub.should_receive(:publish).with('{"foo": "bar"}', routing_key: "MockObject.create")
37
+ MultipleMan::AttributeExtractor.any_instance.should_receive(:to_json).and_return('{"id":10,"data":{"foo": "bar"}}')
38
+ topic_stub.should_receive(:publish).with('{"id":10,"data":{"foo": "bar"}}', routing_key: "MockObject.create")
35
39
  described_class.new(:create, fields: [:foo]).after_commit(MockObject.new)
36
40
  end
37
41
  end
@@ -2,8 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe MultipleMan::ModelSubscriber do
4
4
  class MockClass
5
- def remote_id=(value)
6
- end
5
+
7
6
  end
8
7
 
9
8
  describe "register" do
@@ -16,31 +15,18 @@ describe MultipleMan::ModelSubscriber do
16
15
  describe "create" do
17
16
  it "should create a new model" do
18
17
  mock_object = MockClass.new
19
- MockClass.stub(:find_by_remote_id).and_return(nil)
20
- mock_object.should_receive(:attributes=)
21
- mock_object.should_receive(:remote_id=)
22
- MockClass.should_receive(:new).and_return(mock_object)
23
- mock_object.should_receive(:save!)
24
-
25
- MultipleMan::ModelSubscriber.new(MockClass).create({a: 1, b: 2})
26
- end
27
- end
28
-
29
- describe "update" do
30
- it "should find an existing model and update it" do
31
- mock_object = MockClass.new
32
- MockClass.should_receive(:find_by_remote_id).and_return(mock_object)
18
+ MockClass.stub(:find_or_initialize_by).with(multiple_man_identifier: 5).and_return(mock_object)
33
19
  mock_object.should_receive(:attributes=)
34
20
  mock_object.should_receive(:save!)
35
21
 
36
- MultipleMan::ModelSubscriber.new(MockClass).update({id: 5, a: 1, b: 2})
22
+ MultipleMan::ModelSubscriber.new(MockClass).create({id: 5, data:{a: 1, b: 2}})
37
23
  end
38
24
  end
39
25
 
40
26
  describe "destroy" do
41
27
  it "should destroy the model" do
42
28
  mock_object = MockClass.new
43
- MockClass.should_receive(:find_by_remote_id).and_return(mock_object)
29
+ MockClass.should_receive(:find_or_initialize_by).and_return(mock_object)
44
30
  mock_object.should_receive(:destroy!)
45
31
 
46
32
  MultipleMan::ModelSubscriber.new(MockClass).destroy({id: 1})
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: multiple_man
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Brunner