motion_bindable 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7ef10a54e768eb79857d0bd5539519a6b90932e0
4
+ data.tar.gz: cf500b93047492d2fb8dfbeba5c9b70e386c6f29
5
+ SHA512:
6
+ metadata.gz: 3ff1166dc058c57b9fa4adaea29eef1a328e0061c49a6d831edf0986a5aed0486066988471f17f5581f658ee58489c927b6c7381643bfc1b101f57df765edd1e
7
+ data.tar.gz: bda1bc7c69f24ca5fe4046cf4cc4dce45a66cc3a8160d76459f445eea30448d63d4954b32c1c75f1c7fb6339a61a4e63155e7d1ae3432654be23a00dec036fae
data/.gitignore ADDED
@@ -0,0 +1,32 @@
1
+ .repl_history
2
+ build
3
+ tags
4
+ app/pixate_code.rb
5
+ resources/*.nib
6
+ resources/*.momd
7
+ resources/*.storyboardc
8
+ .DS_Store
9
+ nbproject
10
+ .redcar
11
+ #*#
12
+ *~
13
+ *.sw[po]
14
+ .eprj
15
+ .sass-cache
16
+ .idea
17
+ *.gem
18
+ *.rbc
19
+ .bundle
20
+ .config
21
+ .yardoc
22
+ InstalledFiles
23
+ _yardoc
24
+ coverage
25
+ doc/
26
+ lib/bundler/man
27
+ pkg
28
+ rdoc
29
+ spec/reports
30
+ test/tmp
31
+ test/version_tmp
32
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rake'
4
+ # Add your dependencies here:
5
+ gem 'webstub'
6
+ gem 'motion-stump'
data/Gemfile.lock ADDED
@@ -0,0 +1,14 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ motion-stump (0.3.0)
5
+ rake (10.1.0)
6
+ webstub (1.0.1)
7
+
8
+ PLATFORMS
9
+ ruby
10
+
11
+ DEPENDENCIES
12
+ motion-stump
13
+ rake
14
+ webstub
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Nathan Kot
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,94 @@
1
+ # Motion Bindable
2
+
3
+ A simple data binding library for RubyMotion.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'motion_bindable'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```sh
16
+ $ bundle
17
+ ```
18
+
19
+ If you want to use the default strategies that come with MotionBindable add
20
+ this to your `app_delegate.rb`:
21
+
22
+ ``` ruby
23
+ def application(application, didFinishLaunchingWithOptions: launch_options)
24
+ MotionBindable::Strategies.use
25
+ true
26
+ end
27
+ ```
28
+
29
+ ## Usage
30
+
31
+ Add `include MotionBindable::Bindable` to make an object bindable:
32
+
33
+ ```ruby
34
+ # Models
35
+
36
+ class Item
37
+ include MotionBindable::Bindable
38
+ attr_accessor :name
39
+ attr_accessor :location
40
+
41
+ def location
42
+ @address ||= Address.new
43
+ end
44
+ end
45
+
46
+ class Address
47
+ attr_accessor :address
48
+ end
49
+ ```
50
+
51
+ In your view controller, you can bind the object to a set of Views or any
52
+ other object:
53
+
54
+ ```ruby
55
+ class ItemListViewController
56
+ def viewDidLoad
57
+ super
58
+ @name_field = UITextField.alloc.initWithFrame [[110, 60], [100, 26]]
59
+ @name_field.placeholder = "Name"
60
+ view.addSubview @name_field
61
+
62
+ @address_field = UITextField.alloc.initWithFrame [[110, 100], [100, 26]]
63
+ @address_field.placeholder = "Address"
64
+ view.addSubview @address_field
65
+
66
+ @item = Item.new
67
+ @item.bind_attributes({
68
+ name: @name_field,
69
+ location: {
70
+ address: @address_field
71
+ }
72
+ })
73
+ end
74
+ end
75
+ ```
76
+
77
+ When `@name_field.text` or `@address_field.text` changes, so will your model!
78
+
79
+ ### Strategies
80
+
81
+ The above example uses the `MotionBindable::Strategies::UITextField`.
82
+ which comes with MotionBindable. Take a look in
83
+ `lib/motion_bindable/strategies` for the available defaults. You can implement
84
+ your own strategies by extending `MotionBindable::Strategy` like so:
85
+
86
+ **TODO**
87
+
88
+ ## Contributing
89
+
90
+ 1. Fork it
91
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
92
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
93
+ 4. Push to the branch (`git push origin my-new-feature`)
94
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # -*- coding: utf-8 -*-
2
+ $LOAD_PATH.unshift('/Library/RubyMotion/lib')
3
+ require 'motion/project/template/ios'
4
+
5
+ require './lib/motion_bindable'
6
+ require 'bundler'
7
+ Bundler.require
8
+
9
+ Motion::Project::App.setup do |app|
10
+ # Use `rake config' to see complete project settings.
11
+ app.name = 'motion_bindable'
12
+ end
@@ -0,0 +1,2 @@
1
+ class AppDelegate
2
+ end
@@ -0,0 +1,50 @@
1
+ module MotionBindable
2
+
3
+ #
4
+ # # Bindable Module
5
+ #
6
+ # Allow attributes of an object to be bound to other arbitrary objects
7
+ # through unique strategies.
8
+ #
9
+ # ## One-way binding
10
+ #
11
+ # Currently bindings are only one-way, i.e change in the arbitrary object
12
+ # affects the bindable object but not vice-versa.
13
+ #
14
+ module Bindable
15
+
16
+ def bind_attributes(attrs, object = self)
17
+ attrs.each_pair do |k, v|
18
+ if v.is_a?(Hash) then bind_attributes(v, object.send(k))
19
+ else bind strategy_for(v).new(object, k).bind(v)
20
+ end
21
+ end
22
+ end
23
+
24
+ def bind(strategy)
25
+ @bindings ||= []
26
+ @bindings << strategy
27
+ self
28
+ end
29
+
30
+ def refresh
31
+ @bindings.each { |b| b.refresh }
32
+ end
33
+
34
+ private
35
+
36
+ def strategy_for(reference)
37
+ Strategy.find_by_reference(reference)
38
+ end
39
+
40
+ def underscore(str)
41
+ str.gsub(/::/, '/')
42
+ .gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
43
+ .gsub(/([a-z\d])([A-Z])/,'\1_\2')
44
+ .tr("-", "_")
45
+ .downcase
46
+ end
47
+
48
+ end
49
+
50
+ end
@@ -0,0 +1,23 @@
1
+ module MotionBindable
2
+
3
+ # This allows objects to register multiple delegates
4
+ class DelegateProxy
5
+
6
+ attr_accessor :delegates
7
+
8
+ def initialize(*initial_delegates)
9
+ self.delegates = initial_delegates
10
+ end
11
+
12
+ def method_missing(name, *args, &block)
13
+ responses = []
14
+ delegates.each do |delegate|
15
+ responses << delegate.send(name, *args, &block) if delegate.respond_to?(name)
16
+ end
17
+
18
+ responses.first
19
+ end
20
+
21
+ end
22
+
23
+ end
@@ -0,0 +1,53 @@
1
+ module MotionBindable
2
+
3
+ #
4
+ # Represents a binding strategy. Designed to be as flexible as possible.
5
+ #
6
+ class Strategy
7
+
8
+ @strategies_map = [{ class: Strategy, candidates: [Object] }]
9
+
10
+ def self.register_strategy(strategy, *objects)
11
+ @strategies_map << { class: strategy, candidates: objects }
12
+ end
13
+
14
+ def self.find_by_reference(object)
15
+ @strategies_map.reverse.find do |h|
16
+ h[:candidates].include? object.class
17
+ end.fetch(:class)
18
+ end
19
+
20
+ attr_accessor :object
21
+ attr_accessor :bound
22
+
23
+ def initialize(object, attribute)
24
+ @attribute = attribute.to_sym
25
+ self.object = object
26
+ end
27
+
28
+ def bind(bound)
29
+ self.bound = bound
30
+ on_bind
31
+
32
+ self
33
+ end
34
+
35
+ # You can either choose to just override `#refresh` for objects that can't
36
+ # be bound with callbacks. Or override `#on_bind` for objects that can be
37
+ # bound with a callback.
38
+ def refresh; end
39
+ def on_bind
40
+ refresh
41
+ end
42
+
43
+ def attribute
44
+ object.send(@attribute)
45
+ end
46
+
47
+ def attribute=(value)
48
+ object.send(:"#{@attribute.to_s}=", value)
49
+ end
50
+
51
+ end
52
+
53
+ end
@@ -0,0 +1,3 @@
1
+ module MotionBindable
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,19 @@
1
+ unless defined?(Motion::Project::Config)
2
+ raise "The motion_bindable gem must be required within a RubyMotion project Rakefile."
3
+ end
4
+
5
+ Motion::Project::App.setup do |app|
6
+
7
+ Dir.glob(
8
+ File.join(File.dirname(__FILE__), 'motion_bindable/**/*.rb')
9
+ ).each do |file|
10
+ app.files.unshift(file)
11
+ end
12
+
13
+ Dir.glob(
14
+ File.join(File.dirname(__FILE__), 'strategies/**/*.rb')
15
+ ).each do |file|
16
+ app.files.unshift(file)
17
+ end
18
+
19
+ end
@@ -0,0 +1,10 @@
1
+ module MotionBindable::Strategies
2
+
3
+ def self.apply
4
+ ::MotionBindable::Strategy.register_strategy(
5
+ MotionBindable::Strategies::UITextField,
6
+ ::UITextField
7
+ )
8
+ end
9
+
10
+ end
@@ -0,0 +1,30 @@
1
+ module MotionBindable::Strategies
2
+
3
+ class UITextField < ::MotionBindable::Strategy
4
+
5
+ def on_bind
6
+ if bound.delegate.is_a? ::MotionBindable::DelegateProxy
7
+ bound.delegate.delegates << self
8
+ else
9
+ bound.delegate = ::MotionBindable::DelegateProxy.new(
10
+ bound.delegate,
11
+ self
12
+ )
13
+ end
14
+
15
+ update_attribute
16
+ end
17
+
18
+ def textFieldDidEndEditing(_)
19
+ update_attribute
20
+ end
21
+
22
+ private
23
+
24
+ def update_attribute
25
+ self.attribute = bound.text
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,20 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require File.expand_path('../lib/motion_bindable/version.rb', __FILE__)
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "motion_bindable"
8
+ spec.version = MotionBindable::VERSION
9
+ spec.authors = ["Nathan Kot"]
10
+ spec.email = ["nk@nathankot.com"]
11
+ spec.description = 'A simple data binding library for RubyMotion.'
12
+ spec.summary = 'Inspired by RivetsJS'
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+ end
Binary file
@@ -0,0 +1,59 @@
1
+ describe 'MotionBindable::Bindable' do
2
+
3
+ class FakeStrategy < MotionBindable::Strategy
4
+ end
5
+
6
+ class FakeBindable
7
+ include MotionBindable::Bindable
8
+ attr_accessor :attribute
9
+ attr_accessor :nested
10
+ end
11
+
12
+ before do
13
+ @object = FakeBindable.new
14
+ @bound = Object.new
15
+ @object.stub!(:strategy_for) { |_| FakeStrategy }
16
+ end
17
+
18
+ describe '#bind_attributes' do
19
+ before do
20
+ @object.nested = FakeBindable.new
21
+ end
22
+
23
+ it 'accepts nested attributes' do
24
+ FakeStrategy.stub!(:bind) do |attribute|
25
+ @attribute = attribute
26
+ end
27
+
28
+ @object.bind_attributes({
29
+ attribute: @bound,
30
+ nested: {
31
+ attribute: @bound
32
+ }
33
+ })
34
+
35
+ @attribute.should.equal @object.nested.attribute
36
+ end
37
+
38
+ it 'passes the strategy to bind' do
39
+ @called = false
40
+ @object.stub!(:bind) { |_| @called = true }
41
+ @object.bind_attributes({ attribute: @bound })
42
+ @called.should.equal true
43
+ end
44
+
45
+ end
46
+
47
+ describe '#bind' do
48
+
49
+ before do
50
+ @strategy = FakeStrategy.new(@object, :attribute)
51
+ end
52
+
53
+ it 'should be chainable' do
54
+ @object.bind(@strategy).should.equal @object
55
+ end
56
+
57
+ end
58
+
59
+ end
@@ -0,0 +1,67 @@
1
+ Strategy = MotionBindable::Strategy
2
+
3
+ class ObjectOne; attr_accessor :attribute end
4
+ class ObjectTwo; attr_accessor :attribute end
5
+
6
+ class FakeStrategyOne < Strategy; end
7
+ class FakeStrategyTwo < Strategy; end
8
+
9
+ describe 'MotionBindable::Strategy' do
10
+
11
+ before do
12
+ Strategy.register_strategy(FakeStrategyOne, ObjectOne)
13
+ Strategy.register_strategy(FakeStrategyTwo, ObjectTwo)
14
+ end
15
+
16
+ describe 'class methods' do
17
+
18
+ describe 'self#find_by_reference' do
19
+
20
+ it 'returns a strategy that specifies the object as a candidate' do
21
+ Strategy.find_by_reference(ObjectOne.new).should.equal FakeStrategyOne
22
+ Strategy.find_by_reference(ObjectTwo.new).should.equal FakeStrategyTwo
23
+ end
24
+
25
+ end
26
+
27
+ end
28
+
29
+ describe 'instance methods' do
30
+
31
+ before do
32
+ @object = ObjectOne.new
33
+ @bound = Object.new
34
+ @strategy = Strategy.new(@object, :attribute)
35
+ end
36
+
37
+ describe '#bind' do
38
+ it 'should respond' do
39
+ @strategy.respond_to?(:bind).should.equal true
40
+ end
41
+
42
+ it 'should set the bound object' do
43
+ @strategy.bind(@bound)
44
+ @strategy.bound.should.equal @bound
45
+ end
46
+
47
+ it 'should return self' do
48
+ @strategy.bind(@bound).should.equal @strategy
49
+ end
50
+ end
51
+
52
+ describe '#refresh' do
53
+ it 'should respond' do
54
+ @strategy.respond_to?(:refresh).should.equal true
55
+ end
56
+ end
57
+
58
+ describe '#attribute=' do
59
+ it 'should be a proxy to set the attribute on the bound object' do
60
+ @strategy.attribute = 'test'
61
+ @object.attribute.should.equal 'test'
62
+ end
63
+ end
64
+
65
+ end
66
+
67
+ end
@@ -0,0 +1,68 @@
1
+ class FakeModel
2
+ include MotionBindable::Bindable
3
+ attr_accessor :attribute
4
+ attr_accessor :nested
5
+ end
6
+
7
+ describe 'MotionBindable::Strategies::UITextField' do
8
+
9
+ before do
10
+ MotionBindable::Strategy.register_strategy(
11
+ MotionBindable::Strategies::UITextField,
12
+ UITextField
13
+ )
14
+
15
+ @app = UIApplication.sharedApplication
16
+ @text_field = UITextField.alloc.initWithFrame [[110, 60], [100, 26]]
17
+ end
18
+
19
+ context 'nested model' do
20
+
21
+ before do
22
+ @object = FakeModel.new
23
+ @object.nested = FakeModel.new
24
+ end
25
+
26
+ context 'text set and then bound' do
27
+
28
+ before do
29
+ @text_field.text = 'Just testing.'
30
+ @object.bind_attributes({
31
+ attribute: @text_field,
32
+ nested: {
33
+ attribute: @text_field
34
+ }
35
+ })
36
+ end
37
+
38
+ it 'should update the root attribute' do
39
+ @object.attribute.should.equal 'Just testing.'
40
+ end
41
+
42
+ it 'should update the nested attribute' do
43
+ @object.nested.attribute.should.equal 'Just testing.'
44
+ end
45
+
46
+ context 'text field is updated' do
47
+
48
+ before do
49
+ @text_field.text = 'Updated.'
50
+ @text_field.delegate.textFieldDidEndEditing(self)
51
+ end
52
+
53
+ it 'should update the root attribute' do
54
+ @object.attribute.should.equal 'Updated.'
55
+ end
56
+
57
+ it 'should update the nested attribute' do
58
+ @object.nested.attribute.should.equal 'Updated.'
59
+ end
60
+
61
+ end
62
+
63
+ end
64
+
65
+ end
66
+
67
+
68
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: motion_bindable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Kot
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple data binding library for RubyMotion.
14
+ email:
15
+ - nk@nathankot.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - .gitignore
21
+ - Gemfile
22
+ - Gemfile.lock
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - app/app_delegate.rb
27
+ - lib/motion_bindable.rb
28
+ - lib/motion_bindable/bindable.rb
29
+ - lib/motion_bindable/delegate_proxy.rb
30
+ - lib/motion_bindable/strategy.rb
31
+ - lib/motion_bindable/version.rb
32
+ - lib/strategies/strategies.rb
33
+ - lib/strategies/ui_text_field.rb
34
+ - motion_bindable.gemspec
35
+ - resources/Default-568h@2x.png
36
+ - spec/bindable_spec.rb
37
+ - spec/strategy_spec.rb
38
+ - spec/ui_text_field_strategy_spec.rb
39
+ homepage: ''
40
+ licenses:
41
+ - MIT
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 2.0.3
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Inspired by RivetsJS
63
+ test_files:
64
+ - spec/bindable_spec.rb
65
+ - spec/strategy_spec.rb
66
+ - spec/ui_text_field_strategy_spec.rb