elevate 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -5,3 +5,4 @@ resources/*.nib
5
5
  resources/*.momd
6
6
  resources/*.storyboardc
7
7
  .rvmrc
8
+ Gemfile.lock
data/README.md CHANGED
@@ -1,8 +1,10 @@
1
- Elevate [![Code Climate](https://codeclimate.com/github/mattgreen/elevate.png)](https://codeclimate.com/github/mattgreen/elevate)
1
+ Elevate
2
2
  ======
3
3
 
4
4
  Stop scattering your domain logic across your view controller. Consolidate it to a single conceptual unit with Elevate.
5
5
 
6
+ [![Code Climate](https://codeclimate.com/github/mattgreen/elevate.png)](https://codeclimate.com/github/mattgreen/elevate) [![Travis](https://api.travis-ci.org/mattgreen/elevate.png)](https://travis-ci.org/mattgreen/elevate)
7
+
6
8
  Example
7
9
  -------
8
10
 
@@ -19,16 +21,24 @@ Example
19
21
  UserRegistration.store(credentials.username, credentials.token)
20
22
  end
21
23
 
22
- # Return value of block is passed back to on_completed
24
+ # Anything yielded from this block is passed to on_update
25
+ yield "Logged in!"
26
+
27
+ # Return value of block is passed back to on_finish
23
28
  credentials != nil
24
29
  end
25
30
 
26
- on_started do
31
+ on_start do
27
32
  # This block runs on the UI thread after the operation has been queued.
28
33
  SVProgressHUD.showWithStatus("Logging In...")
29
34
  end
30
35
 
31
- on_completed do |result, exception|
36
+ on_update do |status|
37
+ # This block runs on the UI thread with anything the task yields
38
+ puts status
39
+ end
40
+
41
+ on_finish do |result, exception|
32
42
  # This block runs on the UI thread after the task block has finished.
33
43
  SVProgressHUD.dismiss
34
44
 
@@ -71,7 +81,7 @@ Installation
71
81
  ------------
72
82
  Update your Gemfile:
73
83
 
74
- gem "elevate", "~> 0.4.0"
84
+ gem "elevate", "~> 0.5.0"
75
85
 
76
86
  Bundle:
77
87
 
@@ -91,7 +101,12 @@ Launch an async task with the `async` method:
91
101
 
92
102
  * Pass all the data the task needs to operate (such as credentials or search terms) in to the `async` method.
93
103
  * Define a block that contains a `task` block. The `task` block should contain all of your non-UI code. It will be run on a background thread. Any data passed into the `async` method will be available as instance variables, keyed by the provided hash key.
94
- * Optionally, define `on_started` and `on_completed` blocks to run as the task starts and finishes. These are run in the UI thread, and should contain all of your UI code.
104
+ * Optionally:
105
+ * Define an `on_start` block to be run when the task starts
106
+ * Define an `on_finish` block to be run when the task finishes
107
+ * Define an `on_update` block to be called any time the task calls yield (useful for relaying status information back during long operations)
108
+
109
+ All of the `on_` blocks are called on the UI thread. `on_start` is guaranteed to precede `on_update` and `on_finish`.
95
110
 
96
111
  ```ruby
97
112
  @track_task = async artist: searchBar.text do
@@ -100,11 +115,11 @@ Launch an async task with the `async` method:
100
115
  ArtistDB.update(artist)
101
116
  end
102
117
 
103
- on_started do
118
+ on_start do
104
119
  SVProgressHUD.showWithStatus("Adding...")
105
120
  end
106
121
 
107
- on_completed do |result, exception|
122
+ on_finish do |result, exception|
108
123
  SVProgressHUD.dismiss
109
124
  end
110
125
  end
@@ -117,12 +132,10 @@ To cancel a task (like when the view controller is being dismissed), call `cance
117
132
  To Do
118
133
  -----
119
134
  * Need ability to set timeout for tasks
120
- * More thought on the semantics
121
135
 
122
136
  Caveats
123
137
  ---------
124
138
  * Must use Elevate's HTTP client instead of other iOS networking libs
125
- * No way to report progress (idea: `execute` could yield status information via optional block)
126
139
 
127
140
  Inspiration
128
141
  -----------
data/lib/elevate/api.rb CHANGED
@@ -22,8 +22,9 @@ module Elevate
22
22
  raise "No task block specified!" unless dsl.task_callback
23
23
 
24
24
  operation = ElevateOperation.alloc.initWithTarget(dsl.task_callback, args: args)
25
- operation.on_started = Callback.new(self, dsl.started_callback) if dsl.started_callback
26
- operation.on_finished = Callback.new(self, dsl.finished_callback) if dsl.finished_callback
25
+ operation.on_start = Callback.new(self, dsl.start_callback) if dsl.start_callback
26
+ operation.on_finish = Callback.new(self, dsl.finish_callback) if dsl.finish_callback
27
+ operation.on_update = Callback.new(self, dsl.update_callback) if dsl.update_callback
27
28
 
28
29
  yield operation
29
30
 
data/lib/elevate/dsl.rb CHANGED
@@ -4,26 +4,31 @@ module Elevate
4
4
  instance_eval(&block)
5
5
  end
6
6
 
7
- attr_reader :started_callback
8
- attr_reader :finished_callback
7
+ attr_reader :finish_callback
8
+ attr_reader :start_callback
9
9
  attr_reader :task_callback
10
+ attr_reader :update_callback
10
11
 
11
- def task(&block)
12
- raise "task blocks must accept zero parameters" unless block.arity == 0
12
+ def on_finish(&block)
13
+ raise "on_finish blocks must accept two parameters" unless block.arity == 2
13
14
 
14
- @task_callback = block
15
+ @finish_callback = block
15
16
  end
16
17
 
17
- def on_started(&block)
18
- raise "on_started blocks must accept zero parameters" unless block.arity == 0
18
+ def on_start(&block)
19
+ raise "on_start blocks must accept zero parameters" unless block.arity == 0
20
+
21
+ @start_callback = block
22
+ end
19
23
 
20
- @started_callback = block
24
+ def on_update(&block)
25
+ @update_callback = block
21
26
  end
22
27
 
23
- def on_completed(&block)
24
- raise "on_completed blocks must accept two parameters" unless block.arity == 2
28
+ def task(&block)
29
+ raise "task blocks must accept zero parameters" unless block.arity == 0
25
30
 
26
- @finished_callback = block
31
+ @task_callback = block
27
32
  end
28
33
  end
29
34
  end
@@ -1,31 +1,21 @@
1
1
  module Elevate
2
- class Context
3
- def initialize(args, &block)
4
- metaclass = class << self; self; end
5
- metaclass.send(:define_method, :execute, &block)
6
-
7
- args.each do |key, value|
8
- instance_variable_set("@#{key}", value)
9
- end
10
- end
11
- end
12
-
13
2
  class ElevateOperation < NSOperation
14
3
  def initWithTarget(target, args:args)
15
4
  if init()
16
5
  @coordinator = IOCoordinator.new
17
- @target = target
18
- @context = Context.new(args, &target)
19
- @finished_callback = nil
6
+ @context = TaskContext.new(args, &target)
7
+ @update_callback = nil
8
+ @finish_callback = nil
20
9
 
21
10
  setCompletionBlock(lambda do
22
- if @finished_callback
23
- @finished_callback.call(@result, @exception) unless isCancelled
11
+ if @finish_callback
12
+ @finish_callback.call(@result, @exception) unless isCancelled
24
13
  end
25
14
 
26
15
  Dispatch::Queue.main.sync do
27
- @target = nil
28
- @finished_callback = nil
16
+ @context = nil
17
+ @update_callback = nil
18
+ @finish_callback = nil
29
19
  end
30
20
  end)
31
21
  end
@@ -42,7 +32,6 @@ module Elevate
42
32
  def inspect
43
33
  details = []
44
34
  details << "<canceled>" if @coordinator.cancelled?
45
- details << "@target=#{@target.class.name}"
46
35
 
47
36
  "#<#{self.class.name}: #{details.join(" ")}>"
48
37
  end
@@ -58,7 +47,9 @@ module Elevate
58
47
 
59
48
  begin
60
49
  unless @coordinator.cancelled?
61
- @result = @context.execute
50
+ @result = @context.execute do |*args|
51
+ @update_callback.call(*args) if @update_callback
52
+ end
62
53
  end
63
54
 
64
55
  rescue Exception => e
@@ -73,18 +64,22 @@ module Elevate
73
64
  attr_reader :exception
74
65
  attr_reader :result
75
66
 
76
- def on_started=(callback)
77
- started_callback = callback
78
- started_callback.retain
67
+ def on_finish=(callback)
68
+ @finish_callback = callback
69
+ end
70
+
71
+ def on_start=(callback)
72
+ start_callback = callback
73
+ start_callback.retain
79
74
 
80
75
  Dispatch::Queue.main.async do
81
- started_callback.call unless isCancelled
82
- started_callback.release
76
+ start_callback.call unless isCancelled
77
+ start_callback.release
83
78
  end
84
79
  end
85
80
 
86
- def on_finished=(callback)
87
- @finished_callback = callback
81
+ def on_update=(callback)
82
+ @update_callback = callback
88
83
  end
89
84
  end
90
85
  end
@@ -0,0 +1,12 @@
1
+ module Elevate
2
+ class TaskContext
3
+ def initialize(args, &block)
4
+ metaclass = class << self; self; end
5
+ metaclass.send(:define_method, :execute, &block)
6
+
7
+ args.each do |key, value|
8
+ instance_variable_set("@#{key}", value)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,3 +1,3 @@
1
1
  module Elevate
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
data/spec/api_spec.rb CHANGED
@@ -12,7 +12,7 @@ describe Elevate do
12
12
  true
13
13
  end
14
14
 
15
- on_completed do |result, exception|
15
+ on_finish do |result, exception|
16
16
  @called = result
17
17
  resume
18
18
  end
@@ -29,7 +29,7 @@ describe Elevate do
29
29
  @name
30
30
  end
31
31
 
32
- on_completed do |name, exception|
32
+ on_finish do |name, exception|
33
33
  @result = name
34
34
  resume
35
35
  end
@@ -39,5 +39,34 @@ describe Elevate do
39
39
  @result.should == "harry"
40
40
  end
41
41
  end
42
+
43
+ it "allows tasks to report progress" do
44
+ @updates = []
45
+
46
+ async do
47
+ task do
48
+ sleep 0.1
49
+ yield 1
50
+ sleep 0.2
51
+ yield 2
52
+ sleep 0.3
53
+ yield 3
54
+
55
+ true
56
+ end
57
+
58
+ on_update do |count|
59
+ @updates << count
60
+ end
61
+
62
+ on_finish do |result, exception|
63
+ resume
64
+ end
65
+ end
66
+
67
+ wait_max 1.0 do
68
+ @updates.should == [1,2,3]
69
+ end
70
+ end
42
71
  end
43
72
  end
@@ -13,12 +13,12 @@ describe Elevate::ElevateOperation do
13
13
  @operation.class.ancestors.should.include NSOperation
14
14
  end
15
15
 
16
- describe "#on_finished" do
16
+ describe "#on_finish=" do
17
17
  it "invokes it after #on_started" do
18
18
  @lock = NSLock.alloc.init
19
19
  @value = []
20
20
 
21
- @operation.on_started = lambda do
21
+ @operation.on_start = lambda do
22
22
  @lock.lock()
23
23
  if @value == []
24
24
  @value << 1
@@ -26,7 +26,7 @@ describe Elevate::ElevateOperation do
26
26
  @lock.unlock()
27
27
  end
28
28
 
29
- @operation.on_finished = lambda do |result, exception|
29
+ @operation.on_finish = lambda do |result, exception|
30
30
  @lock.lock()
31
31
  if @value == [1]
32
32
  @value << 2
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elevate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-07 00:00:00.000000000 Z
12
+ date: 2013-04-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -85,7 +85,6 @@ files:
85
85
  - .gitignore
86
86
  - .travis.yml
87
87
  - Gemfile
88
- - Gemfile.lock
89
88
  - Guardfile
90
89
  - LICENSE
91
90
  - README.md
@@ -107,10 +106,10 @@ files:
107
106
  - lib/elevate/io_coordinator.rb
108
107
  - lib/elevate/operation.rb
109
108
  - lib/elevate/promise.rb
109
+ - lib/elevate/task_context.rb
110
110
  - lib/elevate/version.rb
111
111
  - spec/api_spec.rb
112
112
  - spec/callback_spec.rb
113
- - spec/dsl_spec.rb
114
113
  - spec/http/activity_indicator_spec.rb
115
114
  - spec/http/http_client_spec.rb
116
115
  - spec/http/http_request_spec.rb
@@ -128,28 +127,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
128
127
  - - ! '>='
129
128
  - !ruby/object:Gem::Version
130
129
  version: '0'
131
- segments:
132
- - 0
133
- hash: -1464207392655779665
134
130
  required_rubygems_version: !ruby/object:Gem::Requirement
135
131
  none: false
136
132
  requirements:
137
133
  - - ! '>='
138
134
  - !ruby/object:Gem::Version
139
135
  version: '0'
140
- segments:
141
- - 0
142
- hash: -1464207392655779665
143
136
  requirements: []
144
137
  rubyforge_project:
145
- rubygems_version: 1.8.24
138
+ rubygems_version: 1.8.23
146
139
  signing_key:
147
140
  specification_version: 3
148
141
  summary: Distill the essence of your RubyMotion app
149
142
  test_files:
150
143
  - spec/api_spec.rb
151
144
  - spec/callback_spec.rb
152
- - spec/dsl_spec.rb
153
145
  - spec/http/activity_indicator_spec.rb
154
146
  - spec/http/http_client_spec.rb
155
147
  - spec/http/http_request_spec.rb
data/Gemfile.lock DELETED
@@ -1,41 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- elevate (0.4.0)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
- coderay (1.0.8)
10
- guard (1.6.2)
11
- listen (>= 0.6.0)
12
- lumberjack (>= 1.0.2)
13
- pry (>= 0.9.10)
14
- terminal-table (>= 1.4.3)
15
- thor (>= 0.14.6)
16
- guard-motion (0.1.1)
17
- guard (>= 1.1.0)
18
- rake (>= 0.9)
19
- listen (0.7.2)
20
- lumberjack (1.0.2)
21
- method_source (0.8.1)
22
- pry (0.9.11.4)
23
- coderay (~> 1.0.5)
24
- method_source (~> 0.8)
25
- slop (~> 3.4)
26
- rake (0.9.6)
27
- rb-fsevent (0.9.3)
28
- slop (3.4.3)
29
- terminal-table (1.4.5)
30
- thor (0.17.0)
31
- webstub (0.3.4)
32
-
33
- PLATFORMS
34
- ruby
35
-
36
- DEPENDENCIES
37
- elevate!
38
- guard-motion (~> 0.1.1)
39
- rake (>= 0.9.0)
40
- rb-fsevent (~> 0.9.1)
41
- webstub (~> 0.3.3)
data/spec/dsl_spec.rb DELETED
@@ -1,21 +0,0 @@
1
- describe Elevate::DSL do
2
- describe "#on_started" do
3
- it "stores the provided block" do
4
- i = Elevate::DSL.new do
5
- on_started { puts 'hi' }
6
- end
7
-
8
- i.started_callback.should.not.be.nil
9
- end
10
- end
11
-
12
- describe "#on_completed" do
13
- it "stores the passed block" do
14
- i = Elevate::DSL.new do
15
- on_completed { |result, exception| puts 'hi' }
16
- end
17
-
18
- i.finished_callback.should.not.be.nil
19
- end
20
- end
21
- end