modalsupport 0.8.2 → 0.8.3

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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.8.2
1
+ 0.8.3
@@ -1,4 +1,26 @@
1
- # Mixin that adds a [] operator constructor to a class
1
+ # Mixin that adds a [] operator constructor to a class, which
2
+ # is a not-so-ugly alternative to the +new+ method.
3
+ #
4
+ # class A
5
+ # include BracketConstructor
6
+ # def initialize(a)
7
+ # puts "A.new(#{a.inspect})"
8
+ # end
9
+ # end
10
+ #
11
+ # A[100] # => A.new(100)
12
+ #
13
+ # In Rails, this kind of constructor has an advantage over a function such as:
14
+ #
15
+ # def A(*args)
16
+ # A.new(*args)
17
+ # end
18
+ #
19
+ # With the A[] notation we've referenced the class name via the constant A,
20
+ # which in Rails brings in the declaration for A automatically, whereas the
21
+ # method A() won't be found unless the file which declares it is explicitely
22
+ # required.
23
+ #
2
24
  module ModalSupport::BracketConstructor
3
25
 
4
26
  def self.included(base)
@@ -59,5 +59,20 @@ class String
59
59
  lines.join("\n")
60
60
  end
61
61
 
62
+ # Remove wrapping characters and external whitespace
63
+ def unwrap(wrapping)
64
+ if wrapping.length==1
65
+ open = close = wrapping
66
+ else
67
+ open = wrapping[0,wrapping.length/2]
68
+ close = wrapping[-wrapping.length/2..-1]
69
+ end
70
+ if self.match(/\A\s*#{Regexp.escape(open)}(.*)#{Regexp.escape(close)}\s*\Z/m)
71
+ $1
72
+ else
73
+ self
74
+ end
75
+ end
76
+
62
77
  end
63
78
 
data/modalsupport.gemspec CHANGED
@@ -1,86 +1,65 @@
1
1
  # Generated by jeweler
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{modalsupport}
8
- s.version = "0.8.2"
8
+ s.version = "0.8.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Javier Goizueta"]
12
- s.date = %q{2011-02-21}
11
+ s.authors = [%q{Javier Goizueta}]
12
+ s.date = %q{2011-11-29}
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 = [
16
16
  "LICENSE",
17
- "README.rdoc"
17
+ "README.rdoc"
18
18
  ]
19
19
  s.files = [
20
20
  ".document",
21
- ".gitignore",
22
- "LICENSE",
23
- "README.rdoc",
24
- "Rakefile",
25
- "VERSION",
26
- "lib/modalsupport.rb",
27
- "lib/modalsupport/array.rb",
28
- "lib/modalsupport/enumerable.rb",
29
- "lib/modalsupport/file.rb",
30
- "lib/modalsupport/full.rb",
31
- "lib/modalsupport/mixins/bracket_constructor.rb",
32
- "lib/modalsupport/mixins/state_equivalent.rb",
33
- "lib/modalsupport/recursive_map.rb",
34
- "lib/modalsupport/regexp.rb",
35
- "lib/modalsupport/ruby_engine.rb",
36
- "lib/modalsupport/ruby_platform.rb",
37
- "lib/modalsupport/ruby_version.rb",
38
- "lib/modalsupport/string.rb",
39
- "modalsupport.gemspec",
40
- "test/helper.rb",
41
- "test/test_array_index.rb",
42
- "test/test_array_to_h.rb",
43
- "test/test_bracket_constructor.rb",
44
- "test/test_cross_each.rb",
45
- "test/test_grep.rb",
46
- "test/test_gsub.rb",
47
- "test/test_match.rb",
48
- "test/test_paralell_each.rb",
49
- "test/test_product.rb",
50
- "test/test_recursive_map.rb",
51
- "test/test_relative_path.rb",
52
- "test/test_rotate.rb",
53
- "test/test_slice.rb",
54
- "test/test_state_equivalent.rb",
55
- "test/test_unindent.rb"
21
+ "LICENSE",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "lib/modalsupport.rb",
26
+ "lib/modalsupport/array.rb",
27
+ "lib/modalsupport/enumerable.rb",
28
+ "lib/modalsupport/file.rb",
29
+ "lib/modalsupport/full.rb",
30
+ "lib/modalsupport/mixins/bracket_constructor.rb",
31
+ "lib/modalsupport/mixins/state_equivalent.rb",
32
+ "lib/modalsupport/recursive_map.rb",
33
+ "lib/modalsupport/regexp.rb",
34
+ "lib/modalsupport/ruby_engine.rb",
35
+ "lib/modalsupport/ruby_platform.rb",
36
+ "lib/modalsupport/ruby_version.rb",
37
+ "lib/modalsupport/string.rb",
38
+ "modalsupport.gemspec",
39
+ "test/helper.rb",
40
+ "test/test_array_index.rb",
41
+ "test/test_array_to_h.rb",
42
+ "test/test_bracket_constructor.rb",
43
+ "test/test_cross_each.rb",
44
+ "test/test_grep.rb",
45
+ "test/test_gsub.rb",
46
+ "test/test_match.rb",
47
+ "test/test_paralell_each.rb",
48
+ "test/test_product.rb",
49
+ "test/test_recursive_map.rb",
50
+ "test/test_relative_path.rb",
51
+ "test/test_rotate.rb",
52
+ "test/test_slice.rb",
53
+ "test/test_state_equivalent.rb",
54
+ "test/test_unindent.rb",
55
+ "test/test_unwrap.rb"
56
56
  ]
