synchronisable 0.0.5 → 0.0.6

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: 670f61c0f12b85b365a456735ad8a8c9d67ba37b
4
- data.tar.gz: 2ab99ead8df5badb9fef3ee54d3343d5748826f2
3
+ metadata.gz: 6de60703f28fbed5c2a30a924474691bf98c489e
4
+ data.tar.gz: f5da8cda6005fa932be6ce2a9ecc8eb6a7973053
5
5
  SHA512:
6
- metadata.gz: f929b28fb870bc097bf8c3e28895ed16bebd2f2403cbb4570e6d1b8afcaf729f518116411ecc2f38a13864329510c8b1ab3f09d7ae491fbcc4e44fbe94a880e1
7
- data.tar.gz: 03c0b36a7b6a56022faa4b0adff9b5d6003315a5148778541ff6ba90f65bb57f4826213eaf328ea2974894ccea3b3f2feae543a1680f297323b42c69e722d215
6
+ metadata.gz: 485bd2ca85dcf24fc9ff56cec0442498bfe62cfd523cc220cd2a319fb0d5cde49b574f3e3666af35e126528a46774364a967d0d0e232c8d42f13a3b7687f8c4b
7
+ data.tar.gz: 68d4ff7a28e5b2952b072cf5bbfa1f8d66c124b8c5bf365f8b958968cd390c48632fbbb54793035ede94e4e061519bbb79ca0519ee63dfd6873e3d3a3bfec14f
data/TODO.md CHANGED
@@ -26,3 +26,4 @@ Secondary objectives
26
26
  ======================================
27
27
  - [x] option for verbose logging
28
28
  - [x] colorized STDOUT
29
+ - [ ] actualize docs
@@ -12,6 +12,11 @@ module Synchronisable
12
12
  end
13
13
 
14
14
  module ClassMethods
15
+ def inherited(subclass)
16
+ super
17
+ subclass.associations = {}
18
+ end
19
+
15
20
  [HasOne, HasMany].each do |klass|
16
21
  macro = klass.to_s.demodulize.underscore.to_sym
17
22
  define_method(macro) do |name, options = {}|
@@ -49,7 +54,7 @@ module Synchronisable
49
54
  end
50
55
 
51
56
  def required_associations
52
- self.associations.select { |_, a| a.required }.map(&:key)
57
+ self.associations.select { |_, a| a.required }.keys
53
58
  end
54
59
  end
55
60
  end
@@ -48,6 +48,7 @@ module Synchronisable
48
48
 
49
49
  module ClassMethods
50
50
  def inherited(subclass)
51
+ super
51
52
  class_attributes[subclass] = class_attributes[self].deep_dup
52
53
  end
53
54
 
@@ -1,4 +1,5 @@
1
1
  module Synchronisable
2
+ # @api private
2
3
  class InputDescriptor
3
4
  attr_reader :data
4
5
 
@@ -1,10 +1,11 @@
1
1
  require 'synchronisable/input_descriptor'
2
2
 
3
3
  module Synchronisable
4
- class DataBuilder
4
+ # @api private
5
+ class InputDispatcher
5
6
  class << self
6
- def build(model, synchronizer, data)
7
- new(model, synchronizer).build(data)
7
+ def dispatch(model, synchronizer, data)
8
+ new(model, synchronizer).dispatch(data)
8
9
  end
9
10
  end
10
11
 
@@ -13,17 +14,17 @@ module Synchronisable
13
14
  @synchronizer = synchronizer
14
15
  end
15
16
 
16
- def build(data)
17
+ def dispatch(data)
17
18
  input = InputDescriptor.new(data)
18
19
 
19
- result = case input
20
- when ->(i) { i.empty? }
20
+ result = case
21
+ when input.empty?
21
22
  @synchronizer.fetch
22
- when ->(i) { i.remote_id? }
23
+ when input.remote_id?
23
24
  @synchronizer.find(data)
24
- when ->(i) { i.local_id? }
25
+ when input.local_id?
25
26
  find_by_local_id(data)
