modalsupport 0.8.3 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.8.3
1
+ 0.9.0
@@ -0,0 +1,24 @@
1
+ module ModalSupport
2
+ if defined? ActiveSupport::BasicObject
3
+ BasicObject = ActiveSupport::BasicObject
4
+ elsif defined? ::BasicObject
5
+ class BasicObject < ::BasicObject
6
+ undef_method :==
7
+ undef_method :equal?
8
+ def raise(*args)
9
+ ::Object.send(:raise, *args)
10
+ end
11
+ end
12
+ else
13
+ class BasicObject #:nodoc:
14
+ begin
15
+ old_verbose, $VERBOSE = $VERBOSE, nil # just like Rails silence_warnings: suppress "warning: undefining `object_id' may cause serious problem"
16
+ instance_methods.each do |m|
17
+ undef_method(m) if m.to_s !~ /(?:^__|^nil\?$|^send$|^object_id$)/
18
+ end
19
+ ensure
20
+ $VERBOSE = old_verbose
21
+ end
22
+ end
23
+ end
24
+ end
@@ -44,27 +44,27 @@ module Enumerable
44
44
 
45
45
  # Paralell iteration through multiple enumerable objects
46
46
  # (1..3).paralell_each([:a,:b,:c],(10..12)).to_a
47
- # => [[1, :a, 10], [2, :b, 11], [3, :c, 12]]
48
- #
47
+ # => [[1, :a, 10], [2, :b, 11], [3, :c, 12]]
48
+ #
49
49
  # e.paralell_each(*enums) = e.zip(*enums).each
50
50
  def paralell_each(*enums, &blk)
51
51
  # Note that to implement a lazy iterator we'd need
52
52
  # external iterators which don't work in Rubinius, are
53
53
  # implemented differently in Ruby < 1.8.7 and are slow.
54
54
  # So, for the time being we generate an array and then
55
- # iterate it.
55
+ # iterate it.
56
56
  if block_given?
57
57
  zip(*enums).each(&blk)
58
58
  else
59
59
  enum_for(:paralell_each, *enums)
60
60
  end
61
61
  end
62
-
62
+
63
63
  # Equivalent to paralell_each(*enums).to_a, but more efficient
64
64
  def paralell_array(*enums)
65
65
  zip(*enums)
66
66
  end
67
-
67
+
68
68
  # Cross-product iteration through multiple enumerable objects
69
69
  # (1..4).cross_each([:a,:b,:c],(11..12)).to_a
70
70
  # => [[1, :a, 11], [1, :a, 12], [1, :b, 11], [1, :b, 12], [1, :c, 11], [1, :c, 12],
@@ -89,9 +89,9 @@ module Enumerable
89
89
  enum_for(:cross_each, *enumerables)
90
90
  end
91
91
  end
92
-
92
+
93
93
  # equivalent to cross_each(*enumerables).to_a, but more efficient
94
- def cross_array(*enumerables)
94
+ def cross_array(*enumerables)
95
95
  # return to_a.product(*enumerables.map{|e| e.to_a})
96
96
  enumerables.unshift self
97
97
  result = [[]]
@@ -106,6 +106,34 @@ module Enumerable
106
106
  end
107
107
  result
108
108
  end
109
-
109
+
110
+ unless defined? ::HoboSupport
111
+
112
+ def map_with_index(res=[])
113
+ each_with_index {|x, i| res << yield(x, i)}
114
+ res
115
+ end
116
+
117
+ def build_hash(res={})
118
+ each do |x|
119
+ pair = block_given? ? yield(x) : x
120
+ res[pair.first] = pair.last if pair
121
+ end
122
+ res
123
+ end
124
+
125
+ def map_hash(res={})
126
+ each do |x|
127
+ v = yield x
128
+ res[x] = v
129
+ end
130
+ res
131
+ end
132
+
133
+ def rest
134
+ self[1..-1] || []
135
+ end
136
+
137
+ end # Hobosupport
110
138
 
111
139
  end
@@ -1,4 +1,3 @@
1
- require 'modalsupport'
2
1
  begin
3
2
  require 'active_support'
4
3
  rescue LoadError
@@ -9,3 +8,4 @@ begin
9
8
  rescue LoadError
10
9
  puts "HoboSupport not available. Install it with: gem install hobosupport"
11
10
  end
