roda-flow 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +29 -27
  3. data/lib/roda/plugins/flow.rb +46 -30
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4ee7c883d4158905abdd4ca1aaffee0915628492
4
- data.tar.gz: db5ec8f480bafebf5c2bdd1265b94209941bbec0
3
+ metadata.gz: b4d4b0303ce594d2a42f684180275547d9e11032
4
+ data.tar.gz: 510747bc0c24a158d2149db5575607721a92b862
5
5
  SHA512:
6
- metadata.gz: 8f4f00f2bacad1c8469f8d4a05ee274ce702ca1108c83c3408a1fccc517e41d6b59337e165869e81191805fc5e21d898198512a66128560f488e19e2e80b5b8e
7
- data.tar.gz: bd5c579521e4d4140b10f0d8fec3edf2e03a6d4fd904fd47e04669f31153a11f3484f0372217c3dd0d65ddfc3cd24161e57e393db178ed9e4653f86a1a35a501
6
+ metadata.gz: 9b3b29ef6f199663fed4ef668a3cac41204320bc975d6262e9dc2664bf4d28b0d379b902543e3776e02ad3beff391d2010660a97a3c9be3c6a3e748d5cd1c7f7
7
+ data.tar.gz: 8f92a3583f9c52bd3de118e0f3fb7548b9f6e417c12140320b518e1ac6d753add978328ff146f783bbb1a709748076d72fc2f4836dfb51cbfa31d19a124decc4
data/README.md CHANGED
@@ -103,33 +103,35 @@ class App < Roda
103
103
  plugin :flow
104
104
 
105
105
  route do |r|
106
- r.on 'users', resolve: 'repositories.user' do |user_repository|
107
- r.is do
108
- r.get to: 'controllers.users#index', inject: [response, user_repository]
109
- r.post(
110
- to: 'controllers.users#create',
111
- inject: [response, user_repository],
112
- call_with: [r.params]
113
- )
114
- end
115
-
116
- r.on :user_id do |user_id|
117
- r.get(
118
- to: 'controllers.users#show',
119
- inject: [response, user_repository],
120
- call_with: [user_id]
121
- )
122
- r.put(
123
- to: 'controllers.users#update',
124
- inject: [response, user_repository],
125
- call_with: [user_id, r.params]
126
- )
127
- r.delete(
128
- # to: can also be a registered proc, just omit the "#" and method name
129
- to: 'controllers.users#destroy',
130
- inject: [response, user_repository],
131
- call_with: [user_id]
132
- )
106
+ r.on 'users' do
107
+ r.resolve 'repositories.user' do |user_repository| do
108
+ r.is do
109
+ r.get to: 'controllers.users#index', inject: [response, user_repository]
110
+ r.post(
111
+ to: 'controllers.users#create',
112
+ inject: [response, user_repository],
113
+ call_with: [r.params]
114
+ )
115
+ end
116
+
117
+ r.on :user_id do |user_id|
118
+ r.get(
119
+ to: 'controllers.users#show',
120
+ inject: [response, user_repository],
121
+ call_with: [user_id]
122
+ )
123
+ r.put(
124
+ to: 'controllers.users#update',
125
+ inject: [response, user_repository],
126
+ call_with: [user_id, r.params]
127
+ )
128
+ r.delete(
129
+ # to: can also be a registered proc, just omit the "#" and method name
130
+ to: 'controllers.users#destroy',
131
+ inject: [response, user_repository],
132
+ call_with: [user_id]
133
+ )
134
+ end
133
135
  end
134
136
  end
135
137
  end
@@ -6,49 +6,65 @@ class Roda
6
6
  end
7
7
 
8
8
  module RequestMethods
9
- def if_match(args, &block)
10
- path = @remaining_path
11
- # For every block, we make sure to reset captures so that
12
- # nesting matchers won't mess with each other's captures.
13
- @captures.clear
9
+ def resolve(*args, &block)
10
+ on(resolve: args, &block)
11
+ end
14
12
 
15
- kwargs = {}
13
+ private
16
14
 
17
- if args.last === Base::RequestMethods::TERM && args.first.is_a?(::Hash)
18
- kwargs = args.shift
15
+ def match_resolve(resolve)
16
+ Array(resolve).flatten.each do |key|
17
+ @captures << roda_class.resolve(key)
19
18
  end
19
+ end
20
20
 
21
- kwargs = args.pop if args.last.is_a?(::Hash)
21
+ def match_to(to)
22
+ container_key, @block_method = to.to_s.split('#')
23
+ @block_arg = roda_class.resolve(container_key)
24
+ end
22
25
 
23
- return unless match_all(args)
26
+ def match_inject(inject)
27
+ @block_arg = @block_arg.call(*inject) if @block_arg
28
+ end
24
29
 
25
- container_keys = Array(kwargs.fetch(:resolve, []))
30
+ def match_call_with(call_with)
31
+ @captures.concat(call_with)
32
+ end
26
33
 
27
- resolutions = container_keys.map do |resolution|
28
- if resolution.respond_to?(:call)
29
- resoltion.call(captures)
30
- else
31
- roda_class.resolve(resolution)
32
- end
33
- end
34
+ def if_match(*args, &block)
35
+ path = @remaining_path
36
+ # For every block, we make sure to reset captures so that
37
+ # nesting matchers won't mess with each other's captures.
38
+ @captures.clear
39
+
40
+ return unless match_all(args)
41
+ block_result(get_block(&block).call(*captures))
42
+ throw :halt, response.finish
43
+ ensure
44
+ @remaining_path = path
45
+ end
34
46
 
35
- block = kwargs.fetch(:to, block)
36
- injections = kwargs.fetch(:inject, [])
37
- method_injections = kwargs.fetch(:call_with, [])
47
+ def always(&block)
48
+ super(&get_block(&block))
49
+ end
38
50
 
39
- unless block.nil? || block.respond_to?(:call)
40
- block, method = block.to_s.split('#')
41
- if method
42
- block = roda_class.resolve(block).call(*injections).method(method)
51
+ def get_block(&block)
52
+ if block_given?
53
+ block
54
+ elsif @block_arg
55
+ if @block_method
56
+ block_arg = @block_arg.method(@block_method)
43
57
  else
44
- block = roda_class.resolve(block).call(*injections)
58
+ block_arg = @block_arg
45
59
  end
60
+ clear_block_args
61
+ block_arg
46
62
  end
63
+ end
47
64
 
48
- block_result(block.call(*(captures + resolutions + method_injections)))
49
- throw :halt, response.finish
50
- ensure
51
- @remaining_path = path
65
+ def clear_block_args
66
+ @block_arg = nil
67
+ @block_method = nil
52
68
  end
53
69
  end
54
70
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roda-flow
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Holland
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-28 00:00:00.000000000 Z
11
+ date: 2015-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-container