kase 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8e196cce0e928c43b3b1fed7042abbe6f99e975c
4
- data.tar.gz: 2b8cfd61df68dc2cb32bfad689e7f4151ed94bb0
3
+ metadata.gz: 8246d6d187ce89e4d5949b8822802a0ebed2373f
4
+ data.tar.gz: fb36c07046d8f881ad58145a18a9cf0766d6edfd
5
5
  SHA512:
6
- metadata.gz: f9dffb5a26c729de9f6a6f8984a126708035034b544fe3d626073a1a01f86b83f244150e3ae2923ddef3b0bcbd839990b25402d56717d4a6f8fa59b24e00edd2
7
- data.tar.gz: 8dcd7eb8dceebe67c2e0487b77d7095b0535eb431409d21141ac0aa07d2fa2a05a9af5c79927634ba4c31833ff4f25db4d5cbabc72eafcf8fd67f61757371ad5
6
+ metadata.gz: 4c30df36d99464959b9e431eb2cf531d013de3f8a48ca8ad0993ead3abaf6b0f353a1bd16f9f39a4e9b4f67a97ddb11ac00ca2376333f4f7fbf5435ff787372b
7
+ data.tar.gz: 813455dd6fdabff84994e2d5274ab8c56f85c4c1ca34721889036ae9130ae2f55a8f5f17e2791f293389ec7b844ee839d90be30771badd6b281526461b4efab2
@@ -2,6 +2,12 @@
2
2
 
3
3
  * (Your contributions here)
4
4
 
