motion-callback 0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +83 -0
- data/lib/callback.rb +10 -0
- data/lib/project/callback.rb +5 -0
- metadata +63 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6fa0e287cf4277dc3cb54bef99cb031d3e54fafe
|
4
|
+
data.tar.gz: ffb74cb12edee85bd8e4f8bba4d2f82f8e82c8e4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 819840f8b31f613847bc8051d312f56d4b38bcc37032d2a60211dfa6ffffa8a9c174a093b1b39fc164292a382d71627edd145b319e2fbfb664a6a26143fdc920
|
7
|
+
data.tar.gz: db416c9f9c92a332dedaae28419d640000361e7dba600eb1a25f6d40aeeaf572e23b2cb42b48e5ce113a63e98508b4642d1a5686bb0676a3267479410db4df9d
|
data/README.md
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
# motion-callback
|
2
|
+
|
3
|
+
Allows for multi-branch callbacks from a single method. This allows for blocks of code
|
4
|
+
to be invoked in response to asynchronous events triggered in the called method.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'callback'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install motion-callback
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
Using `callbacks` is really easy. Just add it to your Gemfile. Then in your code do this:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
do_something_complicated do |on|
|
26
|
+
on.success {
|
27
|
+
# take actions based on success
|
28
|
+
}
|
29
|
+
on.failure {
|
30
|
+
# crash and burn gracefully
|
31
|
+
}
|
32
|
+
on.indeterminate_state {
|
33
|
+
# act really confused
|
34
|
+
}
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
In your `do_something_complicated` method, write code like this:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
def do_something_complicated(&block)
|
42
|
+
some_asynchronous_stuff{|status|
|
43
|
+
case status
|
44
|
+
when 'error' then block.call Callback.new(:error)
|
45
|
+
when 'success' then block.call Callback.new(:success)
|
46
|
+
when '???' then block.call Callback.new(:indeterminate_state)
|
47
|
+
end
|
48
|
+
}
|
49
|
+
end
|
50
|
+
```
|
51
|
+
|
52
|
+
A better idea of why this is interesting is code that reaches out
|
53
|
+
to a remote for asynchronously-retrieved data like this:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
check_already_logged_in do |on|
|
57
|
+
on.success{ transition_to_authenticated_user_section }
|
58
|
+
on.failure{
|
59
|
+
login do |on|
|
60
|
+
on.success{ transition_to_authenticated_user_section }
|
61
|
+
on.failure{ alert_user_to_screwup }
|
62
|
+
end
|
63
|
+
}
|
64
|
+
end
|
65
|
+
```
|
66
|
+
|
67
|
+
## Thanks To
|
68
|
+
|
69
|
+
[Emmanuel Oga](https://gist.github.com/EmmanuelOga)
|
70
|
+
|
71
|
+
and this Gist, which is essentially the entire Gem.
|
72
|
+
|
73
|
+
https://gist.github.com/EmmanuelOga/1417762
|
74
|
+
|
75
|
+
Note also [this blog post and the ensuing discussion](http://www.mattsears.com/articles/2011/11/27/ruby-blocks-as-dynamic-callbacks).
|
76
|
+
|
77
|
+
## Contributing
|
78
|
+
|
79
|
+
1. Fork it
|
80
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
81
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
82
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
83
|
+
5. Create new Pull Request
|
data/lib/callback.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
unless defined?(Motion::Project::Config)
|
4
|
+
raise "This file must be required within a RubyMotion project Rakefile."
|
5
|
+
end
|
6
|
+
|
7
|
+
lib_dir_path = File.dirname(File.expand_path(__FILE__))
|
8
|
+
Motion::Project::App.setup do |app|
|
9
|
+
app.files.unshift(Dir.glob(File.join(lib_dir_path, "project/**/*.rb")))
|
10
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: motion-callback
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Steve Ross
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-06-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: |-
|
28
|
+
Allows for multi-branch callbacks from a single method. This allows for blocks of code
|
29
|
+
to be invoked in response to asynchronous events triggered in the called method.
|
30
|
+
email:
|
31
|
+
- sxross@gmail.com
|
32
|
+
executables: []
|
33
|
+
extensions: []
|
34
|
+
extra_rdoc_files: []
|
35
|
+
files:
|
36
|
+
- README.md
|
37
|
+
- lib/callback.rb
|
38
|
+
- lib/project/callback.rb
|
39
|
+
homepage: ''
|
40
|
+
licenses:
|
41
|
+
- ''
|
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.2.2
|
60
|
+
signing_key:
|
61
|
+
specification_version: 4
|
62
|
+
summary: Multi-branch callbacks
|
63
|
+
test_files: []
|