modalsupport 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -18,4 +18,7 @@ coverage
18
18
  rdoc
19
19
  pkg
20
20
 
21
+ ## Rubinius
22
+ *.rbc
23
+
21
24
  ## PROJECT::SPECIFIC
data/Rakefile CHANGED
@@ -10,7 +10,7 @@ begin
10
10
  gem.email = "jgoizueta@gmail.com"
11
11
  gem.homepage = "http://github.com/jgoizueta/modalsupport"
12
12
  gem.authors = ["Javier Goizueta"]
13
- gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
13
+ gem.add_development_dependency "shoulda"
14
14
  gem.add_dependency "hobosupport"
15
15
  gem.add_dependency "activesupport"
16
16
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
@@ -1,6 +1,12 @@
1
- require 'activesupport'
1
+ require 'active_support'
2
2
  require 'hobosupport'
3
3
  require 'modalsupport/string'
4
4
  require 'modalsupport/regexp'
5
5
  require 'modalsupport/enumerable'
6
6
  require 'modalsupport/file'
7
+
8
+ module ModalSupport
9
+ end
10
+
11
+ require 'modalsupport/mixins/state_equivalent'
12
+ require 'modalsupport/mixins/bracket_constructor'
@@ -3,7 +3,7 @@ module Enumerable
3
3
  def grep_each(pattern)
4
4
  grep(pattern){|str| yield(Regexp.last_match)}
5
5
  end
6
-
6
+
7
7
  if RUBY_VERSION < "1.8.7"
8
8
  require 'enumerator'
9
9
  alias enumerator_each_slice each_slice
@@ -16,16 +16,31 @@ module Enumerable
16
16
  end
17
17
  end
18
18
  end
19
-
19
+
20
20
  def each_pair(&blk)
21
21
  each_slice(2, &blk)
22
22
  end
23
-
23
+
24
24
  # Convert to pairs [[e1,e2], [e3,e4], ...]
25
25
  # Note that for a Hash, this is equivalent to hash.to_a.to_pairs which may not be what's intended; hash.to_a is
26
26
  # an array of key-value pairs; to_pairs is an array of pairs of key-value pairs.
27
27
  def to_pairs
28
28
  each_pair.to_a
29
29
  end
30
-
30
+
31
+ # cartesian product
32
+ # note that for Ruby >= 1.8.7 a.each_product_pair(b).to_a == a.to_a.product(b.to_a)
33
+ def each_product_pair(other)
34
+ if block_given?
35
+ self.each do |this|
36
+ other.each do |that|
37
+ yield [this, that]
38
+ end
39
+ end
40
+ else
41
+ enum_for(:each_product_pair, other)
42
+ end
43
+ end
44
+
45
+
31
46
  end
@@ -0,0 +1,19 @@
1
+ # Mixin that adds a [] operator constructor to a class
2
+ module ModalSupport::BracketConstructor
3
+
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def [](*args)
10
+ if args.size==1
11
+ arg = args.first
12
+ return nil if arg.nil?
13
+ return arg if arg.kind_of?(self)
14
+ end
15
+ self.new(*args)
16
+ end
17
+ end
18
+
19
+ end
@@ -0,0 +1,36 @@
1
+ module ModalSupport::StateEquivalent
2
+
3
+ def ==(obj)
4
+ test_equal(obj)
5
+ end
6
+
7
+ def eql?(obj)
8
+ test_equal(obj)
9
+ end
10
+
11
+ def ===(obj)
12
+ test_equal(obj)
13
+ end
14
+
15
+ def hash
16
+ h = 0
17
+ self.instance_variables.each do |var|
18
+ v = self.instance_variable_get(var)
19
+ h ^= v.hash unless v.nil?
20
+ end
21
+ h
22
+ end
23
+
24
+ private
25
+
26
+ def test_equal(other)
27
+ return false unless self.class == other.class
28
+ (self.instance_variables + other.instance_variables).uniq.each do |var|
29
+ v1 = self.instance_variable_get(var)
30
+ v2 = other.instance_variable_get(var)
31
+ return false unless v1 == v2
32
+ end
33
+ true
34
+ end
35
+
36
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{modalsupport}
8
- s.version = "0.2.0"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Javier Goizueta"]
12
- s.date = %q{2010-02-05}
12
+ s.date = %q{2010-06-09}
13
13
  s.description = %q{additional support extensions to ActiveSupport and HoboSupport}