26
- when ->(i) { i.array_of_ids? }
27
+ when input.array_of_ids?
27
28
  find_by_array_of_ids(input)
28
29
  else
29
30
  result = data.dup
@@ -44,8 +45,6 @@ module Synchronisable
44
45
  import ? @synchronizer.find(import.remote_id) : nil
45
46
  end
46
47
 
47
- private
48
-
49
48
  def find_imports(class_name, ids)
50
49
  case class_name
51
50
  when 'Fixnum'
@@ -41,13 +41,13 @@ module Synchronisable
41
41
  def set_defaults(args)
42
42
  options = args.extract_options!
43
43
 
44
- self.synchronizer = args.first ||
45
- options[:synchronizer] || default_synchronizer
44
+ self.synchronizer = args.first || options[:synchronizer] ||
45
+ find_synchronizer || SynchronizerDefault
46
46
  end
47
47
 
48
- def default_synchronizer
48
+ def find_synchronizer
49
49
  const_name = "#{self.name.demodulize}#{SYNCHRONIZER_SUFFIX}"
50
- const_name.safe_constantize || SynchronizerDefault
50
+ const_name.safe_constantize
51
51
  end
52
52
  end
53
53
  end
@@ -31,7 +31,7 @@ module Synchronisable
31
31
  end
32
32
 
33
33
  @import_record = Import.find_by(
34
- :remote_id => @remote_id,
34
+ :remote_id => @remote_id.to_s,
35
35
  :synchronisable_type => @model
36
36
  )
37
37
 
@@ -51,7 +51,7 @@ module Synchronisable
51
51
  @import_record = Import.create!(
52
52
  :synchronisable_id => record.id,
53
53
  :synchronisable_type => @model.to_s,
54
- :remote_id => @remote_id,
54
+ :remote_id => @remote_id.to_s,
55
55
  :attrs => @local_attrs
56
56
  )
57
57
  end
@@ -110,10 +110,6 @@ module Synchronisable
110
110
  data.present? ? data : gateway_instance.try(:find, id)
111
111
  end
112
112
 
113
- def gateway_instance
114
- @gateway_instance ||= gateway.try(:new, self)
115
- end
116
-
117
113
  # Extracts remote id from given attribute hash.
118
114
  #
119
115
  # @param attrs [Hash] remote attributes
@@ -165,12 +161,16 @@ module Synchronisable
165
161
  #
166
162
  # @raise [MissedRemoteIdError] raised when data doesn't contain remote id
167
163
  def ensure_remote_id(id)
168
- return id if id.present?
164
+ return id.to_s if id.present?
169
165
  raise MissedRemoteIdError, I18n.t(
170
166
  'errors.missed_remote_id',
171
167
  remote_id: remote_id
172
168
  )
173
169
  end
170
+
171
+ def gateway_instance
172
+ @gateway_instance ||= gateway.try(:new, self)
173
+ end
174
174
  end
175
175
  end
176
176
  end
@@ -5,6 +5,8 @@ module Synchronisable
5
5
  # model specific synchronizer is not defined.
6
6
  #
7
7
  # @api private
8
+ #
9
+ # @see Synchronisable::Synchronizer
8
10
  class SynchronizerDefault < Synchronizer
9
11
  end
10
12
  end
@@ -2,7 +2,7 @@ module Synchronisable
2
2
  module VERSION
3
3
  MAJOR = 0
4
4
  MINOR = 0
5
- PATCH = 5
5
+ PATCH = 6
6
6
  SUFFIX = nil
7
7
 
8
8
  STRING = [MAJOR, MINOR, PATCH, SUFFIX].compact.join('.')
@@ -2,7 +2,7 @@ require 'colorize'
2
2
 
3
3
  require 'synchronisable/error_handler'
4
4
  require 'synchronisable/context'
5
- require 'synchronisable/data_builder'
5
+ require 'synchronisable/input_dispatcher'
6
6
  require 'synchronisable/source'
7
7
  require 'synchronisable/models/import'
8
8
 
@@ -45,7 +45,7 @@ module Synchronisable
45
45
  error_handler = ErrorHandler.new(@logger, context)
46
46
  context.before = @model.imports_count
47
47
 
48
- hashes = DataBuilder.build(@model, @synchronizer, data)
48
+ hashes = InputDispatcher.dispatch(@model, @synchronizer, data)
49
49
  hashes.each do |attrs|
50
50
  source = Source.new(@model, @parent, attrs)
51
51
  error_handler.handle(source) do
@@ -14,6 +14,7 @@ require 'synchronisable/version'
14
14
  require 'synchronisable/models/import'
15
15
  require 'synchronisable/synchronizer'
16
16
  require 'synchronisable/model'
17
+ require 'synchronisable/gateway'
17
18
 
18
19
  locale_paths = File.join(File.dirname(__FILE__),
19
20
  'synchronisable', 'locale', '*.yml')
@@ -34,7 +35,7 @@ I18n.available_locales = [:en, :ru]
34
35
  # Match.sync(:include => {
35
36
  # :match_players => :player
36
37
  # })
37
- # Model.sync([id1, ..., idn])
38
+ # + Model.sync([id1, ..., idn])
38
39
  #
39
40
  # Model.where(condition).sync
40
41
  # Match.where(condition).sync(:include => {
@@ -1,5 +1,4 @@
1
1
  require 'spec_helper'
2
- require 'pry-byebug'
3
2
 
4
3
  describe Team do
5
4
  describe 'synchronization' do
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe Synchronisable::DSL::Macro do
4
4
  describe 'commom attributes' do
5
- subject { HasMacro }
5
+ subject { HasMacroFoo }
6
6
 
7
7
  before do
8
8
  subject.xyz 7
@@ -15,6 +15,9 @@ describe Synchronisable::DSL::Macro do
15
15
  it { respond_to? :xxx }
16
16
  it { respond_to? :arr }
17
17
 
18
+ it { should_not respond_to? :baz }
19
+ it { should_not respond_to? :sqr2 }
20
+
18
21
  its(:foo) { should be nil }
19
22
  its(:bar) { should eq(1) }
20
23
  its(:baz) { should eq(2) }
@@ -24,11 +27,14 @@ describe Synchronisable::DSL::Macro do
24
27
 
25
28
  describe 'attribute with default value lambda that raises an error' do
26
29
  it 'raises error' do
27
- expect { HasMacro.carefull }.to raise_error(NotImplementedError)
30
+ expect { HasMacroFoo.carefull }.to raise_error(NotImplementedError)
28
31
  end
29
32
 
30
33
  context 'when overriden in subclass not to raise an error' do
31
- subject { HasMacroSubclass }
34
+ subject { HasMacroFooSubclass }
35
+
36
+ it { should_not respond_to? :baz }
37
+ it { should_not respond_to? :sqr2 }
32
38
 
33
39
  it { respond_to? :foo }
34
40
  it { respond_to? :bar }
@@ -37,17 +43,17 @@ describe Synchronisable::DSL::Macro do
37
43
  its(:bar) { should eq(1) }
38
44
 
39
45
  it 'not raises error' do
40
- expect { HasMacroSubclass.carefull }.to_not raise_error
46
+ expect { HasMacroFooSubclass.carefull }.to_not raise_error
41
47
  end
42
48
 
43
49
  it 'equals to the new default value' do
44
- expect(HasMacroSubclass.carefull).to eq(0)
50
+ expect(HasMacroFooSubclass.carefull).to eq(0)
45
51
  end
46
52
  end
47
53
  end
48
54
 
49
55
  describe 'methods' do
50
- subject { HasMacroSubclass }
56
+ subject { HasMacroFooSubclass }
51
57
 
52
58
  it { respond_to? :sqr }
53
59
  its(:sqr) { respond_to? :call }
@@ -56,4 +62,15 @@ describe Synchronisable::DSL::Macro do
56
62
  expect(subject.sqr.(2)).to eq(4)
57
63
  end
58
64
  end
65
+
66
+ describe 'class attributes' do
67
+ subject { HasMacroBar }
68
+
69
+ it { should_not respond_to? :bar }
70
+ it { should_not respond_to? :foo }
71
+ it { should_not respond_to? :sqr }
72
+
73
+ it { respond_to? :baz }
74
+ it { respond_to? :sqr2 }
75
+ end
59
76
  end
@@ -0,0 +1,6 @@
1
+ class HasMacroBar
2
+ include Synchronisable::DSL::Macro
3
+
4
+ attribute :baz
5
+ method :sqr2
6
+ end
@@ -1,4 +1,4 @@
1
- class HasMacro
1
+ class HasMacroFoo
2
2
  include Synchronisable::DSL::Macro
3
3
 
4
4
  attribute :bar, default: 1
@@ -0,0 +1,9 @@
1
+ require_relative 'has_macro_foo'
2
+
3
+ class HasMacroFooSubclass < HasMacroFoo
4
+ attribute :carefull, default: 0
5
+
6
+ sqr do |x|
7
+ x * x
8
+ end
9
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: synchronisable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vasiliy Yorkin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-21 00:00:00.000000000 Z
11
+ date: 2014-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -308,7 +308,6 @@ files:
308
308
  - lib/synchronisable.rb
309
309
  - lib/synchronisable/attribute_mapper.rb
310
310
  - lib/synchronisable/context.rb
311
- - lib/synchronisable/data_builder.rb
312
311
  - lib/synchronisable/dsl/associations.rb
313
312
  - lib/synchronisable/dsl/associations/association.rb
314
313
  - lib/synchronisable/dsl/associations/has_many.rb
@@ -321,6 +320,7 @@ files:
321
320
  - lib/synchronisable/exceptions.rb
322
321
  - lib/synchronisable/gateway.rb
323
322
  - lib/synchronisable/input_descriptor.rb
323
+ - lib/synchronisable/input_dispatcher.rb
324
324
  - lib/synchronisable/locale/en.yml
325
325
  - lib/synchronisable/locale/ru.yml
326
326
  - lib/synchronisable/model.rb
@@ -417,8 +417,9 @@ files:
417
417
  - spec/spec_helper.rb
418
418
  - spec/synchronisable/dsl/macro_spec.rb
419
419
  - spec/synchronisable/models/import_spec.rb
420
- - spec/synchronisable/support/has_macro.rb
421
- - spec/synchronisable/support/has_macro_subclass.rb
420
+ - spec/synchronisable/support/has_macro_bar.rb
421
+ - spec/synchronisable/support/has_macro_foo.rb
422
+ - spec/synchronisable/support/has_macro_foo_subclass.rb
422
423
  - spec/synchronisable/support/shared/contexts.rb
423
424
  - spec/synchronisable/support/shared/examples.rb
424
425
  - spec/synchronisable/synchronisable_spec.rb
@@ -535,8 +536,9 @@ test_files:
535
536
  - spec/spec_helper.rb
536
537
  - spec/synchronisable/dsl/macro_spec.rb
537
538
  - spec/synchronisable/models/import_spec.rb
538
- - spec/synchronisable/support/has_macro.rb
539
- - spec/synchronisable/support/has_macro_subclass.rb
539
+ - spec/synchronisable/support/has_macro_bar.rb
540
+ - spec/synchronisable/support/has_macro_foo.rb
541
+ - spec/synchronisable/support/has_macro_foo_subclass.rb
540
542
  - spec/synchronisable/support/shared/contexts.rb
541
543
  - spec/synchronisable/support/shared/examples.rb
542
544
  - spec/synchronisable/synchronisable_spec.rb
@@ -1,9 +0,0 @@
1
- require_relative 'has_macro'
2
-
3
- class HasMacroSubclass < HasMacro
4
- attribute :carefull, default: 0
5
-
6
- sqr do |x|
7
- x * x
8
- end
9
- end