methodize 0.1 → 0.2.0
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/README.md +4 -0
- data/lib/methodize.rb +6 -2
- data/test/hash_test.rb +13 -13
- data/test/methodize_test.rb +24 -24
- metadata +17 -5
data/README.md
CHANGED
data/lib/methodize.rb
CHANGED
@@ -1,9 +1,13 @@
|
|
1
1
|
module Methodize
|
2
2
|
def self.extended(base)
|
3
|
+
# ruby >1.9 returns an array of symbols for object.public_methods
|
4
|
+
# while <1.9 returns an array of string. This methods guess it right
|
5
|
+
@@key_coerce = RUBY_VERSION.start_with?("1.9") ? lambda { |k| k.to_sym } : lambda { |k| k.to_s }
|
6
|
+
|
3
7
|
# if some of the Hash keys and public methods names conflict
|
4
8
|
# we free the existant method to enable the user to call it
|
5
9
|
base.keys.each do |k|
|
6
|
-
base.__free_method__(k.to_sym) if base.public_methods.include?(k
|
10
|
+
base.__free_method__(k.to_sym) if base.public_methods.include?(@@key_coerce.call(k))
|
7
11
|
end
|
8
12
|
end
|
9
13
|
|
@@ -12,7 +16,7 @@ module Methodize
|
|
12
16
|
end
|
13
17
|
|
14
18
|
def []=(key, value)
|
15
|
-
__free_method__(key) if !self.keys.include?(key) && self.public_methods.include?(key
|
19
|
+
__free_method__(key) if !self.keys.include?(key) && self.public_methods.include?(@@key_coerce.call(key))
|
16
20
|
super(key,value)
|
17
21
|
end
|
18
22
|
|
data/test/hash_test.rb
CHANGED
@@ -4,7 +4,7 @@ require 'methodize/hash'
|
|
4
4
|
require 'rubygems'
|
5
5
|
require 'ruby-debug'
|
6
6
|
|
7
|
-
class
|
7
|
+
class MethodizeHashTest < Test::Unit::TestCase
|
8
8
|
|
9
9
|
def setup
|
10
10
|
@hash = {
|
@@ -36,29 +36,29 @@ class MethodizeTest < Test::Unit::TestCase
|
|
36
36
|
end
|
37
37
|
|
38
38
|
def test_methodize_should_still_work_as_expected
|
39
|
-
assert_equal @hash[:article].last[:title]
|
40
|
-
assert_equal @hash[:article][1]["author"]
|
41
|
-
assert_equal @hash["type"]
|
42
|
-
assert_equal @hash[:size]
|
39
|
+
assert_equal "Article 3", @hash[:article].last[:title]
|
40
|
+
assert_equal "Foo Bar" , @hash[:article][1]["author"]
|
41
|
+
assert_equal :text , @hash["type"]
|
42
|
+
assert_equal 3 , @hash[:size]
|
43
43
|
assert_nil @hash[:wrong_key]
|
44
44
|
|
45
45
|
assert @hash.keys.include?(:size)
|
46
|
-
assert_equal @hash.article.size
|
46
|
+
assert_equal 3, @hash.article.size
|
47
47
|
end
|
48
48
|
|
49
49
|
def test_methodize_should_support_read_of_values_as_methods
|
50
|
-
assert_equal @hash.article.last.title
|
51
|
-
assert_equal @hash.article[1].author
|
52
|
-
assert_equal @hash.article.last.info.category.first
|
50
|
+
assert_equal "Article 3", @hash.article.last.title
|
51
|
+
assert_equal "Foo Bar" , @hash.article[1].author
|
52
|
+
assert_equal :sports , @hash.article.last.info.category.first
|
53
53
|
assert_nil @hash.wrong_key
|
54
54
|
|
55
|
-
assert_equal @hash.size
|
56
|
-
assert_equal @hash.type
|
57
|
-
assert_equal @hash.id
|
55
|
+
assert_equal 3 , @hash.size
|
56
|
+
assert_equal :text , @hash.type
|
57
|
+
assert_equal 123456789, @hash.id
|
58
58
|
end
|
59
59
|
|
60
60
|
def test_double_methodize_call_does_not_affect_anything
|
61
|
-
assert_equal @hash.methodize!.article.last.title
|
61
|
+
assert_equal "Article 3", @hash.methodize!.article.last.title
|
62
62
|
end
|
63
63
|
|
64
64
|
end
|
data/test/methodize_test.rb
CHANGED
@@ -27,7 +27,7 @@ class MethodizeTest < Test::Unit::TestCase
|
|
27
27
|
}
|
28
28
|
}
|
29
29
|
],
|
30
|
-
"
|
30
|
+
"class" => :text,
|
31
31
|
:size => 3,
|
32
32
|
:id => 123456789
|
33
33
|
}
|
@@ -36,39 +36,39 @@ class MethodizeTest < Test::Unit::TestCase
|
|
36
36
|
end
|
37
37
|
|
38
38
|
def test_hash_should_still_work_as_expected
|
39
|
-
assert_equal @hash[:article].last[:title]
|
40
|
-
assert_equal @hash[:article][1]["author"]
|
41
|
-
assert_equal @hash["
|
42
|
-
assert_equal @hash[:size]
|
39
|
+
assert_equal "Article 3", @hash[:article].last[:title]
|
40
|
+
assert_equal "Foo Bar" , @hash[:article][1]["author"]
|
41
|
+
assert_equal :text , @hash["class"]
|
42
|
+
assert_equal 3 , @hash[:size]
|
43
43
|
assert_nil @hash[:wrong_key]
|
44
44
|
|
45
45
|
assert @hash.keys.include?(:size)
|
46
|
-
assert_equal @hash.article.size
|
46
|
+
assert_equal 3, @hash.article.size
|
47
47
|
end
|
48
48
|
|
49
49
|
def test_hash_should_support_read_of_values_as_methods
|
50
|
-
assert_equal @hash.article.last.title
|
51
|
-
assert_equal @hash.article[1].author
|
52
|
-
assert_equal @hash.article.last.info.category.first
|
50
|
+
assert_equal "Article 3", @hash.article.last.title
|
51
|
+
assert_equal "Foo Bar" , @hash.article[1].author
|
52
|
+
assert_equal :sports , @hash.article.last.info.category.first
|
53
53
|
assert_nil @hash.wrong_key
|
54
54
|
end
|
55
55
|
|
56
56
|
def test_should_free_existant_methods_by_default
|
57
|
-
assert_equal @hash.size
|
58
|
-
assert_equal @hash.
|
59
|
-
assert_equal @hash.id
|
57
|
+
assert_equal 3 , @hash.size
|
58
|
+
assert_equal :text , @hash.class
|
59
|
+
assert_equal 123456789, @hash.id
|
60
60
|
end
|
61
61
|
|
62
62
|
def test_should_be_able_to_call_previously_freed_methods
|
63
|
-
assert_equal
|
64
|
-
begin
|
63
|
+
assert_equal 4, @hash.__size__
|
64
|
+
begin
|
65
65
|
$stderr = StringIO.new
|
66
|
-
assert_equal
|
66
|
+
assert_equal Hash, @hash.__class__
|
67
67
|
$stderr.rewind
|
68
68
|
ensure
|
69
69
|
$stderr = STDERR
|
70
70
|
end
|
71
|
-
assert_not_equal @hash.__id__
|
71
|
+
assert_not_equal 123456789, @hash.__id__
|
72
72
|
end
|
73
73
|
|
74
74
|
def test_should_enable_write_operations
|
@@ -85,14 +85,14 @@ class MethodizeTest < Test::Unit::TestCase
|
|
85
85
|
@hash["inspect"] = false
|
86
86
|
@hash.size = 4
|
87
87
|
|
88
|
-
assert_equal @hash.article[2].title
|
89
|
-
assert_equal @hash.article[1].info.published
|
90
|
-
assert_equal @hash.article.last.author
|
91
|
-
assert_equal @hash.shift
|
92
|
-
assert_equal @hash.inspect
|
93
|
-
assert_equal @hash.__inspect__.class
|
94
|
-
assert_equal @hash.size
|
95
|
-
assert_equal @hash.__size__
|
88
|
+
assert_equal "Article 3" , @hash.article[2].title
|
89
|
+
assert_equal "2010-08-31" , @hash.article[1].info.published
|
90
|
+
assert_equal "Marty Mcfly", @hash.article.last.author
|
91
|
+
assert_equal 12 , @hash.shift
|
92
|
+
assert_equal false , @hash.inspect
|
93
|
+
assert_equal String , @hash.__inspect__.class
|
94
|
+
assert_equal 4 , @hash.size
|
95
|
+
assert_equal 6 , @hash.__size__
|
96
96
|
end
|
97
97
|
end
|
98
98
|
|
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: methodize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 23
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- Luis Cipriani
|
@@ -9,7 +15,7 @@ autorequire:
|
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date:
|
18
|
+
date: 2011-06-03 00:00:00 -03:00
|
13
19
|
default_executable:
|
14
20
|
dependencies: []
|
15
21
|
|
@@ -37,21 +43,27 @@ rdoc_options: []
|
|
37
43
|
require_paths:
|
38
44
|
- lib
|
39
45
|
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
40
47
|
requirements:
|
41
48
|
- - ">="
|
42
49
|
- !ruby/object:Gem::Version
|
50
|
+
hash: 3
|
51
|
+
segments:
|
52
|
+
- 0
|
43
53
|
version: "0"
|
44
|
-
version:
|
45
54
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
46
56
|
requirements:
|
47
57
|
- - ">="
|
48
58
|
- !ruby/object:Gem::Version
|
59
|
+
hash: 3
|
60
|
+
segments:
|
61
|
+
- 0
|
49
62
|
version: "0"
|
50
|
-
version:
|
51
63
|
requirements: []
|
52
64
|
|
53
65
|
rubyforge_project:
|
54
|
-
rubygems_version: 1.3.
|
66
|
+
rubygems_version: 1.3.7
|
55
67
|
signing_key:
|
56
68
|
specification_version: 3
|
57
69
|
summary: Module to read from and write to the keys of a ruby Hash using methods
|