11
+ require 'modalsupport'
@@ -0,0 +1,14 @@
1
+ unless defined? ::HoboSupport
2
+
3
+ class Hash
4
+
5
+ def map_hash(&b)
6
+ res = {}
7
+ each {|k,v| res[k] = b.arity == 1 ? yield(v) : yield(k, v) }
8
+ res
9
+ end
10
+
11
+ end
12
+
13
+
14
+ end
@@ -0,0 +1,31 @@
1
+ # HoboSupport-style ._?
2
+ # ._? calls a method if the receiver is not nil, returns nil
3
+ # otherwise.
4
+ require 'singleton'
5
+
6
+ unless nil.respond_to?(:_?)
7
+
8
+ class Object
9
+ def _?()
10
+ self
11
+ end
12
+ end
13
+
14
+ class NilClass
15
+ def _?()
16
+ ModalSupport::SafeNil.instance
17
+ end
18
+ end
19
+
20
+ module ModalSupport
21
+
22
+ class SafeNil < ModalSupport::BasicObject
23
+ include ::Singleton
24
+ def method_missing(method, *args, &b)
25
+ return nil
26
+ end
27
+ end
28
+
29
+ end
30
+
31
+ end
data/lib/modalsupport.rb CHANGED
@@ -6,6 +6,8 @@ require 'modalsupport/regexp'
6
6
  require 'modalsupport/enumerable'
7
7
  require 'modalsupport/file'
8
8
  require 'modalsupport/array'
9
+ require 'modalsupport/hash'
10
+ require 'modalsupport/basic_object'
9
11
 
10
12
  module ModalSupport
11
13
  end
@@ -13,3 +15,4 @@ end
13
15
  require 'modalsupport/recursive_map'
14
16
  require 'modalsupport/mixins/state_equivalent'
15
17
  require 'modalsupport/mixins/bracket_constructor'
18
+ require 'modalsupport/methodcall'
data/modalsupport.gemspec CHANGED
@@ -4,14 +4,14 @@
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = %q{modalsupport}
8
- s.version = "0.8.3"
7
+ s.name = "modalsupport"
8
+ s.version = "0.9.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = [%q{Javier Goizueta}]
12
- s.date = %q{2011-11-29}
13
- s.description = %q{additional support extensions to ActiveSupport and HoboSupport}
14
- s.email = %q{jgoizueta@gmail.com}
11
+ s.authors = ["Javier Goizueta"]
12
+ s.date = "2012-04-30"
13
+ s.description = "additional support extensions to ActiveSupport and HoboSupport"
14
+ s.email = "jgoizueta@gmail.com"
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE",
17
17
  "README.rdoc"
@@ -24,9 +24,12 @@ Gem::Specification.new do |s|
24
24
  "VERSION",
25
25
  "lib/modalsupport.rb",
26
26
  "lib/modalsupport/array.rb",
27
+ "lib/modalsupport/basic_object.rb",
27
28
  "lib/modalsupport/enumerable.rb",
28
29
  "lib/modalsupport/file.rb",
29
30
  "lib/modalsupport/full.rb",
31
+ "lib/modalsupport/hash.rb",
32
+ "lib/modalsupport/methodcall.rb",
30
33
  "lib/modalsupport/mixins/bracket_constructor.rb",
31
34
  "lib/modalsupport/mixins/state_equivalent.rb",
32
35
  "lib/modalsupport/recursive_map.rb",
@@ -40,24 +43,29 @@ Gem::Specification.new do |s|
40
43
  "test/test_array_index.rb",
41
44
  "test/test_array_to_h.rb",
42
45
  "test/test_bracket_constructor.rb",
46
+ "test/test_build_hash.rb",
43
47
  "test/test_cross_each.rb",
44
48
  "test/test_grep.rb",
45
49
  "test/test_gsub.rb",
50
+ "test/test_map_hash.rb",
51
+ "test/test_map_with_index.rb",
46
52
  "test/test_match.rb",
53
+ "test/test_methodcall.rb",
47
54
  "test/test_paralell_each.rb",
48
55
  "test/test_product.rb",
49
56
  "test/test_recursive_map.rb",
50
57
  "test/test_relative_path.rb",
58
+ "test/test_rest.rb",
51
59
  "test/test_rotate.rb",
52
60
  "test/test_slice.rb",
53
61
  "test/test_state_equivalent.rb",
54
62
  "test/test_unindent.rb",
55
63
  "test/test_unwrap.rb"
56
64
  ]
57
- s.homepage = %q{http://github.com/jgoizueta/modalsupport}
58
- s.require_paths = [%q{lib}]
59
- s.rubygems_version = %q{1.8.6}
60
- s.summary = %q{simple extensions to core classes}
65
+ s.homepage = "http://github.com/jgoizueta/modalsupport"
66
+ s.require_paths = ["lib"]
67
+ s.rubygems_version = "1.8.23"
68
+ s.summary = "simple extensions to core classes"
61
69
 
62
70
  if s.respond_to? :specification_version then
63
71
  s.specification_version = 3
@@ -0,0 +1,14 @@
1
+ require 'helper'
2
+
3
+ class TestBuildHash < Test::Unit::TestCase
4
+
5
+ context 'Enumerable#build_hash' do
6
+
7
+ should "returns a hash where each element is given by the block unless nil" do
8
+ assert_equal({'some'=>4, 'words'=>5}, %w(some short words).build_hash { |s| [s, s.length] unless s == "short" })
9
+ assert_equal({10=>3, 20=>6, 30=>9}, (1..3).build_hash{|v| [v*10,v*3]})
10
+ end
11
+
12
+ end
13
+
14
+ end
@@ -0,0 +1,29 @@
1
+ require 'helper'
2
+
3
+ class TestMapHash< Test::Unit::TestCase
4
+
5
+ context 'Enumerable#map_hash' do
6
+
7
+ should "returns a hash where keys are the enumerated elements and the values are given by the block" do
8
+ assert_equal({'some'=>4, 'words'=>5, 'short'=>nil}, %w(some short words).map_hash { |s| s.length unless s == "short" })
9
+ assert_equal({1=>3, 2=>6, 3=>9}, (1..3).map_hash{|v| v*3})
10
+ end
11
+
12
+ end
13
+
14
+ context 'Hash#map_hash' do
15
+
16
+ should "applies a function with the key and value as arguments to each value of the hash" do
17
+ assert_equal({1=>true, 3=>true, 6=>false}, {1=>2, 3=>4, 6=>5}.map_hash { |key, value| key < value })
18
+ assert_equal({1=>3, 2=>6, 3=>9}, (1..3).map_hash{|v| v*3})
19
+ end
20
+
21
+ should "applies a function with the value as arguments to each value of the hash" do
22
+ assert_equal({1=>true, 3=>true, 6=>false}, {1=>2, 3=>4, 6=>5}.map_hash { |key, value| key < value })
23
+ assert_equal({1=>200, 3=>400, 6=>500 }, {1=>2, 3=>4, 6=>5}.map_hash { |value| value * 100 })
24
+ end
25
+
26
+ end
27
+
28
+
29
+ end
@@ -0,0 +1,13 @@
1
+ require 'helper'
2
+
3
+ class TestMapWithIndex< Test::Unit::TestCase
4
+
5
+ context 'Enumerable#map_with_index' do
6
+
7
+ should "maps elements passing also de index to the block" do
8
+ assert_equal ["", "short", "wordswords"], %w(some short words).map_with_index { |s, i| s * i }
9
+ end
10
+
11
+ end
12
+
13
+ end
@@ -0,0 +1,34 @@
1
+ require 'helper'
2
+
3
+ class TestMethodcall < Test::Unit::TestCase
4
+
5
+ context "Given a non-nil object" do
6
+
7
+ should "Return itself from ._?" do
8
+ assert_same 3, 3._?
9
+ o = "xxx"
10
+ assert_same o, o._?
11
+ o = Object.new
12
+ assert_same o, o._?
13
+ o = false
14
+ assert_same o, o._?
15
+ o = 0
16
+ assert_same o, o._?
17
+ end
18
+
19
+ end
20
+
21
+ context "Given a nil object" do
22
+
23
+ should "Return something that returns nil to any message" do
24
+
25
+ assert_nil nil._?.to_s
26
+ assert_nil nil._?.inspect
27
+ assert_nil nil._?.xxxx
28
+
29
+ end
30
+
31
+ end
32
+
33
+
34
+ end
@@ -3,7 +3,7 @@ require 'helper'
3
3
  class TestRecursiveMap < Test::Unit::TestCase
4
4
 
5
5
  context "recursive_map" do
6
-
6
+
7
7
  setup do
8
8
  require 'yaml'
9
9
  @value_doubler = lambda{|v| v.kind_of?(Numeric) ? v*2 : v}
@@ -20,31 +20,31 @@ class TestRecursiveMap < Test::Unit::TestCase
20
20
  end
21
21
  }
22
22
  end
23
-
23
+
24
24
  should "process values in containers" do
25
- assert_equal(
25
+ assert_equal(
26
26
  {:a=>22, :b=>44},
27
27
  ModalSupport.recursive_map(:a=>11, :b=>22, &@value_doubler)
28
28
  )
29
- assert_equal(
29
+ assert_equal(
30
30
  [22,44],
31
31
  ModalSupport.recursive_map([11,22], &@value_doubler)
32
32
  )
33
33
  end
34
34
 
35
35
  should "process values in nested containers" do
36
- assert_equal(
36
+ assert_equal(
37
37
  {:a=>22, :b=>44, :c=>{:x=>200,:y=>400}, :d=>[2,4,6]},
38
38
  ModalSupport.recursive_map(:a=>11, :b=>22, :c=>{:x=>100,:y=>200}, :d=>[1,2,3], &@value_doubler)
39
39
  )
40
- assert_equal(
40
+ assert_equal(
41
41
  [22, 44, {:c=>{:x=>200,:y=>400}, :d=>[2,4,6]}],
42
42
  ModalSupport.recursive_map([11, 22, {:c=>{:x=>100,:y=>200}, :d=>[1,2,3]}], &@value_doubler)
43
43
  )
44
44
  end
45
45
 
46
46
  should "process values in deeply nested containers" do
47
- assert_equal(
47
+ assert_equal(
48
48
  {:a=>22, :b=>44, :c=>{:x=>200, :y=>400}, :d=>[2, 4, 6, {:zz=>[2000, 4000, {:yy=>{:xx=>6000}}]}]},
49
49
  ModalSupport.recursive_map({:a=>11, :b=>22, :c=>{:x=>100,:y=>200}, :d=>[1,2,3,{:zz=>[1000,2000,{:yy=>{:xx=>3000}}]}]}, &@value_doubler)
50
50
  )
@@ -53,19 +53,15 @@ class TestRecursiveMap < Test::Unit::TestCase
53
53
  ModalSupport.recursive_map([11,22,{:a=>11, :b=>22, :c=>{:x=>100,:y=>200}, :d=>[1,2,3,{:zz=>[1000,2000,{:yy=>{:xx=>3000}}]}]}], &@value_doubler)
54
54
  )
55
55
  end
56
-
56
+
57
57
  should "transform containers" do
58
58
  assert_same_elements(
59
59
  [[:a, 11], [:b, 22], [:d, [1, 2, 3]]],
60
- ModalSupport.recursive_map(:a=>11, :b=>22, :d=>[1,2,3], &@hash_to_array)
60
+ ModalSupport.recursive_map(:a=>11, :b=>22, :d=>[1,2,3], &@hash_to_array)
61
61
  )
62
62
  assert_equal(
63
63
  {:a=>11, :b=>22, :d=>[1,2,3].inspect},
64
- ModalSupport.recursive_map(:a=>11, :b=>22, :d=>[1,2,3], &@array_to_string)
65
- )
66
- assert_same_elements(
67
- [[:a, 11], [:b, 22], [:c, {:x=>100,:y=>200}.to_a], [:d, [1, 2, 3]]],
68
- ModalSupport.recursive_map(:a=>11, :b=>22, :c=>{:x=>100,:y=>200}, :d=>[1,2,3], &@hash_to_array)
64
+ ModalSupport.recursive_map(:a=>11, :b=>22, :d=>[1,2,3], &@array_to_string)
69
65
  )
70
66
  assert_equal(
71
67
  {:a=>11, :b=>22, :c=>{:x=>100, :y=>200}, :d=>[1,2,3].inspect},
@@ -73,13 +69,17 @@ class TestRecursiveMap < Test::Unit::TestCase
73
69
  )
74
70
  ruby_19 do
75
71
  # we only test this in Ruby 1.9 because prior versions don't keep Hash ordering
72
+ assert_same_elements(
73
+ [[:a, 11], [:b, 22], [:c, {:x=>100,:y=>200}.to_a], [:d, [1, 2, 3]]],
74
+ ModalSupport.recursive_map(:a=>11, :b=>22, :c=>{:x=>100,:y=>200}, :d=>[1,2,3], &@hash_to_array)
75
+ )
76
76
  assert_equal(
77
77
  "[111, 222, {:a=>11, :b=>22, :c=>{:x=>100, :y=>200}, :d=>\"[1, 2, 3]\"}]",
78
78
  ModalSupport.recursive_map([111,222,{:a=>11, :b=>22, :c=>{:x=>100,:y=>200}, :d=>[1,2,3]}], &@array_to_string)
79
- )
79
+ )
80
80
  end
81
81
  end
82
-
82
+
83
83
  should "recursively process elements transformed to a container" do
84
84
  assert_equal(
85
85
  [222, 444],
data/test/test_rest.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'helper'
2
+
3
+ class TestRest < Test::Unit::TestCase
4
+
5
+ context 'Enumerable#rest' do
6
+
7
+ should "returns the rest of elements having removed the first one" do
8
+ assert_equal [2,3,4,5], [1,2,3,4,5].rest
9
+ assert_equal [2], [1,2].rest
10
+ assert_equal [], [1].rest
11
+ assert_equal [], [].rest
12
+ end
13
+
14
+ end
15
+
16
+ end
metadata CHANGED
@@ -1,46 +1,40 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: modalsupport
3
- version: !ruby/object:Gem::Version
4
- hash: 57
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 8
9
- - 3
10
- version: 0.8.3
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Javier Goizueta
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-11-29 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2012-04-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: shoulda
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 3
29
- segments:
30
- - 0
31
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
32
22
  type: :development
33
- version_requirements: *id001
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
34
30
  description: additional support extensions to ActiveSupport and HoboSupport
35
31
  email: jgoizueta@gmail.com
36
32
  executables: []
37
-
38
33
  extensions: []
39
-
40
- extra_rdoc_files:
34
+ extra_rdoc_files:
41
35
  - LICENSE
42
36
  - README.rdoc
43
- files:
37
+ files:
44
38
  - .document
45
39
  - LICENSE
46
40
  - README.rdoc
@@ -48,9 +42,12 @@ files:
48
42
  - VERSION
49
43
  - lib/modalsupport.rb
50
44
  - lib/modalsupport/array.rb
45
+ - lib/modalsupport/basic_object.rb
51
46
  - lib/modalsupport/enumerable.rb
52
47
  - lib/modalsupport/file.rb
53
48
  - lib/modalsupport/full.rb
49
+ - lib/modalsupport/hash.rb
50
+ - lib/modalsupport/methodcall.rb
54
51
  - lib/modalsupport/mixins/bracket_constructor.rb
55
52
  - lib/modalsupport/mixins/state_equivalent.rb
56
53
  - lib/modalsupport/recursive_map.rb
@@ -64,14 +61,19 @@ files:
64
61
  - test/test_array_index.rb
65
62
  - test/test_array_to_h.rb
66
63
  - test/test_bracket_constructor.rb
64
+ - test/test_build_hash.rb
67
65
  - test/test_cross_each.rb
68
66
  - test/test_grep.rb
69
67
  - test/test_gsub.rb
68
+ - test/test_map_hash.rb
69
+ - test/test_map_with_index.rb
70
70
  - test/test_match.rb
71
+ - test/test_methodcall.rb
71
72
  - test/test_paralell_each.rb
72
73
  - test/test_product.rb
73
74
  - test/test_recursive_map.rb
74
75
  - test/test_relative_path.rb
76
+ - test/test_rest.rb
75
77
  - test/test_rotate.rb
76
78
  - test/test_slice.rb
77
79
  - test/test_state_equivalent.rb
@@ -79,36 +81,26 @@ files:
79
81
  - test/test_unwrap.rb
80
82
  homepage: http://github.com/jgoizueta/modalsupport
81
83
  licenses: []
82
-
83
84
  post_install_message:
84
85
  rdoc_options: []
85
-
86
- require_paths:
86
+ require_paths:
87
87
  - lib
88
- required_ruby_version: !ruby/object:Gem::Requirement
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
89
  none: false
90
- requirements:
91
- - - ">="
92
- - !ruby/object:Gem::Version
93
- hash: 3
94
- segments:
95
- - 0
96
- version: "0"
97
- required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
95
  none: false
99
- requirements:
100
- - - ">="
101
- - !ruby/object:Gem::Version
102
- hash: 3
103
- segments:
104
- - 0
105
- version: "0"
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
106
100
  requirements: []
107
-
108
101
  rubyforge_project:
109
- rubygems_version: 1.8.6
102
+ rubygems_version: 1.8.23
110
103
  signing_key:
111
104
  specification_version: 3
112
105
  summary: simple extensions to core classes
113
106
  test_files: []
114
-