aquasync_model 0.1.2 → 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: cdfc373678e7208e1a2e20ccf3d4f143f3cae718
4
- data.tar.gz: 3fcccdcf5243e914d9df62a6b6a6ef51fb9dc720
3
+ metadata.gz: 5b19f1195106fe8588b7022f7a59d632894f0aa4
4
+ data.tar.gz: 3510b14f212a9f15a9f62fc7b5a72a64273671d7
5
5
  SHA512:
6
- metadata.gz: 1f6103937ac58cbfe06276db4b2e92b162275b375c14a7fc67ca8bc5b362216526ecbded76a3c2c98e0597d6e212fa7c955710b776def67d0e6ff801f9c180d6
7
- data.tar.gz: 0616a7406bcc8d68233aa5cb92e86f536897ae06db764d2577bae6a2954c6b5a98934f2366d9a18ec9fb72cea43c55d1078edc13bd85cd6da1b1f02c578415da
6
+ metadata.gz: 176ebc703f6098f16b4d4e6f8f7fa40fc54b5078b16ab40ae3ac6412f8564f6c72c81c66ad80f181999816e30774c7a3e177730835dc3e61e7a075dd1874c7fb
7
+ data.tar.gz: f2b6bfaceb3ae5b4052f7271a9ddbe4915f61f06bc1a2fbb1987ee8e1c235d60e02e37f6e754a4e6043f95d8bd683b6befbee57a05553d68c4d0ee346629d5f5
@@ -1,4 +1,5 @@
1
1
  require 'active_support/concern'
2
+ require 'active_support/inflector'
2
3
 
3
4
  module Aquasync
4
5
  # Has a responsibility to implement Aquasync::DeltasAggregator requirement.
@@ -18,30 +19,54 @@ module Aquasync
18
19
  module ClassMethods
19
20
  # DeltasAggregator requirement
20
21
  # @return [Array<Aquasync::Base>]
21
- def aq_deltas(ust)
22
- where(:ust.gt => ust)
22
+ def aq_deltas(ust, opts = {})
23
+ begin_of_association_chain(opts).where(:ust.gt => ust)
23
24
  end
24
25
 
25
26
  # DeltasAggregator requirement
26
27
  # @return [NilClass]
27
- def aq_commit_deltas(deltas)
28
- deltas.each {|delta| commit_delta(delta) }
28
+ def aq_commit_deltas(deltas, opts = {})
29
+ deltas.each {|d|
30
+ delta = append_attributes(d, opts)
31
+ commit_delta(delta, opts)
32
+ }
33
+ end
34
+
35
+ # Handle :append_attributes option.
36
+ # @param [Hash]
37
+ def append_attributes(delta, opts)
38
+ return delta unless opts[:append_attributes]
39
+ atr = opts[:append_attributes]
40
+ klass = self.name
41
+ delta.merge!(atr[:all]) if atr[:all]
42
+ delta.merge!(atr[klass]) if atr[klass]
43
+ delta
29
44
  end
30
45
 
31
46
  # commits a delta.
32
47
  # @param [Hash]
33
- def commit_delta(delta)
34
- record = find_by(gid: delta["gid"])
48
+ def commit_delta(delta, opts)
49
+ record = begin_of_association_chain(opts).find_by(gid: delta["gid"])
35
50
  if record
36
51
  record.resolve_conflict(delta)
37
52
  else
38
- create_record_from_delta(delta)
53
+ create_record_from_delta(delta, opts)
39
54
  end
40
55
  end
41
56
 
42
57
  # @param [Hash]
43
- def create_record_from_delta(delta)
44
- create!(delta)
58
+ def create_record_from_delta(delta, opts)
59
+ begin_of_association_chain(opts).create!(delta)
60
+ end
61
+
62
+ def begin_of_association_chain(opts)
63
+ model = opts[:begin_of_association_chain]
64
+ if(model)
65
+ # current_user Book => current_user.books
66
+ model.send(self.name.downcase.pluralize)
67
+ else
68
+ self
69
+ end
45
70
  end
46
71
  end
47
72
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aquasync_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaiinui
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-01 00:00:00.000000000 Z
11
+ date: 2014-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid