smart_case 0.0.2 → 0.0.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: 7a6e289ec9557517f4783010ac04df00d3d2e572
4
- data.tar.gz: 39adfcc359f7dfcf6bdecff9cb4157533be1c642
3
+ metadata.gz: f5b22acb17f4e40fc1571ed077eb53bfd6ff4c29
4
+ data.tar.gz: 9d4ca1cb3deca1e54c4494d68fec4b1c5e69ad01
5
5
  SHA512:
6
- metadata.gz: b739faaa1d1a0c722b43b18dd8a3386903450047e0dedbcc3d7af189fd44952184204a6b043eba1faf85654880e926a784349138f4083ceb6f73d042f83b57a9
7
- data.tar.gz: 104c486bfd03c2957ec3e764991d9fd2ff871c9cf20b03356b4fde8bd37797a3a5db953f0a7eaf793d6c8f95a09a452aede62e09468f107acc304f401141a40f
6
+ metadata.gz: 0c82c6d5edb83a51f8b771aa4f2dd1ffde7ffdda3f499878fa3c6698bf43fd4ee3c786a21970fb335f82eeaed442a1d706b488528f76ff9671d16dca45217fe5
7
+ data.tar.gz: 999d841cf81673fcd2e5c1a1fdae11d6d43a3f5b485b528960e3600c8543b0b42c81105108c4411182845332c4ae106e7d4487c300ad9710e8d49242aef4216d
data/README.md CHANGED
@@ -2,7 +2,7 @@ smart_case
2
2
  ==========
3
3
  A while ago came up with the idea that it would be cool if ruby `case` statements accepted blocks for the condition evaluation. `SmartCase` is the result.
4
4
 
5
- ### Installation
5
+ ### Installation [![Gem Version](https://badge.fury.io/rb/smart_case.png)](http://badge.fury.io/rb/smart_case)
6
6
 
7
7
  ```bash
8
8
  $ gem install smart_case
@@ -62,6 +62,18 @@ sm.instance_eval do
62
62
  end
63
63
  => "Your number is 5!"
64
64
  ```
65
+ Pass `multi: true` option to get an array of results, instead of breaking execution when an `w` statement evaluates to `true`:
66
+
67
+ ```ruby
68
+ smart_case(10, multi: true) do
69
+ w { |x| x == 10 }
70
+ t { 'Your number is 10!' }
71
+
72
+ w { |x| x < 100 }
73
+ t { 'Your number is lesser than 100!' }
74
+ end
75
+ => ["Your number is 10!", "Your number is lesser than 100!"]
76
+ ```
65
77
 
66
78
  ### Contributing to smart_case
67
79
  + Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
@@ -74,4 +86,4 @@ end
74
86
 
75
87
  ### Copyright
76
88
  Copyright (c) 2013 Nicolas Oga. See LICENSE.txt for
77
- further details.
89
+ further details.
data/Rakefile CHANGED
@@ -21,7 +21,7 @@ Jeweler::Tasks.new do |gem|
21
21
  gem.description = %Q{Better case statements for ruby}
22
22
  gem.email = "2112.oga@gmail.com"
23
23
  gem.authors = ["Nicolas Oga"]
24
- gem.version = '0.0.2'
24
+ gem.version = '0.0.3'
25
25
  # dependencies defined in Gemfile
26
26
  end
27
27
  Jeweler::RubygemsDotOrgTasks.new
data/benchmarking.rb ADDED
@@ -0,0 +1,31 @@
1
+ require 'smart_case'
2
+ require 'benchmark'
3
+
4
+ Benchmark.bmbm do |x|
5
+ n = rand(1..100)
6
+
7
+ x.report('traditional case') do
8
+ 1000.times do
9
+ case n
10
+ when 1..33 then 'Your number is between 1 and 33'
11
+ when 33..66 then 'Your number is between 33 and 66'
12
+ when 66..100 then 'Your number is between 66 and 100'
13
+ end
14
+ end
15
+ end
16
+
17
+ x.report('smart_case') do
18
+ 1000.times do
19
+ smart_case n do
20
+ w { |x| (1..33) === x }
21
+ t { 'Your number is between 1 and 33' }
22
+
23
+ w { |x| (33..66) === x }
24
+ t { 'Your number is between 33 and 66' }
25
+
26
+ w { |x| (66..100) === x }
27
+ t { 'Your number is between 66 and 100' }
28
+ end
29
+ end
30
+ end
31
+ end
data/lib/smart_case.rb CHANGED
@@ -6,16 +6,26 @@ class SmartCase
6
6
  end
7
7
 
8
8
  def w(proc = nil) @when << (proc || Proc.new) end
9
- def t(proc = nil) @then << (proc || Proc.new) end
9
+ def t(proc = nil) @then << (proc || Proc.new) end
10
10
 
11
- def call(o = nil)
12
- @when.zip(@then).each do |w, t|
13
- return t.call(o || @o) if w.call(o || @o)
11
+ def call(o = nil, multi: nil)
12
+ raise ArgumentError unless @when.size == @then.size
13
+
14
+ if multi
15
+ result = @when.zip(@then).map do |w, t|
16
+ w.call(o || @o) ? t.call(o || @o) : nil
17
+ end
18
+ return result
19
+ else
20
+ @when.zip(@then).each do |w, t|
21
+ return t.call(o || @o) if w.call(o || @o)
22
+ end
14
23
  end
24
+
15
25
  return nil
16
26
  end
17
27
  end
18
28
 
19
- def smart_case(o)
20
- SmartCase.new(o, &Proc.new).call
29
+ def smart_case(o, multi: nil, &block)
30
+ SmartCase.new(o, &block).call(multi: multi)
21
31
  end
data/smart_case.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "smart_case"
8
- s.version = "0.0.2"
8
+ s.version = "0.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Nicolas Oga"]
@@ -24,6 +24,7 @@ Gem::Specification.new do |s|
24
24
  "LICENSE.txt",
25
25
  "README.md",
26
26
  "Rakefile",
27
+ "benchmarking.rb",
27
28
  "lib/smart_case.rb",
28
29
  "lib/version.rb",
29
30
  "smart_case.gemspec",
@@ -38,4 +38,27 @@ describe SmartCase do
38
38
  end).to eq("Your number is #{x}!")
39
39
  end
40
40
  end
41
+
42
+ it 'should fail with bad syntax' do
43
+ expect do
44
+ smart_case(Object.new) do
45
+ w { |o| o == x }
46
+ t { "Your number is #{x}!" }
47
+
48
+ w { |o| o == 3 }
49
+ end
50
+ end.to raise_error(ArgumentError)
51
+ end
52
+
53
+ it 'should have a multlipart feature' do
54
+ sm = smart_case(10, multi: true) do
55
+ w { |x| x == 10 }
56
+ t { 'Your number is 10!' }
57
+
58
+ w { |x| x < 100 }
59
+ t { 'Your number is lesser than 100!' }
60
+ end
61
+
62
+ expect(sm).to eq(['Your number is 10!', 'Your number is lesser than 100!'])
63
+ end
41
64
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smart_case
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicolas Oga
@@ -81,6 +81,7 @@ files:
81
81
  - LICENSE.txt
82
82
  - README.md
83
83
  - Rakefile
84
+ - benchmarking.rb
84
85
  - lib/smart_case.rb
85
86
  - lib/version.rb
86
87
  - smart_case.gemspec