synced 1.0.7 → 1.0.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7aa93a0045e49e35ef45f18f16afcd94d9e0bd24
4
- data.tar.gz: 699a8ae50503c3bb5467dacc47084ce59f849c92
3
+ metadata.gz: c751562a0b6773e1685c936cf9461f6e1672b993
4
+ data.tar.gz: b1620c76e516e4e6334c0b968196e9d731550f1a
5
5
  SHA512:
6
- metadata.gz: 539bad5fcea8a7499960b79168d83196658a3479e953486fa70240c75e89079bf4bb3a1f12802d8edaee653fc66799cc6547db2f6a1f830fc404c4ddd1baa103
7
- data.tar.gz: 4d1c9bb509766abbb41a245522b150442338f25ef23901650378a239709cb1c384fd14ed090238ff085ca6eaf2c341918c53f2476ba05e009d933608c74b7f2b
6
+ metadata.gz: 7bc982cf7b18f570fd5e0879306a94e322ccff6aa049325621538630db503edc71e59352428d56ed28db4289b7814a146bd80ee1488f453ead1056d09b8969e7
7
+ data.tar.gz: 1c4855bd6caaeeebc113e877724c2556c29775612bc911e199e09813b6fa71618bfde5322cc27cca46cbfbd6751a7f7d252a46ae33c1e189e50d43da3ed7c740
data/README.md CHANGED
@@ -128,7 +128,7 @@ will be assigned value of `size` attribute of the remote object.
128
128
 
129
129
  ### Local attributes with mapping blocks
130
130
 
131
- If you want to convert an attributes value during synchronization you can
131
+ If you want to convert attribute's value during synchronization you can
132
132
  pass a block as value in the mapping hash. Block will receive remote object
133
133
  as the only argument.
134
134
 
@@ -358,6 +358,34 @@ This can be overwritten in synchronize method.
358
358
  Photo.synchronize(fields: [:name, :size])
359
359
  ```
360
360
 
361
+ ## Delegate attributes
362
+
363
+ You can delegate attributes from your synced model to `synced_data` Hash for easier access to
364
+ synchronized data.
365
+
366
+ ```ruby
367
+ class Photo < ActiveRecord::Base
368
+ synced delegate_attributes: [:name]
369
+ end
370
+ ```
371
+
372
+ Now you can fetch photo name using:
373
+
374
+ ```ruby
375
+ @photo.name #=> "Sunny morning"
376
+ ```
377
+
378
+ If you want to access synced attribute with different name, you can pass a Hash:
379
+
380
+ ```ruby
381
+ class Photo < ActiveRecord::Base
382
+ synced delegate_attributes: {title: :name}
383
+ end
384
+
385
+ keys are delegated attributes' names and values are keys on synced data Hash. This is a simpler
386
+ version of `delegate :name, to: :synced_data` which works with Hash reserved attributes names, like
387
+ `:zip`, `:map`.
388
+
361
389
  ## Synced configuration options
362
390
 
363
391
  Option name | Default value | Description | synced | synchronize |