5
+ # Version 0.1.3 (2016-04-10)
6
+
7
+ * Simpler and safer DSL that runs in the context of the calling code,
8
+ [#6](https://github.com/lasseebert/kase/issues/6),
9
+ [lasseebert](https://github.com/lasseebert) and [hosh](https://github.com/hosh)
10
+
5
11
  # Version 0.1.2 (2016-04-06)
6
12
 
7
13
  * Fixed instance variables lookup inside DSL, [#4](https://github.com/lasseebert/kase/issues/4), [lasseebert](https://github.com/lasseebert)
data/README.md CHANGED
@@ -16,28 +16,24 @@ The idea is inspired by Elixir in which many functions returns something like
16
16
  In Ruby we would usually handle those kind of return values like this:
17
17
 
18
18
  ```ruby
19
- class Orders
20
- def process(cart)
21
- status, result, message = complete_order(cart)
22
-
23
- case status
24
- when :ok
25
- order = result
26
- process_order(order)
27
- when :error
28
- error_kind = result
29
- case error_kind
30
- when :not_found
31
- [404, {}, "Not found"]
32
- when :invalid_state
33
- [400, {}, "Invalid request: #{message}"]
34
- else
35
- raise "Unhandled error kind: #{error_kind}"
36
- end
37
- else
38
- raise "Unhandles status: #{status}"
39
- end
19
+ status, result, message = complete_order(cart)
20
+
21
+ case status
22
+ when :ok
23
+ order = result
24
+ process_order(order)
25
+ when :error
26
+ error_kind = result
27
+ case error_kind
28
+ when :not_found
29
+ [404, {}, "Not found"]
30
+ when :invalid_state
31
+ [400, {}, "Invalid request: #{message}"]
32
+ else
33
+ raise "Unhandled error kind: #{error_kind}"
40
34
  end
35
+ else
36
+ raise "Unhandles status: #{status}"
41
37
  end
42
38
  ```
43
39
 
@@ -50,25 +46,17 @@ handled that specific status or error_kind).
50
46
  With Kase we can do this instead, which is equivalent to the above:
51
47
 
52
48
  ```ruby
53
- require "kase"
54
-
55
- class Orders
56
- include Kase
57
-
58
- def process(cart)
59
- kase complete_order(cart) do
60
- on :ok do |order|
61
- process_order(order)
62
- end
49
+ kase complete_order(cart) do
50
+ on :ok do |order|
51
+ process_order(order)
52
+ end
63
53
 
64
- on :error, :not_found do
65
- [404, {}, "Not found"]
66
- end
54
+ on :error, :not_found do
55
+ [404, {}, "Not found"]
56
+ end
67
57
 
68
- on :error, :invalid_state do |message|
69
- [400, {}, "Invalid request: #{message}"]
70
- end
71
- end
58
+ on :error, :invalid_state do |message|
59
+ [400, {}, "Invalid request: #{message}"]
72
60
  end
73
61
  end
74
62
  ```
@@ -112,7 +100,9 @@ pattern. E.g. if `[:ok, "THE RESULT"]` is matched with `on(:ok, &block)`,
112
100
  #### Simple examples:
113
101
 
114
102
  ```ruby
115
- kase process_order do
103
+ require "kase"
104
+
105
+ Kase.kase process_order do
116
106
  on :ok do
117
107
  puts "Great success!"
118
108
  end
@@ -140,7 +130,9 @@ than one value is returned.
140
130
  All values that are not part of the pattern will be yielded to the given block:
141
131
 
142
132
  ```ruby
143
- kase process_order do
133
+ require "kase"
134
+
135
+ Kase.kase process_order do
144
136
  on :ok do |order|
145
137
  puts "Great success: #{order.inspect}"
146
138
  end
@@ -159,6 +151,8 @@ be able to catch and use the values.
159
151
  We can match on multiple values, but only from the left:
160
152
 
161
153
  ```ruby
154
+ require "kase"
155
+
162
156
  kase process_order do
163
157
  on :ok do |order|
164
158
  puts "Great success: #{order.inspect}"
@@ -201,6 +195,20 @@ ok! something do |result|
201
195
  end
202
196
  ```
203
197
 
198
+ Or this:
199
+
200
+ ```ruby
201
+ result = kase something do
202
+ on(:ok) { |result| result }
203
+ end
204
+ ```
205
+
206
+ To this:
207
+
208
+ ```ruby
209
+ result = ok! something
210
+ ```
211
+
204
212
  ### Include or module_function
205
213
 
206
214
  Kase is a module with helper methods. You can either include it in your own
@@ -228,6 +236,9 @@ class MySecondClass
228
236
  end
229
237
  ```
230
238
 
239
+ Note that `#kase` is aliased to `#call` so you can use the shorthand
240
+ `Kase.(values)`.
241
+
231
242
  All the logic resides in the Kase::Switcher class which you can use directly if
232
243
  you need to:
233
244
 
@@ -248,9 +259,6 @@ result = Kase.kase :ok, "RESULT" do
248
259
  end
249
260
  ```
250
261
 
251
- Note that `#kase` is aliased to `#call` so you can use the shorthand
252
- `Kase.(values)`.
253
-
254
262
  ## Development
255
263
 
256
264
  * Install development dependencies with `bundle`
@@ -271,8 +279,8 @@ A pull request should consist of
271
279
 
272
280
  * At least one failing test that proves the bug or documents the feature.
273
281
  * The implementation of the bugfix or feature
274
- * A line in the `CHANGELOG.md` with a description of the change, a link to the pull
275
- request and a link to your github user.
282
+ * A line in the `CHANGELOG.md` with a description of the change, a link to your
283
+ github user and, if this closes or references an issue, a link to the issue.
276
284
 
277
285
  ## Contact
278
286
 
@@ -34,39 +34,45 @@ module Kase
34
34
  end
35
35
 
36
36
  def switch(&block)
37
- context = eval("self", block.binding)
38
- dsl = DSL.new(self, context)
39
- dsl.__call(&block)
37
+ DSL.call(self, &block)
40
38
  validate!
41
39
  result
42
40
  end
43
41
 
44
- class DSL
45
- def initialize(switcher, context)
46
- @__switcher = switcher
47
- @__context = context
48
- end
42
+ module DSL
43
+ module_function
49
44
 
50
- def __call(&block)
51
- @__context.instance_variables.each do |name|
52
- next if name.to_s =~ /^@__/
53
- instance_variable_set(name, @__context.instance_variable_get(name))
54
- end
45
+ def call(switcher, &block)
46
+ context = eval("self", block.binding)
47
+ original_on_method = context.method(:on) if defined? context.on
48
+ new_on_method = nil
49
+
50
+ # Define a new :on method for the caller context
51
+ context.define_singleton_method(:on) do |*pattern, &inner_block|
55
52
 
56
- instance_eval(&block)
53
+ new_inner_block = proc do |*args|
54
+ # Use the original :on method inside the inner blocks
55
+ DSL.set_on_method(context, original_on_method)
56
+ result = inner_block.call(*args)
57
+ DSL.set_on_method(context, new_on_method)
58
+ result
59
+ end
57
60
 
58
- instance_variables.each do |name|
59
- next if name.to_s =~ /^@__/
60
- @__context.instance_variable_set(name, instance_variable_get(name))
61
+ switcher.on(*pattern, &new_inner_block)
61
62
  end
62
- end
63
+ new_on_method = context.method(:on)
63
64
 
64
- def on(*args, &block)
65
- @__switcher.on(*args, &block)
65
+ block.call
66
+ ensure
67
+ DSL.set_on_method(context, original_on_method)
66
68
  end
67
69
 
68
- def method_missing(method, *args, &block)
69
- @__context.send(method, *args, &block)
70
+ def set_on_method(context, method)
71
+ if method
72
+ context.define_singleton_method(:on, method)
73
+ else
74
+ context.instance_eval { undef :on if defined? on }
75
+ end
70
76
  end
71
77
  end
72
78
  end
@@ -1,3 +1,3 @@
1
1
  module Kase
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kase
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lasse Skindstad Ebert
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-04-06 00:00:00.000000000 Z
11
+ date: 2016-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler