roda-flow 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +30 -26
  3. data/lib/roda/plugins/flow.rb +57 -7
  4. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2621240ad009f4a0b9e43cbd8daecc84fdb66d46
4
- data.tar.gz: b9217d5f49b36c73f138b34255ae2c7d20e044a9
3
+ metadata.gz: 480bc546b4bf68f5b31d38fb3c948b9f051e796c
4
+ data.tar.gz: c6e8b194c79d7e85a823ec5dcb94f35d67104cac
5
5
  SHA512:
6
- metadata.gz: ca370e3372f1184da2f49519275575e338ee641137bd07498958445febc0bad3d63d024bb98e3dd3cdecbf5ea3094d283df08fedbb9192da93c26d6d06a589e5
7
- data.tar.gz: d081fc002739af9fc377056da30d49bd68b52284f8cd4e524ad0c37ac08f4ba864a3c0d1d120aa16d401cb8d3a913fa9999aa145fd150eddac183d26e4814944
6
+ metadata.gz: bc02a87ab0d85340acde77d09e273dabf7a6764ae76ad0c5adb9c5d4e2c1fd955ebdb39b2528a9e3ab33f6db492b1df798e6428418129abd1a6a39aa85fa7cd9
7
+ data.tar.gz: a89efaef010bdb5ef4e481d695ea4302952ee290c2329acf7e9108237cdfd68c49f490f0880f221368f6cbdf73ef921ec4fd76f0adfe6136ed5ba02d72637fc4
data/README.md CHANGED
@@ -120,32 +120,36 @@ class App < Roda
120
120
  route do |r|
121
121
  r.on 'users' do
122
122
  r.resolve 'repositories.user' do |user_repository| do
123
- r.is do
124
- r.get to: 'controllers.users#index', inject: [response, user_repository]
125
- r.post(
126
- to: 'controllers.users#create',
127
- inject: [response, user_repository],
128
- call_with: [r.params]
129
- )
130
- end
131
-
132
- r.on :user_id do |user_id|
133
- r.get(
134
- to: 'controllers.users#show',
135
- inject: [response, user_repository],
136
- call_with: [user_id]
137
- )
138
- r.put(
139
- to: 'controllers.users#update',
140
- inject: [response, user_repository],
141
- call_with: [user_id, r.params]
142
- )
143
- r.delete(
144
- # to: can also be a registered proc, just omit the "#" and method name
145
- to: 'controllers.users#destroy',
146
- inject: [response, user_repository],
147
- call_with: [user_id]
148
- )
123
+ # You can set the default options passed when
124
+ # mapping a route to a container item with
125
+ # flow_defaults, i.e.
126
+ flow_defaults inject: [response, user_repository] do
127
+ r.is do
128
+ r.get to: 'controllers.users#index'
129
+ r.post(
130
+ to: 'controllers.users#create',
131
+ # The following line is implied because of
132
+ # the call to flow_defaults above
133
+ # inject: [response, user_repository],
134
+ call_with: [r.params]
135
+ )
136
+ end
137
+
138
+ r.on :user_id do |user_id|
139
+ r.get(
140
+ to: 'controllers.users#show',
141
+ call_with: [user_id]
142
+ )
143
+ r.put(
144
+ to: 'controllers.users#update',
145
+ call_with: [user_id, r.params]
146
+ )
147
+ r.delete(
148
+ # to: can also be a registered proc, just omit the "#" and method name
149
+ to: 'controllers.users#destroy',
150
+ call_with: [user_id]
151
+ )
152
+ end
149
153
  end
150
154
  end
151
155
  end
@@ -1,6 +1,26 @@
1
1
  class Roda
2
2
  module RodaPlugins
3
3
  module Flow
4
+
5
+ KEY = 'flow.defaults'.freeze
6
+
7
+ module InstanceMethods
8
+ def flow_defaults(args = {}, &block)
9
+ if block_given?
10
+ defaults.merge!(args)
11
+ yield
12
+ else
13
+ raise RodaError 'must pass a block when using flow defaults'
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def defaults
20
+ env[KEY] ||= {}
21
+ end
22
+ end
23
+
4
24
  module RequestMethods
5
25
  def resolve(*keys)
6
26
  yield *keys.map { |key| roda_class.resolve(key) }
@@ -14,24 +34,27 @@ class Roda
14
34
  end
15
35
 
16
36
  def match_inject(inject)
17
- @block_arg = @block_arg.call(*inject) if @block_arg
37
+ @block_arg = @block_arg.call(*inject)
18
38
  end
19
39
 
20
40
  def match_call_with(call_with)
21
41
  @captures.concat(call_with)
22
42
  end
23
43
 
24
- def if_match(*args, &block)
44
+
45
+ def if_match(args, &block)
25
46
  path = @remaining_path
26
47
  # For every block, we make sure to reset captures so that
27
48
  # nesting matchers won't mess with each other's captures.
28
49
  @captures.clear
29
50
 
30
- return unless match_all(args)
31
- block_result(get_block(&block).call(*captures))
32
- throw :halt, response.finish
33
- ensure
34
- @remaining_path = path
51
+ if match_all(args)
52
+ block_result(get_block(&block).call(*captures))
53
+ throw :halt, response.finish
54
+ else
55
+ @remaining_path = path
56
+ false
57
+ end
35
58
  end
36
59
 
37
60
  def always(&block)
@@ -56,7 +79,34 @@ class Roda
56
79
  @block_arg = nil
57
80
  @block_method = nil
58
81
  end
82
+
83
+ def _match_hash(hash)
84
+ if hash.keys.include?(:to)
85
+ hash = merge_defaults(hash)
86
+ end
87
+
88
+ super(hash)
89
+ end
90
+
91
+ def merge_defaults(hash)
92
+ hash = defaults.merge(hash)
93
+ hash = hash.delete_if { |k,v| v == false }
94
+
95
+ # Order matters...
96
+ sorted = %i[to inject call_with].inject({}) do |retval, key|
97
+ retval[key] = hash[key] if hash[key]
98
+ retval
99
+ end
100
+
101
+ sorted
102
+ end
103
+
104
+ def defaults
105
+ env[KEY] || {}
106
+ end
107
+
59
108
  end
109
+
60
110
  end
61
111
 
62
112
  register_plugin(:flow, Flow)
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.3.1
4
+ version: 0.4.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: 2017-02-20 00:00:00.000000000 Z
11
+ date: 2018-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -110,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
110
  version: '0'
111
111
  requirements: []
112
112
  rubyforge_project:
113
- rubygems_version: 2.6.10
113
+ rubygems_version: 2.6.13
114
114
  signing_key:
115
115
  specification_version: 4
116
116
  summary: ''