case 0.3 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -4,7 +4,7 @@ require 'rake/rdoctask'
4
4
  require 'rake/gempackagetask'
5
5
  require 'rake/clean'
6
6
 
7
- GEM_VERSION = "0.3"
7
+ GEM_VERSION = "0.4"
8
8
 
9
9
  Rake::RDocTask.new do |task|
10
10
  task.rdoc_files.add [ 'lib/**/*.rb' ]
@@ -7,61 +7,58 @@
7
7
 
8
8
  module Case
9
9
 
10
- # Like regular Struct, except that Struct#=== performs a structural match
11
- class Struct < ::Struct
12
- def ===(other)
13
- return false unless self.class == other.class
14
- zip(other) { |a, b| return false unless a === b }
15
- true
10
+ module Impl
11
+ module Struct
12
+ def ===(obj)
13
+ return false unless self.class === obj
14
+ zip(obj) { |e,o| return false unless e === o }
15
+ true
16
+ end
16
17
  end
17
- end
18
18
 
19
- # Pattern-matching disjunction
20
- class Any
21
- class << self
22
- alias_method :[], :new
19
+ module Array
20
+ def ===(obj)
21
+ return false unless ::Array === obj
22
+ zip(obj) { |e,o| return false unless e === o }
23
+ true
24
+ end
23
25
  end
24
26
 
25
- def initialize(*options)
26
- @options = options
27
- end
27
+ class Operator
28
+ class << self
29
+ alias_method :[], :new
30
+ end
28
31
 
29
- def ===(obj)
30
- @options.any? { |e| e === obj }
32
+ def initialize(*patterns)
33
+ @patterns = patterns
34
+ end
35
+
36
+ def ===(obj)
37
+ raise NotImplementedError, "#{self.class}#=== not implemented"
38
+ end
31
39
  end
32
40
  end
33
41
 
34
- # Pattern-matching conjunction
35
- class All
36
- class << self
37
- alias_method :[], :new
38
- end
39
-
40
- def initialize(*patterns)
41
- @patterns = patterns
42
- end
42
+ class Any < Impl::Operator
43
+ def self.===(obj) ; true ; end
43
44
 
44
45
  def ===(obj)
45
- @patterns.all? { |e| e === obj }
46
+ @patterns.any? { |p| p === obj }
46
47
  end
47
48
  end
48
49
 
49
- if defined? ::Tuple
50
- class Tuple < ::Tuple
51
- def ===(other)
52
- return false unless ::Tuple === other and size == other.size
53
- to_a.zip(other) { |a, b| return false unless a === b }
54
- true
50
+ class All < Impl::Operator
51
+ def ===(obj)
52
+ @patterns.all? { |p| p === obj }
55
53
  end
56
54
  end
55
+
56
+ class Struct < ::Struct
57
+ include Impl::Struct
57
58
  end
58
59
 
59
60
  class Array < ::Array
60
- def ===(other)
61
- return false unless ::Array === other and size == other.size
62
- zip(other) { |a, b| return false unless a === b }
63
- true
64
- end
61
+ include Impl::Array
65
62
  end
66
63
 
67
64
  class Guard
@@ -69,8 +66,8 @@ class Guard
69
66
  @predicate = predicate
70
67
  end
71
68
 
72
- def ===(other)
73
- @predicate.call other
69
+ def ===(obj)
70
+ @predicate.call obj
74
71
  end
75
72
  end
76
73
 
@@ -0,0 +1,17 @@
1
+ # case/core.rb - pattern matching for Ruby (core supplement)
2
+ #
3
+ # Copyright 2008 MenTaLguY <mental@rydia.net>
4
+ #
5
+ # This library is made available under the same terms as Ruby.
6
+ #
7
+
8
+ require 'case'
9
+
10
+ class Struct
11
+ include Case::Impl::Struct
12
+ end
13
+
14
+ class Array
15
+ include Case::Impl::Array
16
+ end
17
+
@@ -0,0 +1 @@
1
+ require 'test/test_case.rb'
@@ -0,0 +1,14 @@
1
+ require 'test/unit'
2
+ require 'case'
3
+ require 'case/core'
4
+
5
+ class CaseTest < Test::Unit::TestCase
6
+ def test_struct
7
+ [ ::Struct, Case::Struct ].each do |s|
8
+ c = s.new :foo, :bar, :baz
9
+ assert c[1, 2, Object] === c[1, 2, 3]
10
+ assert !( c[1, 2, 3] === c[1, 2, Object] )
11
+ end
12
+ end
13
+ end
14
+
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: case
5
5
  version: !ruby/object:Gem::Version
6
- version: "0.3"
7
- date: 2008-01-17 00:00:00 -05:00
6
+ version: "0.4"
7
+ date: 2008-01-22 00:00:00 -05:00
8
8
  summary: Pattern matching for Ruby
9
9
  require_paths:
10
10
  - lib
@@ -30,7 +30,9 @@ authors:
30
30
  files:
31
31
  - Rakefile
32
32
  - test/test_all.rb
33
+ - test/test_case.rb
33
34
  - lib/case.rb
35
+ - lib/case/core.rb
34
36
  test_files:
35
37
  - test/test_all.rb
36
38
  rdoc_options: []