synced 0.0.7 → 0.0.8

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: ac4ba6fd11df9ce17296c4afbfb4740cfb919035
4
- data.tar.gz: 390c0578545827b5a88169ae09535c917897196a
3
+ metadata.gz: d2786ec502b4ef2aba2596f7928997723db04afa
4
+ data.tar.gz: ef2db43fbd30aa1b8be1e4705a3fbf126879b820
5
5
  SHA512:
6
- metadata.gz: f3109f17e56fbd5fa37ffeb6655a963ffd584e0af775bf5b7bfe98c1b64f99785e47892d2b57b775774d1e6bfc909e19a0d9881349e5d6a15e6df5fa587d1ad3
7
- data.tar.gz: c94a411274efb0e4f94c03675dc66bbdcc4caef474ad108b0759be7f79f50e1289f4ae6ece55ce16324e700f443cd21e9203302f84ed223714c7fbd4d03e6b22
6
+ metadata.gz: 74cde200cbe7d2cc2f26ff015f4be75c491c27c81599099f09bd262196c91e5cfaf941d69498ac73dd94c0e99ff534288c8ed5994608dba74e94adb936e3b2ee
7
+ data.tar.gz: e86f5074aab4550788db4454a64254e7858fe2e8ed7edce07166a42c8299284420b1cdedfd3fb4e36f6758432b4a2b9b7a7e7a829e0dd3221f334665fc3b7d8b
data/lib/synced/model.rb CHANGED
@@ -20,14 +20,17 @@ module Synced
20
20
  # object which will be mapped to local object attributes.
21
21
  def synced(options = {})
22
22
  class_attribute :synced_id_key, :synced_all_at_key, :synced_data_key,
23
- :synced_local_attributes, :synced_associations, :synced_only_updated
23
+ :synced_local_attributes, :synced_associations, :synced_only_updated,
24
+ :synced_mapper_module
24
25
  self.synced_id_key = options.fetch(:id_key, :synced_id)
25
26
  self.synced_all_at_key = options.fetch(:synced_all_at_key,
26
- :synced_all_at)
27
- self.synced_data_key = options.fetch(:data_key, :synced_data)
27
+ synced_column_presence(:synced_all_at))
28
+ self.synced_data_key = options.fetch(:data_key,
29
+ synced_column_presence(:synced_data))
28
30
  self.synced_local_attributes = options.fetch(:local_attributes, [])
29
31
  self.synced_associations = options.fetch(:associations, [])
30
32
  self.synced_only_updated = options.fetch(:only_updated, false)
33
+ self.synced_mapper_module = options.fetch(:mapper, nil)
31
34
  include Synced::HasSyncedData
32
35
  end
33
36
 
@@ -71,10 +74,17 @@ module Synced
71
74
  associations: synced_associations,
72
75
  only_updated: synced_only_updated,
73
76
  include: include,
74
- api: api
77
+ api: api,
78
+ mapper: synced_mapper_module
75
79
  }
76
80
  synchronizer = Synced::Synchronizer.new(remote, model_class, options)
77
81
  synchronizer.perform
78
82
  end
83
+
84
+ private
85
+
86
+ def synced_column_presence(name)
87
+ name if column_names.include?(name.to_s)
88
+ end
79
89
  end
80
90
  end
@@ -35,6 +35,8 @@ module Synced
35
35
  # @option options [Boolean] only_updated: If true requests to API will take
36
36
  # advantage of updated_since param and fetch only created/changed/deleted
37
37
  # remote objects
38
+ # @option options [Module] mapper: Module class which will be used for
39
+ # mapping remote objects attributes into local object attributes
38
40
  def initialize(remote_objects, model_class, options = {})
39
41
  @model_class = model_class
40
42
  @scope = options[:scope]
@@ -46,6 +48,7 @@ module Synced
46
48
  @include = options[:include]
47
49
  @local_attributes = options[:local_attributes]
48
50
  @api = options[:api]
51
+ @mapper = options[:mapper]
49
52
  @associations = Array(options[:associations])
50
53
  @remote_objects = Array(remote_objects) if remote_objects
51
54
  @request_performed = false
@@ -56,6 +59,7 @@ module Synced
56
59
  remove_relation.send(remove_strategy) if @remove
57
60
 
58
61
  remote_objects.map do |remote|
62
+ remote.extend(@mapper) if @mapper
59
63
  local_object = local_object_by_remote_id(remote.id) || relation_scope.new
60
64
  local_object.attributes = default_attributes_mapping(remote)
61
65
  local_object.attributes = local_attributes_mapping(remote)
@@ -81,11 +85,11 @@ module Synced
81
85
  if @local_attributes.is_a?(Hash)
82
86
  Hash[
83
87
  @local_attributes.map do |k, v|
84
- [k, v.respond_to?(:call) ? v.call(remote) : remote[v]]
88
+ [k, v.respond_to?(:call) ? v.call(remote) : remote.send(v)]
85
89
  end
86
90
  ]
87
91
  else
88
- Hash[Array(@local_attributes).map { |k| [k, remote[k]] }]
92
+ Hash[Array(@local_attributes).map { |k| [k, remote.send(k)] }]
89
93
  end
90
94
  end
91
95
 
@@ -1,3 +1,3 @@
1
1
  module Synced
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: synced
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sebastien Grosjean
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-07-30 00:00:00.000000000 Z
12
+ date: 2014-08-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails