motion_bindable 0.2.2 → 0.2.3

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: d5c897c0eb305bd4f7b5ffd32cf03c647c02f225
4
- data.tar.gz: cd0bf8cc88ac483a0296e0fc0399cf157dd77e32
3
+ metadata.gz: ca57bb625e112f16ca5235ede05268ebba35a898
4
+ data.tar.gz: 0295b1de273b684a0542201d3e8d5c206fa46a82
5
5
  SHA512:
6
- metadata.gz: be96fcb5c62954e620367ebf7318eaa3aa2ea5fc9367d375aa6a472ada78e5f2e3d4b7de5c7f753837f741acfa350a604fe177c60f6e9f53e8b8f6dc57ad44b1
7
- data.tar.gz: 523280e1eb706f1b561e9e81f52512537a49e3e824487ef2ec896eeaf41f739f3bd6f3267fbf2266bf85b07ccc4a51df32ddf44e34961b4fac17ecaab09bbc7b
6
+ metadata.gz: 6e70e15e59ad61a1fc6f007d1b2023a11c279f5980c1f81d5148f6968c39fa76a66d73d6395bcd5606dcb5c27ec4c3d2c0ee60d401ec5a322f352f08a0903b48
7
+ data.tar.gz: eac62bffcf0e08564602b3399b51a1d1734d548fee82cbb2671ed6ff1c9fd6747cb617fcada65a21417c85ede1b259652160f7edcb3ba4e7e9f125a171018d74
@@ -18,6 +18,7 @@ module MotionBindable
18
18
 
19
19
  attr_accessor :object
20
20
  attr_accessor :bound
21
+ attr_reader :attr_name
21
22
 
22
23
  def initialize(object, attr_name)
23
24
  @attr_name = attr_name.to_sym
@@ -39,10 +40,6 @@ module MotionBindable
39
40
  self
40
41
  end
41
42
 
42
- def refresh_object
43
- attribute
44
- end
45
-
46
43
  def unbind
47
44
  @watching = nil
48
45
  end
@@ -50,22 +47,22 @@ module MotionBindable
50
47
  private # Methods to leave alone
51
48
 
52
49
  def attribute
53
- object.send(@attr_name)
50
+ object.send(attr_name)
54
51
  end
55
52
 
56
53
  def attribute=(value)
57
- object.send(:"#{@attr_name.to_s}=", value)
54
+ object.send(:"#{attr_name.to_s}=", value)
58
55
  end
59
56
 
60
57
  def initial_state
61
- if attribute.nil?
58
+ if attribute.nil? && respond_to?(:on_bound_change)
62
59
  if respond_to?(:refresh_bound) then on_bound_change(refresh_bound)
63
60
  else on_bound_change
64
- end if respond_to?(:on_bound_change)
65
- else
61
+ end
62
+ elsif respond_to?(:on_object_change)
66
63
  if respond_to?(:refresh_object) then on_object_change(refresh_object)
67
64
  else on_object_change(attribute)
68
- end if respond_to?(:on_object_change)
65
+ end
69
66
  end
70
67
  end
71
68
 
@@ -78,7 +75,7 @@ module MotionBindable
78
75
  end
79
76
  if respond_to?(:start_observing_object) then start_observing_object
80
77
  elsif respond_to?(:refresh_object) && respond_to?(:on_object_change)
81
- sides << object
78
+ sides << :object
82
79
  end
83
80
 
84
81
  watch(sides)
@@ -1,3 +1,3 @@
1
1
  module MotionBindable
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  end
@@ -6,6 +6,10 @@ module MotionBindable::Strategies
6
6
  bound.call
7
7
  end
8
8
 
9
+ def refresh_object
10
+ attribute
11
+ end
12
+
9
13
  def on_bound_change(new = nil)
10
14
  self.attribute = new || bound.call
11
15
  end
@@ -5,6 +5,10 @@ module MotionBindable::Strategies
5
5
  MotionBindable::Strategies::UITextField,
6
6
  ::UITextField
7
7
  )
8
+ ::MotionBindable::Strategy.register_strategy(
9
+ MotionBindable::Strategies::UILabel,
10
+ ::UILabel
11
+ )
8
12
  ::MotionBindable::Strategy.register_strategy(
9
13
  MotionBindable::Strategies::Proc,
10
14
  ::Proc
@@ -0,0 +1,26 @@
1
+ module MotionBindable::Strategies
2
+
3
+ class UILabel < ::MotionBindable::Strategy
4
+
5
+ def start_observing_object
6
+ # Observe the attribute
7
+ object.addObserver(
8
+ self,
9
+ forKeyPath: attr_name,
10
+ options: NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld,
11
+ context: nil
12
+ )
13
+ end
14
+
15
+ def on_object_change(new = nil)
16
+ @bound.text = (new || attribute)
17
+ @bound.setNeedsDisplay
18
+ end
19
+
20
+ def observeValueForKeyPath(_, ofObject: _, change: _, context: _)
21
+ on_object_change
22
+ end
23
+
24
+ end
25
+
26
+ end
@@ -22,11 +22,11 @@ module MotionBindable::Strategies
22
22
  end
23
23
 
24
24
  def on_bound_change(new = nil)
25
- self.attribute = new || bound.text
25
+ self.attribute = (new || bound.text)
26
26
  end
27
27
 
28
28
  def on_object_change(new = nil)
29
- @bound.text = new || attribute
29
+ @bound.text = (new || attribute)
30
30
  end
31
31
 
32
32
  def unbind
@@ -32,8 +32,7 @@ describe 'MotionBindable::Strategy' do
32
32
  @object = ObjectOne.new
33
33
  @bound = Object.new
34
34
  @strategy = Strategy.new(@object, :attribute)
35
- @strategy.stub!(:watch_bound)
36
- @strategy.stub!(:watch_object)
35
+ @strategy.stub!(:watch)
37
36
  end
38
37
 
39
38
  describe '#bind' do
@@ -0,0 +1,67 @@
1
+ class FakeModel
2
+ include MotionBindable::Bindable
3
+ attr_accessor :nested
4
+ attr_accessor :attribute
5
+ def attribute
6
+ @attribute
7
+ end
8
+ end
9
+
10
+ describe 'MotionBindable::Strategies::UILabel' do
11
+
12
+ before do
13
+ MotionBindable::Strategy.register_strategy(
14
+ MotionBindable::Strategies::UILabel,
15
+ UILabel
16
+ )
17
+
18
+ @label = UILabel.alloc.initWithFrame [[110, 60], [100, 26]]
19
+ @label2 = UILabel.alloc.initWithFrame [[110, 60], [100, 26]]
20
+ @object = FakeModel.new
21
+ @object.nested = FakeModel.new
22
+ end
23
+
24
+ context 'attribute is not set yet' do
25
+
26
+ before do
27
+ @object.bind_attributes({
28
+ attribute: @label,
29
+ nested: { attribute: @label2 }
30
+ })
31
+ end
32
+
33
+ it 'should update the label on change' do
34
+ @object.attribute = 'one'
35
+ @object.nested.attribute = 'two'
36
+ @label.text.should.equal 'one'
37
+ @label2.text.should.equal'two'
38
+ end
39
+
40
+ end
41
+
42
+ context 'attribute is set first' do
43
+
44
+ before do
45
+ @object.attribute = 'test'
46
+ @object.nested.attribute = 'test2'
47
+ @object.bind_attributes({
48
+ attribute: @label,
49
+ nested: { attribute: @label2 }
50
+ })
51
+ end
52
+
53
+ it 'should set the label on bind' do
54
+ @label.text.should.equal 'test'
55
+ @label2.text.should.equal 'test2'
56
+ end
57
+
58
+ it 'should change the label when the attribute changes' do
59
+ @object.attribute = 'changed'
60
+ @object.nested.attribute = 'changed2'
61
+ @label.text.should.equal 'changed'
62
+ @label2.text.should.equal 'changed2'
63
+ end
64
+
65
+ end
66
+
67
+ end
@@ -15,8 +15,8 @@ describe 'MotionBindable::Strategies::UITextField' do
15
15
  UITextField
16
16
  )
17
17
 
18
- @app = UIApplication.sharedApplication
19
18
  @text_field = UITextField.alloc.initWithFrame [[110, 60], [100, 26]]
19
+ @text_field2 = UITextField.alloc.initWithFrame [[110, 60], [100, 26]]
20
20
  end
21
21
 
22
22
  context 'nested model' do
@@ -26,6 +26,26 @@ describe 'MotionBindable::Strategies::UITextField' do
26
26
  @object.nested = FakeModel.new
27
27
  end
28
28
 
29
+ context 'attribute set and bound' do
30
+
31
+ before do
32
+ @object.attribute = 'one'
33
+ @object.nested.attribute = 'two'
34
+ @object.bind_attributes({
35
+ attribute: @text_field,
36
+ nested: {
37
+ attribute: @text_field2
38
+ }
39
+ })
40
+ end
41
+
42
+ it 'should update the text field' do
43
+ @text_field.text.should.equal 'one'
44
+ @text_field2.text.should.equal 'two'
45
+ end
46
+
47
+ end
48
+
29
49
  context 'text set and then bound' do
30
50
  before do
31
51
  @text_field.text = 'Just testing.'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: motion_bindable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Kot
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-23 00:00:00.000000000 Z
11
+ date: 2013-12-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A simple data binding library for RubyMotion.
14
14
  email:
@@ -32,14 +32,16 @@ files:
32
32
  - lib/motion_bindable/version.rb
33
33
  - lib/strategies/proc.rb
34
34
  - lib/strategies/strategies.rb
35
+ - lib/strategies/ui_label.rb
35
36
  - lib/strategies/ui_text_field.rb
36
37
  - motion_bindable.gemspec
37
38
  - resources/Default-568h@2x.png
38
39
  - spec/bindable_spec.rb
39
40
  - spec/helpers/facon.rb
40
- - spec/proc_strategy_spec.rb
41
- - spec/strategy_spec.rb
42
- - spec/ui_text_field_strategy_spec.rb
41
+ - spec/strategies/proc_spec.rb
42
+ - spec/strategies/strategy_spec.rb
43
+ - spec/strategies/ui_label_spec.rb
44
+ - spec/strategies/ui_text_field_spec.rb
43
45
  homepage: https://github.com/nathankot/motion-bindable
44
46
  licenses:
45
47
  - MIT
@@ -67,6 +69,7 @@ summary: Inspired by RivetsJS
67
69
  test_files:
68
70
  - spec/bindable_spec.rb
69
71
  - spec/helpers/facon.rb
70
- - spec/proc_strategy_spec.rb
71
- - spec/strategy_spec.rb
72
- - spec/ui_text_field_strategy_spec.rb
72
+ - spec/strategies/proc_spec.rb
73
+ - spec/strategies/strategy_spec.rb
74
+ - spec/strategies/ui_label_spec.rb
75
+ - spec/strategies/ui_text_field_spec.rb