method_pattern 0.1.0 → 0.1.1

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: 422890fc9550a6b721a10c8202183520526cd9bc
4
- data.tar.gz: 476ff0fc0a52db7e699998714603f9db3867458e
3
+ metadata.gz: ad178a56d4c759b0776f91601fde1f40eff08221
4
+ data.tar.gz: 9d6117c475302d52b9047edcd87efa69db7b27e4
5
5
  SHA512:
6
- metadata.gz: 9970b20986dee6229c22daed99db1a5332533126700abd6401510422db9cefdf3b46a03e8ee3a9eb333c989b35a37e662b5de1ecf3783329e4754b298fea1c71
7
- data.tar.gz: d35dcabc7023e7445f26c67607113b90c07fc2f85a0fed555b32f862d984d747d90f543fe39d384abf6cb89bc9d38805ee34ff1ae41bae40cf25964ae94529c9
6
+ metadata.gz: b3ce4ef3f75239681c4939961a8c6d0102a8f20d7a3579a1a88fbbe0be9951092981a1997b4d0b9f7b4ac6b7f7fe651ca6d2e90cde6afcff2ccfb8a3827fbeca
7
+ data.tar.gz: b151bc6df67519d9e88af1dc24726e825d5243a2ed1b097895613e662501b6d2d26262b40488820aa55fe07f311f221667075e2991009018663b09c65cebf135
data/README.md CHANGED
@@ -57,14 +57,19 @@ This example will handle 0 and 1 as special cases and all other integers are fun
57
57
 
58
58
  Notice that we could pass in a class or even a range for our pattern. There are several things we can use:
59
59
 
60
- - Strings: `with('hello') { ... }`
61
- - Numbers: `with(15) { ... }`
62
- - Symbol: `with(:foo) { ... }`
63
- - Class: `with(Integer) { ... }`
64
- - Regex: `with(/foo/) { ... }`
65
- - Range: `with(0...10) { ... }`
66
-
67
- It's not just for single arguments"
60
+ - Strings: `with('hello') { |str| ... }` matches an exact string
61
+ - Numbers: `with(15) { |num| ... }` matches an exact number
62
+ - Symbol: `with(:foo) { |sym| ... }` matches a particular symbol
63
+ - Class: `with(Integer) { |num| ... }` matches any instance of the given class
64
+ - Regex: `with(/foo/) { |str| ... }` matches any string that matches the regex
65
+ - Range: `with(0...10) { |num| ... }` matches any value covered by the range
66
+ - Proc/lambda: `with(-> n { n > 3 }) { |n| ... }` matches if the proc returns a truthy value
67
+
68
+ Note that the method arguments are passed to the block. This lets the block become the method body.
69
+
70
+ ### It's not just for single arguments
71
+
72
+ You can pass multiple patterns to `with` and it will match them in order:
68
73
 
69
74
  ```ruby
70
75
  defn :baz do
@@ -6,7 +6,7 @@ module MethodPattern
6
6
  fn.instance_exec(&definition)
7
7
 
8
8
  define_method name do |*args, &block|
9
- instance_exec(*args, &fn.match(*args))
9
+ instance_exec(*args, &fn.match(args))
10
10
  end
11
11
  end
12
12
 
@@ -21,12 +21,12 @@ module MethodPattern
21
21
  end
22
22
 
23
23
  def with *patterns, &block
24
- @patterns << Pattern.new(*patterns, block)
24
+ @patterns << Pattern.new(patterns, block)
25
25
  end
26
26
 
27
- def match *args
27
+ def match args
28
28
  @patterns.each do |pattern|
29
- if pattern.match? *args
29
+ if pattern.match? args
30
30
  return pattern.block
31
31
  end
32
32
  end
@@ -37,15 +37,17 @@ module MethodPattern
37
37
  class Pattern
38
38
  attr_reader :accepted, :block
39
39
 
40
- def initialize *accepted, block
40
+ def initialize accepted, block
41
41
  @accepted = accepted
42
42
  @block = block
43
43
  end
44
44
 
45
- def match? *args
46
- @accepted.each_with_index.reduce(true) do |result, (pattern, index)|
47
- result && match_arg?(pattern, args[index])
45
+ def match? args
46
+ @accepted.each_with_index do |pattern, index|
47
+ return false unless match_arg?(pattern, args[index])
48
48
  end
49
+
50
+ true
49
51
  end
50
52
 
51
53
  def match_arg? pattern, arg
@@ -53,9 +55,11 @@ module MethodPattern
53
55
  when Hash
54
56
  return false unless arg.is_a? Hash
55
57
 
56
- pattern.reduce(true) do |result, (key, value)|
57
- result && arg.key?(key) && match_arg?(value, arg[key])
58
+ pattern.each do |key, value|
59
+ return false unless arg.key?(key) && match_arg?(value, arg[key])
58
60
  end
61
+
62
+ true
59
63
  else
60
64
  pattern === arg
61
65
  end
@@ -1,3 +1,3 @@
1
1
  module MethodPattern
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: method_pattern
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jamie Gaskins
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-03-25 00:00:00.000000000 Z
11
+ date: 2018-03-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler