pattern-match 1.0.0 → 1.0.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ef7bf869eb224fa684f0d76e818765b7c29bac7a
4
- data.tar.gz: 8f0250bc045bc40226c470eeecc0d4cb19c9948e
3
+ metadata.gz: 7472cd3f781f3de13a150b3f7451f1b6c7acbebc
4
+ data.tar.gz: a400bedeed0e8e87ec9a0c56e283184083f34988
5
5
  SHA512:
6
- metadata.gz: da3c2aee6e261dc82bac97d578629b2d92a4449e6eb10ccd8f321fbd99330f2b505cde23df1e10080e7f9d2549ce40a9ba2c20c6e680d0fc6abe2c701cea3596
7
- data.tar.gz: 5c7108710be125a049fe4fde57a1292e6e820a87cb414ef6fe7db18ad6d339da2549659381ea018b17de6469adcf51cb90240fa532c8ea92b44d027f3065f847
6
+ metadata.gz: c94fa67c645a45c2e67d3c02a2e86e0612a61749ccbc9098469d784e5afa65891d9dc88311f3ae4b73a27c1fa252d0db31789ad4739f96840100a3633a95f98d
7
+ data.tar.gz: 3822c0b6c0d94237c589e1f15aa93818c399ffb677bdfce95c6e8ae5082ddd4fb51e5d00b35222e044b2bd9b3041319bbae5ad10f085d36bcf61e09e553932e9
data/.gitignore CHANGED
@@ -1,4 +1,5 @@
1
1
  .bundle
2
+ Gemfile.local
2
3
  Gemfile.lock
3
4
  coverage
4
5
  pkg/*
@@ -16,10 +16,6 @@ matrix:
16
16
  env: TEST="test/test_*"
17
17
  - rvm: 1.9.3
18
18
  env: TEST="test/test_standard.rb"
19
- - rvm: 2.0.0
20
- env: TEST="test/test_*"
21
- - rvm: 2.0.0
22
- env: TEST="test/test_standard.rb"
23
19
  - rvm: jruby-19mode
24
20
  env: TEST="test/test_*"
25
21
  - rvm: jruby-19mode
data/Gemfile CHANGED
@@ -1,3 +1,9 @@
1
1
  source "http://rubygems.org"
2
2
 
3
3
  gemspec
4
+
5
+ # https://github.com/redmine/redmine/blob/3.0.4/Gemfile#L101
6
+ local_gemfile = File.join(File.dirname(__FILE__), "Gemfile.local")
7
+ if File.exist?(local_gemfile)
8
+ eval_gemfile local_gemfile
9
+ end
data/LEGAL ADDED
@@ -0,0 +1,4 @@
1
+ LEGAL NOTICE INFORMATION
2
+ ------------------------
3
+
4
+ All the files in this distribution are written by the author.
@@ -1,5 +1,7 @@
1
1
  require 'pattern-match/deconstructor'
2
2
 
3
+ using PatternMatch if respond_to?(:using, true)
4
+
3
5
  module PatternMatch
4
6
  module HasOrderedSubPatterns
5
7
  private
@@ -16,6 +18,9 @@ module PatternMatch
16
18
  class Pattern
17
19
  attr_accessor :parent, :next, :prev
18
20
 
21
+ attr_reader :subpatterns
22
+ private :subpatterns
23
+
19
24
  def initialize(*subpatterns)
20
25
  @parent = nil
21
26
  @next = nil
@@ -25,7 +30,7 @@ module PatternMatch
25
30
  end
26
31
 
27
32
  def vars
28
- subpatterns.map(&:vars).flatten
33
+ subpatterns.flat_map(&:vars)
29
34
  end
30
35
 
31
36
  def ancestors
@@ -117,8 +122,6 @@ module PatternMatch
117
122
 
118
123
  private
119
124
 
120
- attr_reader :subpatterns
121
-
122
125
  def repeating_match(vals, is_greedy)
123
126
  quantifier = @next
124
127
  candidates = generate_candidates(vals)
@@ -200,8 +203,6 @@ module PatternMatch
200
203
  class PatternObjectDeconstructor < PatternDeconstructor
201
204
  include HasOrderedSubPatterns
202
205
 
203
- using PatternMatch if respond_to?(:using, true)
204
-
205
206
  def initialize(deconstructor, *subpatterns)
206
207
  super(*subpatterns)
207
208
  @deconstructor = deconstructor
@@ -483,14 +484,14 @@ module PatternMatch
483
484
  def with(pat_or_val, guard_proc = nil, &block)
484
485
  ctx = @ctx
485
486
  pat = pat_or_val.kind_of?(Pattern) ? pat_or_val : PatternValue.new(pat_or_val)
486
- pat.append(PatternCondition.new { check_for_duplicate_vars(pat.vars) })
487
+ pat.append(PatternCondition.new { validate_uniqueness_of_dup_vars(pat.vars) })
487
488
  if guard_proc
488
489
  pat.append(PatternCondition.new { with_quasibinding(ctx, pat.quasibinding, &guard_proc) })
489
490
  end
490
491
  pat.validate
491
492
  if pat.match([@val])
492
493
  ret = with_quasibinding(ctx, pat.quasibinding, &block)
493
- ::Kernel.throw(:exit_match, ret)
494
+ ::Kernel.throw(self, ret)
494
495
  else
495
496
  nil
496
497
  end
@@ -524,8 +525,6 @@ module PatternMatch
524
525
  when 0
525
526
  uscore = PatternVariable.new(:_)
526
527
  class << uscore
527
- using PatternMatch if respond_to?(:using, true)
528
-
529
528
  def [](*args)
530
529
  Array.(*args)
531
530
  end
@@ -568,17 +567,8 @@ module PatternMatch
568
567
  PatternNot.new(*subpatterns)
569
568
  end
570
569
 
571
- def check_for_duplicate_vars(vars)
572
- vars.each_with_object({}) do |v, h|
573
- if h.has_key?(v.name)
574
- unless h[v.name] == v.val
575
- return false
576
- end
577
- else
578
- h[v.name] = v.val
579
- end
580
- end
581
- true
570
+ def validate_uniqueness_of_dup_vars(vars)
571
+ vars.group_by(&:name).all? {|_, dup_vars| dup_vars.map(&:val).uniq.length == 1 }
582
572
  end
583
573
 
584
574
  class QuasiBindingModule < ::Module
@@ -630,26 +620,23 @@ module PatternMatch
630
620
  class NoMatchingPatternError < PatternMatchError; end
631
621
  class MalformedPatternError < PatternMatchError; end
632
622
 
633
- # Make Pattern and its subclasses/Env private.
634
- if respond_to?(:private_constant)
635
- constants.each do |c|
636
- klass = const_get(c)
637
- next unless klass.kind_of?(Class)
638
- if klass <= Pattern
639
- private_constant c
640
- end
623
+ # Make Pattern and its subclasses, etc private.
624
+ constants.each do |c|
625
+ klass = const_get(c)
626
+ next unless klass.kind_of?(Class)
627
+ if klass <= Pattern
628
+ private_constant c
641
629
  end
642
- private_constant :Env
643
630
  end
631
+ private_constant :Env, :HasOrderedSubPatterns
644
632
 
645
633
  refine Object do
646
-
647
634
  private
648
635
 
649
636
  def match(*vals, &block)
650
637
  do_match = Proc.new do |val|
651
638
  env = Env.new(self, val)
652
- catch(:exit_match) do
639
+ catch(env) do
653
640
  env.instance_eval(&block)
654
641
  raise NoMatchingPatternError
655
642
  end
@@ -8,10 +8,12 @@ module PatternMatch
8
8
  PatternObjectDeconstructor.new(self, *subpatterns)
9
9
  end
10
10
  end
11
+ end
11
12
 
12
- module Deconstructable
13
- using PatternMatch if respond_to?(:using, true)
13
+ using PatternMatch if respond_to?(:using, true)
14
14
 
15
+ module PatternMatch
16
+ module Deconstructable
15
17
  def call(*subpatterns)
16
18
  pattern_matcher(*subpatterns)
17
19
  end
@@ -1,10 +1,12 @@
1
1
  class Module
2
- methods = Module.instance_methods(false) + Module.private_instance_methods(false)
2
+ refine_orig = "__refine_orig_#{Time.now.to_i}".to_sym
3
+
4
+ methods = instance_methods(false) + private_instance_methods(false)
3
5
  if methods.include?(:refine)
4
- if methods.include?(:__refine_orig)
6
+ if methods.include?(refine_orig)
5
7
  raise LoadError, "can't re-define Module#refine"
6
8
  end
7
- alias_method :__refine_orig, :refine
9
+ alias_method refine_orig, :refine
8
10
  remove_method :refine
9
11
  end
10
12
 
@@ -17,9 +19,9 @@ class Module
17
19
  ensure
18
20
  remove_method :refine
19
21
 
20
- if Kernel.respond_to?(:__refine_orig, true)
21
- alias_method :refine, :__refine_orig
22
- remove_method :__refine_orig
22
+ if Kernel.respond_to?(refine_orig, true)
23
+ alias_method :refine, refine_orig
24
+ remove_method refine_orig
23
25
  end
24
26
  end
25
27
  end
@@ -4,6 +4,8 @@ require 'set'
4
4
 
5
5
  raise LoadError, 'Module#prepend required' unless Module.respond_to?(:prepend, true)
6
6
 
7
+ using PatternMatch if respond_to?(:using, true)
8
+
7
9
  module PatternMatch
8
10
  class Pattern
9
11
  module Backtrackable
@@ -50,8 +52,6 @@ module PatternMatch
50
52
  end
51
53
 
52
54
  module Deconstructable
53
- using PatternMatch if respond_to?(:using, true)
54
-
55
55
  remove_method :call
56
56
  def call(*subpatterns)
57
57
  if Object == self
@@ -180,8 +180,6 @@ class Hash
180
180
  end
181
181
 
182
182
  class Object
183
- using PatternMatch if respond_to?(:using, true)
184
-
185
183
  def assert_pattern(pattern)
186
184
  match(self) do
187
185
  Kernel.eval("with(#{pattern}) { self }", Kernel.binding)
@@ -1,3 +1,3 @@
1
1
  module PatternMatch
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -16,10 +16,11 @@ Gem::Specification.new do |s|
16
16
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
17
  s.executables = `git ls-files -- bin/*`.split("\n").map{|f| File.basename(f) }
18
18
  s.require_paths = ['lib']
19
- s.add_development_dependency 'test-unit'
20
- s.add_development_dependency 'rake'
21
- s.add_development_dependency 'simplecov'
22
19
  s.extra_rdoc_files = ['README.rdoc']
23
20
  s.rdoc_options = ['--main', 'README.rdoc']
24
21
  s.licenses = ['2-clause BSDL', "Ruby's"]
22
+
23
+ s.add_development_dependency 'test-unit'
24
+ s.add_development_dependency 'rake'
25
+ s.add_development_dependency 'simplecov'
25
26
  end
@@ -6,6 +6,12 @@ module Test::Unit::Assertions
6
6
  end
7
7
  end
8
8
 
9
+ module TestUtils
10
+ def eval_in_unrefined_scope(expr)
11
+ eval(expr)
12
+ end
13
+ end
14
+
9
15
  begin
10
16
  if ENV['COVERAGE']
11
17
  require 'simplecov'
@@ -1,14 +1,14 @@
1
1
  require_relative 'helper'
2
2
  require 'test-unit'
3
3
  if ENV['DISABLE_REFINEMENTS']
4
- require_relative '../lib/pattern-match/disable_refinements'
5
- require_relative '../lib/pattern-match'
4
+ require 'pattern-match/disable_refinements'
5
+ require 'pattern-match'
6
6
  else
7
- require_relative '../lib/pattern-match'
7
+ require 'pattern-match'
8
8
  using PatternMatch
9
9
  end
10
10
  begin
11
- require_relative '../lib/pattern-match/experimental'
11
+ require 'pattern-match/experimental'
12
12
  rescue LoadError
13
13
  end
14
14
 
@@ -1,14 +1,16 @@
1
1
  require_relative 'helper'
2
2
  require 'test-unit'
3
3
  if ENV['DISABLE_REFINEMENTS']
4
- require_relative '../lib/pattern-match/disable_refinements'
5
- require_relative '../lib/pattern-match'
4
+ require 'pattern-match/disable_refinements'
5
+ require 'pattern-match'
6
6
  else
7
- require_relative '../lib/pattern-match'
7
+ require 'pattern-match'
8
8
  using PatternMatch
9
9
  end
10
10
 
11
11
  class TestStandard < Test::Unit::TestCase
12
+ include TestUtils
13
+
12
14
  def test_basic
13
15
  this = self
14
16
  ret = match([0, 1, 2, 3]) do
@@ -631,14 +633,14 @@ class TestStandard < Test::Unit::TestCase
631
633
 
632
634
  def test_refinements
633
635
  if ENV['DISABLE_REFINEMENTS']
634
- assert_kind_of(PatternMatch.const_get(:Pattern), eval('Class.()', TOPLEVEL_BINDING))
635
- assert_equal(0, eval('match(0) { with(_) { 0 } }', TOPLEVEL_BINDING))
636
+ assert_kind_of(PatternMatch.const_get(:Pattern), eval_in_unrefined_scope('Class.()'))
637
+ assert_equal(0, eval_in_unrefined_scope('match(0) { with(_) { 0 } }'))
636
638
  else
637
639
  assert_raises(NoMethodError) do
638
- eval('Class.()', TOPLEVEL_BINDING)
640
+ eval_in_unrefined_scope('Class.()')
639
641
  end
640
642
  assert_raises(NoMethodError) do
641
- eval('match(0) { with(_) { 0 } }', TOPLEVEL_BINDING)
643
+ eval_in_unrefined_scope('match(0) { with(_) { 0 } }')
642
644
  end
643
645
  end
644
646
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pattern-match
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kazuki Tsujimoto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-07 00:00:00.000000000 Z
11
+ date: 2015-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: test-unit
@@ -65,6 +65,7 @@ files:
65
65
  - BSDL
66
66
  - COPYING
67
67
  - Gemfile
68
+ - LEGAL
68
69
  - README.rdoc
69
70
  - Rakefile
70
71
  - lib/pattern-match.rb
@@ -100,7 +101,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
101
  version: '0'
101
102
  requirements: []
102
103
  rubyforge_project:
103
- rubygems_version: 2.4.5
104
+ rubygems_version: 2.4.6
104
105
  signing_key:
105
106
  specification_version: 4
106
107
  summary: A pattern matching library