57
57
  s.homepage = %q{http://github.com/jgoizueta/modalsupport}
58
- s.rdoc_options = ["--charset=UTF-8"]
59
- s.require_paths = ["lib"]
60
- s.rubygems_version = %q{1.3.7}
58
+ s.require_paths = [%q{lib}]
59
+ s.rubygems_version = %q{1.8.6}
61
60
  s.summary = %q{simple extensions to core classes}
62
- s.test_files = [
63
- "test/helper.rb",
64
- "test/test_array_index.rb",
65
- "test/test_array_to_h.rb",
66
- "test/test_bracket_constructor.rb",
67
- "test/test_cross_each.rb",
68
- "test/test_grep.rb",
69
- "test/test_gsub.rb",
70
- "test/test_match.rb",
71
- "test/test_paralell_each.rb",
72
- "test/test_product.rb",
73
- "test/test_recursive_map.rb",
74
- "test/test_relative_path.rb",
75
- "test/test_rotate.rb",
76
- "test/test_slice.rb",
77
- "test/test_state_equivalent.rb",
78
- "test/test_unindent.rb",
79
- "test/tstr.rb"
80
- ]
81
61
 
82
62
  if s.respond_to? :specification_version then
83
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
84
63
  s.specification_version = 3
85
64
 
86
65
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
@@ -6,6 +6,7 @@ class TestArrayIndex < Test::Unit::TestCase
6
6
 
7
7
  should "have an to_h method converting key-value pairs to a hash" do
8
8
  assert_respond_to [], :to_h
9
+ assert_equal({}, [].to_h)
9
10
  assert_equal({:a=>11, :b=>22}, [[:a,11], [:b,22]].to_h)
10
11
  assert_equal({:a=>11, :b=>[22,33]}, [[:a,11], [:b,22,33]].to_h)
11
12
  assert_equal({1=>nil, 2=>nil, 3=>nil}, [1,2,3].to_h)
@@ -64,7 +64,7 @@ class TestRecursiveMap < Test::Unit::TestCase
64
64
  ModalSupport.recursive_map(:a=>11, :b=>22, :d=>[1,2,3], &@array_to_string)
65
65
  )
66
66
  assert_same_elements(
67
- [[:a, 11], [:b, 22], [:c, [[:x, 100], [:y, 200]]], [:d, [1, 2, 3]]],
67
+ [[:a, 11], [:b, 22], [:c, {:x=>100,:y=>200}.to_a], [:d, [1, 2, 3]]],
68
68
  ModalSupport.recursive_map(:a=>11, :b=>22, :c=>{:x=>100,:y=>200}, :d=>[1,2,3], &@hash_to_array)
69
69
  )
70
70
  assert_equal(
@@ -0,0 +1,50 @@
1
+ require 'helper'
2
+
3
+ class TestUnwrap < Test::Unit::TestCase
4
+
5
+ should "unwrap brackets" do
6
+ assert_equal "1234", "[1234]".unwrap("[]")
7
+ assert_equal "[1234]", "[[1234]]".unwrap("[]")
8
+ assert_equal "1234", "{1234}".unwrap("{}")
9
+ assert_equal "{1234}", "{{1234}}".unwrap("{}")
10
+ assert_equal "1234", "(1234)".unwrap("()")
11
+ assert_equal "(1234)", "((1234))".unwrap("()")
12
+ end
13
+
14
+ should "unwrap quotes" do
15
+ assert_equal "1234", '"1234"'.unwrap('""')
16
+ assert_equal "1234", '"1234"'.unwrap('"')
17
+ assert_equal "1234", "'1234'".unwrap("''")
18
+ assert_equal "1234", "'1234'".unwrap("'")
19
+ assert_equal '"1234"', '""1234""'.unwrap('""')
20
+ assert_equal '"1234"', '""1234""'.unwrap('"')
21
+ assert_equal "'1234'", "''1234''".unwrap("''")
22
+ assert_equal "'1234'", "''1234''".unwrap("'")
23
+ end
24
+
25
+ should "unwrap and remove external whitespace" do
26
+ assert_equal "1234", " [1234]".unwrap("[]")
27
+ assert_equal "1234", " [1234] ".unwrap("[]")
28
+ assert_equal "[1234]", " [[1234]] ".unwrap("[]")
29
+ end
30
+
31
+ should "preserve internal whitespace" do
32
+ assert_equal "12 34", "[12 34]".unwrap("[]")
33
+ assert_equal "12 3\n4", "[12 3\n4]".unwrap("[]")
34
+ assert_equal " 12 3\n4", "[ 12 3\n4]".unwrap("[]")
35
+ assert_equal " 12 3\n4 ", "[ 12 3\n4 ]".unwrap("[]")
36
+ assert_equal " 12 3\n4 ", " [ 12 3\n4 ] ".unwrap("[]")
37
+ assert_equal " 12 3\n4 ", " [ 12 3\n4 ] ".unwrap("[]")
38
+ end
39
+
40
+ should "preserve non-wrapped strings" do
41
+ assert_equal "1234", "1234".unwrap("[]")
42
+ assert_equal "1234", "1234".unwrap('""')
43
+ assert_equal "1234", "1234".unwrap('"')
44
+ assert_equal " 1234 ", " 1234 ".unwrap("[]")
45
+ assert_equal " 1234 ", " 1234 ".unwrap('""')
46
+ assert_equal " 1234 ", " 1234 ".unwrap('"')
47
+ end
48
+
49
+
50
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: modalsupport
3
3
  version: !ruby/object:Gem::Version
4
- hash: 59
5
- prerelease: false
4
+ hash: 57
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 8
9
- - 2
10
- version: 0.8.2
9
+ - 3
10
+ version: 0.8.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Javier Goizueta
@@ -15,8 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-21 00:00:00 +01:00
19
- default_executable:
18
+ date: 2011-11-29 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
21
  name: shoulda
@@ -43,7 +42,6 @@ extra_rdoc_files:
43
42
  - README.rdoc
44
43
  files:
45
44
  - .document
46
- - .gitignore
47
45
  - LICENSE
48
46
  - README.rdoc
49
47
  - Rakefile
@@ -78,14 +76,13 @@ files:
78
76
  - test/test_slice.rb
79
77
  - test/test_state_equivalent.rb
80
78
  - test/test_unindent.rb
81
- - test/tstr.rb
82
- has_rdoc: true
79
+ - test/test_unwrap.rb
83
80
  homepage: http://github.com/jgoizueta/modalsupport
84
81
  licenses: []
85
82
 
86
83
  post_install_message:
87
- rdoc_options:
88
- - --charset=UTF-8
84
+ rdoc_options: []
85
+
89
86
  require_paths:
90
87
  - lib
91
88
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -109,25 +106,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
106
  requirements: []
110
107
 
111
108
  rubyforge_project:
112
- rubygems_version: 1.3.7
109
+ rubygems_version: 1.8.6
113
110
  signing_key:
114
111
  specification_version: 3
115
112
  summary: simple extensions to core classes
116
- test_files:
117
- - test/helper.rb
118
- - test/test_array_index.rb
119
- - test/test_array_to_h.rb
120
- - test/test_bracket_constructor.rb
121
- - test/test_cross_each.rb
122
- - test/test_grep.rb
123
- - test/test_gsub.rb
124
- - test/test_match.rb
125
- - test/test_paralell_each.rb
126
- - test/test_product.rb
127
- - test/test_recursive_map.rb
128
- - test/test_relative_path.rb
129
- - test/test_rotate.rb
130
- - test/test_slice.rb
131
- - test/test_state_equivalent.rb
132
- - test/test_unindent.rb
133
- - test/tstr.rb
113
+ test_files: []
114
+
data/.gitignore DELETED
@@ -1,24 +0,0 @@
1
- ## MAC OS
2
- .DS_Store
3
-
4
- ## TEXTMATE
5
- *.tmproj
6
- tmtags
7
-
8
- ## EMACS
9
- *~
10
- \#*
11
- .\#*
12
-
13
- ## VIM
14
- *.swp
15
-
16
- ## PROJECT::GENERAL
17
- coverage
18
- rdoc
19
- pkg
20
-
21
- ## Rubinius
22
- *.rbc
23
-
24
- ## PROJECT::SPECIFIC
data/test/tstr.rb DELETED
@@ -1,109 +0,0 @@
1
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '.', 'lib'))
2
-
3
- require 'benchmark'
4
-
5
- class Array
6
-
7
- # rot1: splicing
8
- def rot1(n=1)
9
- self[n..-1] + self[0...n]
10
- end
11
- def rot1!(n=1)
12
- self.replace(self.rot1(n))
13
- end
14
-
15
- # rot2: <</shift, no válido para n<0
16
- def rot2(n=1)
17
- a = dup
18
- n.times do a << a.shift end
19
- a
20
- end
21
- def rot2!(n=1)
22
- n.times do self << self.shift end
23
- self
24
- end
25
-
26
- # rot3: in-place juggling
27
- def rot3!(n=1)
28
- m = self.size
29
- i0 = 0
30
- v0 = self[i=i0]
31
- loop do
32
- j = (i+n)%m # next index
33
- if j==i0
34
- self[i] = v0
35
- break
36
- else
37
- self[i] = self[j]
38
- end
39
- i = j
40
- end
41
- self
42
- end
43
- def rot3(n=1)
44
- self.dup.rot3!(n)
45
- end
46
-
47
- end
48
-
49
- if false
50
- puts [1,2,3,4,5,6,7].rot3(0).inspect
51
- puts [1,2,3,4,5,6,7].rot3(1).inspect
52
- puts [1,2,3,4,5,6,7].rot3(3).inspect
53
- puts [1,2,3,4,5,6,7].rot3(5).inspect
54
- puts [1,2,3,4,5,6,7].rot3(6).inspect
55
-
56
- a=(1..7).to_a
57
- a.rot3!(3)
58
- puts a.inspect
59
-
60
- exit
61
- end
62
-
63
- N=100
64
- M=1000
65
- M2=15
66
-
67
- a1 = Array.new(M){rand}
68
- a2 = Array.new(M){Array.new(rand(M2)){rand}}
69
- b1 = b2 = nil
70
-
71
- Benchmark.bm(15) do |bm|
72
- b1 = a1.dup
73
- b2 = a2.dup
74
- bm.report("rot1") {
75
- N.times{ (1...b1.size).each{|n| b1.rot1(n)} }
76
- N.times{ (1...b2.size).each{|n| b2.rot1(n)} }
77
- }
78
- b1 = a1.dup
79
- b2 = a2.dup
80
- bm.report("rot1!") {
81
- N.times{ (1...b1.size).each{|n| b1.rot1!(n)} }
82
- N.times{ (1...b2.size).each{|n| b2.rot1!(n)} }
83
- }
84
- b1 = a1.dup
85
- b2 = a2.dup
86
- bm.report("rot2") {
87
- N.times{ (1...b1.size).each{|n| b1.rot2(n)} }
88
- N.times{ (1...b2.size).each{|n| b2.rot2(n)} }
89
- }
90
- b1 = a1.dup
91
- b2 = a2.dup
92
- bm.report("rot2!") {
93
- N.times{ (1...b1.size).each{|n| b1.rot2!(n)} }
94
- N.times{ (1...b2.size).each{|n| b2.rot2!(n)} }
95
- }
96
- b1 = a1.dup
97
- b2 = a2.dup
98
- bm.report("rot3") {
99
- N.times{ (1...b1.size).each{|n| b1.rot3(n)} }
100
- N.times{ (1...b2.size).each{|n| b2.rot3(n)} }
101
- }
102
- b1 = a1.dup
103
- b2 = a2.dup
104
- bm.report("rot3!") {
105
- N.times{ (1...b1.size).each{|n| b1.rot3!(n)} }
106
- N.times{ (1...b2.size).each{|n| b2.rot3!(n)} }
107
- }
108
-
109
- end