@@ -372,6 +400,7 @@ Option name | Default value | Description
372
400
  `:include` | `[]` | [An array of associations to be fetched](#including-associations-in-synced_data) | YES | YES |
373
401
  `:fields` | `[]` | [An array of fields to be fetched](#selecting-fields-to-be-synchronized) | YES | YES |
374
402
  `:remote` | `nil` | [Remote objects to be synchronized with local ones](#synchronization-of-given-remote-objects) | NO | YES |
403
+ `:delegate_attributes`| `[]` | [Define delegators to synced data Hash attributes](#delegate-attributes) | YES | NO |
375
404
 
376
405
  ## Documentation
377
406
 
@@ -0,0 +1,11 @@
1
+ module Synced
2
+ module AttributesAsHash
3
+ # On a Hash returns the same Hash
4
+ # On an Array returns a Hash with identical corresponding keys and values
5
+ # Used for mapping local - remote attributes
6
+ def synced_attributes_as_hash(attributes)
7
+ return attributes if attributes.is_a?(Hash)
8
+ Hash[Array(attributes).map { |name| [name, name] }]
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,16 @@
1
+ require 'synced/attributes_as_hash'
2
+
3
+ module Synced
4
+ module DelegateAttributes
5
+ extend ActiveSupport::Concern
6
+ included do
7
+ synced_attributes_as_hash(synced_delegate_attributes).each do |key, value|
8
+ define_method(key) { send(synced_data_key)[value] }
9
+ end
10
+ end
11
+
12
+ module ClassMethods
13
+ include Synced::AttributesAsHash
14
+ end
15
+ end
16
+ end
data/lib/synced/model.rb CHANGED
@@ -32,15 +32,18 @@ module Synced
32
32
  # @option options [Time|Proc] initial_sync_since: A point in time from which
33
33
  # objects will be synchronized on first synchronization.
34
34
  # Works only for partial (updated_since param) synchronizations.
35
+ # @option options [Array|Hash] delegate_attributes: Given attributes will be defined
36
+ # on synchronized object and delegated to synced_data Hash
35
37
  def synced(options = {})
36
38
  options.symbolize_keys!
37
39
  options.assert_valid_keys(:associations, :data_key, :fields,
38
40
  :globalized_attributes, :id_key, :include, :initial_sync_since,
39
- :local_attributes, :mapper, :only_updated, :remove, :synced_all_at_key)
41
+ :local_attributes, :mapper, :only_updated, :remove, :synced_all_at_key,
42
+ :delegate_attributes)
40
43
  class_attribute :synced_id_key, :synced_all_at_key, :synced_data_key,
41
44
  :synced_local_attributes, :synced_associations, :synced_only_updated,
42
45
  :synced_mapper, :synced_remove, :synced_include, :synced_fields,
43
- :synced_globalized_attributes, :synced_initial_sync_since
46
+ :synced_globalized_attributes, :synced_initial_sync_since, :synced_delegate_attributes
44
47
  self.synced_id_key = options.fetch(:id_key, :synced_id)
45
48
  self.synced_all_at_key = options.fetch(:synced_all_at_key,
46
49
  synced_column_presence(:synced_all_at))
@@ -58,6 +61,8 @@ module Synced
58
61
  [])
59
62
  self.synced_initial_sync_since = options.fetch(:initial_sync_since,
60
63
  nil)
64
+ self.synced_delegate_attributes = options.fetch(:delegate_attributes, [])
65
+ include Synced::DelegateAttributes
61
66
  include Synced::HasSyncedData
62
67
  end
63
68
 
@@ -128,3 +133,4 @@ module Synced
128
133
  end
129
134
  end
130
135
  end
136
+
@@ -1,7 +1,10 @@
1
+ require 'synced/delegate_attributes'
2
+ require 'synced/attributes_as_hash'
1
3
  # Synchronizer class which performs actual synchronization between
2
4
  # local database and given array of remote objects
3
5
  module Synced
4
6
  class Synchronizer
7
+ include AttributesAsHash
5
8
  attr_reader :id_key
6
9
 
7
10
  # Initializes a new Synchronizer
@@ -51,7 +54,7 @@ module Synced
51
54
  @remove = options[:remove]
52
55
  @only_updated = options[:only_updated]
53
56
  @include = options[:include]
54
- @local_attributes = attributes_as_hash(options[:local_attributes])
57
+ @local_attributes = synced_attributes_as_hash(options[:local_attributes])
55
58
  @api = options[:api]
56
59
  @mapper = options[:mapper].respond_to?(:call) ?
57
60
  options[:mapper].call : options[:mapper]
@@ -60,7 +63,7 @@ module Synced
60
63
  @associations = Array(options[:associations])
61
64
  @perform_request = options[:remote].nil?
62
65
  @remote_objects = Array(options[:remote]) unless @perform_request
63
- @globalized_attributes = attributes_as_hash(options[:globalized_attributes])
66
+ @globalized_attributes = synced_attributes_as_hash(options[:globalized_attributes])
64
67
  @initial_sync_since = options[:initial_sync_since]
65
68
  end
66
69
 
@@ -240,11 +243,6 @@ module Synced
240
243
  Synced.instrumenter.instrument(*args, &block)
241
244
  end
242
245
 
243
- def attributes_as_hash(attributes)
244
- return attributes if attributes.is_a?(Hash)
245
- Hash[Array(attributes).map { |name| [name, name] }]
246
- end
247
-
248
246
  class MissingAPIClient < StandardError
249
247
  def initialize(scope, model_class)
250
248
  @scope = scope
@@ -1,3 +1,3 @@
1
1
  module Synced
2
- VERSION = "1.0.7"
2
+ VERSION = "1.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: 1.0.7
4
+ version: 1.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-11-13 00:00:00.000000000 Z
12
+ date: 2014-12-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -162,6 +162,8 @@ files:
162
162
  - README.md
163
163
  - Rakefile
164
164
  - lib/synced.rb
165
+ - lib/synced/attributes_as_hash.rb
166
+ - lib/synced/delegate_attributes.rb
165
167
  - lib/synced/has_synced_data.rb
166
168
  - lib/synced/model.rb
167
169
  - lib/synced/rails.rb