14
14
  s.email = %q{jgoizueta@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -26,29 +26,37 @@ Gem::Specification.new do |s|
26
26
  "lib/modalsupport.rb",
27
27
  "lib/modalsupport/enumerable.rb",
28
28
  "lib/modalsupport/file.rb",
29
+ "lib/modalsupport/mixins/bracket_constructor.rb",
30
+ "lib/modalsupport/mixins/state_equivalent.rb",
29
31
  "lib/modalsupport/regexp.rb",
30
32
  "lib/modalsupport/string.rb",
31
33
  "modalsupport.gemspec",
32
34
  "test/helper.rb",
35
+ "test/test_bracket_constructor.rb",
33
36
  "test/test_grep.rb",
34
37
  "test/test_gsub.rb",
35
38
  "test/test_match.rb",
39
+ "test/test_product.rb",
36
40
  "test/test_relative_path.rb",
37
41
  "test/test_slice.rb",
42
+ "test/test_state_equivalent.rb",
38
43
  "test/test_unindent.rb"
39
44
  ]
40
45
  s.homepage = %q{http://github.com/jgoizueta/modalsupport}
41
46
  s.rdoc_options = ["--charset=UTF-8"]
42
47
  s.require_paths = ["lib"]
43
- s.rubygems_version = %q{1.3.5}
48
+ s.rubygems_version = %q{1.3.7}
44
49
  s.summary = %q{simple extensions to core classes}
45
50
  s.test_files = [
46
51
  "test/helper.rb",
52
+ "test/test_bracket_constructor.rb",
47
53
  "test/test_grep.rb",
48
54
  "test/test_gsub.rb",
49
55
  "test/test_match.rb",
56
+ "test/test_product.rb",
50
57
  "test/test_relative_path.rb",
51
58
  "test/test_slice.rb",
59
+ "test/test_state_equivalent.rb",
52
60
  "test/test_unindent.rb"
53
61
  ]
54
62
 
@@ -56,17 +64,17 @@ Gem::Specification.new do |s|
56
64
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
57
65
  s.specification_version = 3
58
66
 
59
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
60
- s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
67
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
68
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
61
69
  s.add_runtime_dependency(%q<hobosupport>, [">= 0"])
62
70
  s.add_runtime_dependency(%q<activesupport>, [">= 0"])
63
71
  else
64
- s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
72
+ s.add_dependency(%q<shoulda>, [">= 0"])
65
73
  s.add_dependency(%q<hobosupport>, [">= 0"])
66
74
  s.add_dependency(%q<activesupport>, [">= 0"])
67
75
  end
68
76
  else
69
- s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
77
+ s.add_dependency(%q<shoulda>, [">= 0"])
70
78
  s.add_dependency(%q<hobosupport>, [">= 0"])
71
79
  s.add_dependency(%q<activesupport>, [">= 0"])
72
80
  end
@@ -0,0 +1,31 @@
1
+ require 'helper'
2
+
3
+ class TestBracketConstructor < Test::Unit::TestCase
4
+
5
+ context "Mixing-in BracketConstructor" do
6
+
7
+ class TestClass
8
+ def initialize(v)
9
+ @value = v
10
+ end
11
+ attr_reader :value
12
+ include ModalSupport::BracketConstructor
13
+ end
14
+
15
+ should "preserve nils" do
16
+ assert_nil TestClass[nil]
17
+ end
18
+
19
+ should "preserve objects of the class" do
20
+ x = TestClass.new(10)
21
+ # assert_equal x.value, TestClass[x].value
22
+ assert_equal x.object_id, TestClass[x].object_id
23
+ end
24
+
25
+ should "create new object" do
26
+ assert_equal 11, TestClass[11].value
27
+ end
28
+
29
+ end
30
+
31
+ end
@@ -0,0 +1,58 @@
1
+ require 'helper'
2
+
3
+ class TestProduct < Test::Unit::TestCase
4
+
5
+ context "Enumerable objects" do
6
+
7
+ result = [[1, 1], [1, 2], [1, 3],
8
+ [2, 1], [2, 2], [2, 3],
9
+ [3, 1], [3, 2], [3, 3],
10
+ [4, 1], [4, 2], [4, 3],
11
+ [5, 1], [5, 2], [5, 3]]
12
+
13
+ should "have cartesian product iterator" do
14
+ p = []
15
+ (1..5).each_product_pair(1..3) do |pair|
16
+ p << pair
17
+ end
18
+ assert_equal result, p
19
+
20
+ p = []
21
+ (1..5).to_a.each_product_pair((1..3).to_a) do |pair|
22
+ p << pair
23
+ end
24
+ assert_equal result, p
25
+
26
+ p = []
27
+ (1..5).each_product_pair(1..3) do |x,y|
28
+ p << [x,y]
29
+ end
30
+ assert_equal result, p
31
+
32
+ end
33
+
34
+ should "have exernal cartesian product iterator" do
35
+ it = (1..5).each_product_pair(1..3)
36
+ p = []
37
+ it.each do |pair|
38
+ p << pair
39
+ end
40
+ assert_equal result, p
41
+
42
+ it = (1..5).to_a.each_product_pair((1..3).to_a)
43
+ p = []
44
+ it.each do |pair|
45
+ p << pair
46
+ end
47
+ assert_equal result, p
48
+
49
+ assert_equal result, (1..5).each_product_pair(1..3).to_a
50
+ assert_equal result, (1..5).to_a.each_product_pair(1..3).to_a
51
+ assert_equal result, (1..5).each_product_pair((1..3).to_a).to_a
52
+ assert_equal result, (1..5).to_a.each_product_pair((1..3).to_a).to_a
53
+
54
+ end
55
+
56
+ end
57
+
58
+ end
@@ -0,0 +1,35 @@
1
+ require 'helper'
2
+
3
+ class TestStateEquivalent < Test::Unit::TestCase
4
+
5
+ context "Mixing-in StateEquivalent" do
6
+
7
+ class TestClass
8
+ def initialize(v, w)
9
+ @value1 = v
10
+ @value2 = w
11
+ end
12
+ attr_reader :value1, :value2
13
+ include ModalSupport::StateEquivalent
14
+ end
15
+
16
+ should "provide equality" do
17
+ assert_equal TestClass.new(11,22), TestClass.new(11,22)
18
+ assert_equal TestClass.new(11,nil), TestClass.new(11,nil)
19
+ assert_not_equal TestClass.new(11,22), TestClass.new(11,23)
20
+ assert_not_equal TestClass.new(11,22), 11
21
+ assert TestClass.new(11,22).eql?(TestClass.new(11,22))
22
+ assert TestClass.new(11,nil).eql?(TestClass.new(11,nil))
23
+ assert !TestClass.new(11,22).eql?(TestClass.new(11,23))
24
+ assert !TestClass.new(11,22).eql?(11)
25
+ end
26
+
27
+ should "provide hash consistent with equality" do
28
+ assert_equal TestClass.new(11,22).hash, TestClass.new(11,22).hash
29
+ assert_equal TestClass.new(11,nil).hash, TestClass.new(11,nil).hash
30
+ assert_not_equal TestClass.new(11,22).hash, TestClass.new(11,23).hash
31
+ end
32
+
33
+ end
34
+
35
+ end
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: modalsupport
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ hash: 19
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
5
11
  platform: ruby
6
12
  authors:
7
13
  - Javier Goizueta
@@ -9,39 +15,51 @@ autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2010-02-05 00:00:00 +01:00
18
+ date: 2010-06-09 00:00:00 +02:00
13
19
  default_executable:
14
20
  dependencies:
15
21
  - !ruby/object:Gem::Dependency
16
- name: thoughtbot-shoulda
17
- type: :development
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ name: shoulda
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
20
26
  requirements:
21
27
  - - ">="
22
28
  - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
23
32
  version: "0"
24
- version:
33
+ type: :development
34
+ version_requirements: *id001
25
35
  - !ruby/object:Gem::Dependency
26
36
  name: hobosupport
27
- type: :runtime
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
30
40
  requirements:
31
41
  - - ">="
32
42
  - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
33
46
  version: "0"
34
- version:
47
+ type: :runtime
48
+ version_requirements: *id002
35
49
  - !ruby/object:Gem::Dependency
36
50
  name: activesupport
37
- type: :runtime
38
- version_requirement:
39
- version_requirements: !ruby/object:Gem::Requirement
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
40
54
  requirements:
41
55
  - - ">="
42
56
  - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
43
60
  version: "0"
44
- version:
61
+ type: :runtime
62
+ version_requirements: *id003
45
63
  description: additional support extensions to ActiveSupport and HoboSupport
46
64
  email: jgoizueta@gmail.com
47
65
  executables: []
@@ -61,15 +79,20 @@ files:
61
79
  - lib/modalsupport.rb
62
80
  - lib/modalsupport/enumerable.rb
63
81
  - lib/modalsupport/file.rb
82
+ - lib/modalsupport/mixins/bracket_constructor.rb
83
+ - lib/modalsupport/mixins/state_equivalent.rb
64
84
  - lib/modalsupport/regexp.rb
65
85
  - lib/modalsupport/string.rb
66
86
  - modalsupport.gemspec
67
87
  - test/helper.rb
88
+ - test/test_bracket_constructor.rb
68
89
  - test/test_grep.rb
69
90
  - test/test_gsub.rb
70
91
  - test/test_match.rb
92
+ - test/test_product.rb
71
93
  - test/test_relative_path.rb
72
94
  - test/test_slice.rb
95
+ - test/test_state_equivalent.rb
73
96
  - test/test_unindent.rb
74
97
  has_rdoc: true
75
98
  homepage: http://github.com/jgoizueta/modalsupport
@@ -81,29 +104,38 @@ rdoc_options:
81
104
  require_paths:
82
105
  - lib
83
106
  required_ruby_version: !ruby/object:Gem::Requirement
107
+ none: false
84
108
  requirements:
85
109
  - - ">="
86
110
  - !ruby/object:Gem::Version
111
+ hash: 3
112
+ segments:
113
+ - 0
87
114
  version: "0"
88
- version:
89
115
  required_rubygems_version: !ruby/object:Gem::Requirement
116
+ none: false
90
117
  requirements:
91
118
  - - ">="
92
119
  - !ruby/object:Gem::Version
120
+ hash: 3
121
+ segments:
122
+ - 0
93
123
  version: "0"
94
- version:
95
124
  requirements: []
96
125
 
97
126
  rubyforge_project:
98
- rubygems_version: 1.3.5
127
+ rubygems_version: 1.3.7
99
128
  signing_key:
100
129
  specification_version: 3
101
130
  summary: simple extensions to core classes
102
131
  test_files:
103
132
  - test/helper.rb
133
+ - test/test_bracket_constructor.rb
104
134
  - test/test_grep.rb
105
135
  - test/test_gsub.rb
106
136
  - test/test_match.rb
137
+ - test/test_product.rb
107
138
  - test/test_relative_path.rb
108
139
  - test/test_slice.rb
140
+ - test/test_state_equivalent.rb
109
141
  - test/test_unindent.rb