methodize 0.2.0 → 0.2.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.
@@ -1,4 +1,21 @@
1
1
  module Methodize
2
+ def self.extend_object(base)
3
+ __normalize__(base)
4
+ end
5
+
6
+ def self.__normalize__(value)
7
+ case value
8
+ when Hash
9
+ value.extend(MethodizedHash) unless value.kind_of?(MethodizedHash)
10
+ when Array
11
+ value.extend(MethodizedArray) unless value.kind_of?(MethodizedArray)
12
+ end
13
+ value
14
+ end
15
+
16
+ end
17
+
18
+ module MethodizedHash
2
19
  def self.extended(base)
3
20
  # ruby >1.9 returns an array of symbols for object.public_methods
4
21
  # while <1.9 returns an array of string. This methods guess it right
@@ -6,17 +23,18 @@ module Methodize
6
23
 
7
24
  # if some of the Hash keys and public methods names conflict
8
25
  # we free the existant method to enable the user to call it
26
+ __metaclass__ = base.__metaclass__
9
27
  base.keys.each do |k|
10
- base.__free_method__(k.to_sym) if base.public_methods.include?(@@key_coerce.call(k))
28
+ base.__free_method__(k.to_sym, __metaclass__) if base.public_methods.include?(@@key_coerce.call(k))
11
29
  end
12
30
  end
13
31
 
14
32
  def [](key)
15
- __normalize__(super(key))
33
+ ::Methodize.__normalize__(super(key))
16
34
  end
17
35
 
18
36
  def []=(key, value)
19
- __free_method__(key) if !self.keys.include?(key) && self.public_methods.include?(@@key_coerce.call(key))
37
+ __free_method__(key) if !keys.include?(key) && public_methods.include?(@@key_coerce.call(key))
20
38
  super(key,value)
21
39
  end
22
40
 
@@ -24,11 +42,10 @@ module Methodize
24
42
  method_name = name.to_s
25
43
  if method_name[-1,1] == '='
26
44
  method_name = method_name.chop
27
- self.key?(method_name) ? key = method_name : key = method_name.to_sym
45
+ key = key?(method_name) ? method_name : method_name.to_sym
28
46
  self[key] = args[0]
29
47
  else
30
- self.key?(method_name) ? key = method_name : key = method_name.to_sym
31
- self[key]
48
+ __fetch__key__(method_name)
32
49
  end
33
50
  end
34
51
 
@@ -36,27 +53,32 @@ module Methodize
36
53
  # you can use this to free the method and use the method obj.size
37
54
  # to access the value of key "size".
38
55
  # you still can access the old method with __[method_name]__
39
- def __free_method__(sym)
40
- self.__metaclass__.send(:alias_method, "__#{sym.to_s}__".to_sym, sym) unless self.respond_to?("__#{sym.to_s}__")
41
- self.__metaclass__.send(:define_method, sym) { method_missing(sym.to_s) }
56
+ def __free_method__(sym, __metaclass__ = self.__metaclass__)
57
+ __sym__ = "__#{sym}__"
58
+ __metaclass__.send(:alias_method, __sym__.to_sym, sym) unless respond_to?(__sym__)
59
+ __metaclass__.send(:define_method, sym) { __fetch__key__(sym.to_s) }
42
60
  self
43
61
  end
44
62
 
45
63
  def __metaclass__
46
64
  class << self; self; end
47
65
  end
48
-
66
+
49
67
  private
50
68
 
51
- def __normalize__(value)
52
- case value
53
- when Hash
54
- value.extend(Methodize)
55
- when Array
56
- value.map { |v| __normalize__(v) }
57
- else
58
- value
59
- end
60
- value
69
+ def __fetch__key__(key)
70
+ self[key?(key) ? key : key.to_sym]
71
+ end
72
+ end
73
+
74
+ module MethodizedArray
75
+ def [](*args)
76
+ ::Methodize.__normalize__(super(*args))
77
+ end
78
+ def first(*args)
79
+ ::Methodize.__normalize__(super(*args))
80
+ end
81
+ def last(*args)
82
+ ::Methodize.__normalize__(super(*args))
61
83
  end
62
- end
84
+ end
@@ -2,7 +2,6 @@ require 'test/unit'
2
2
  require 'methodize/hash'
3
3
 
4
4
  require 'rubygems'
5
- require 'ruby-debug'
6
5
 
7
6
  class MethodizeHashTest < Test::Unit::TestCase
8
7
 
@@ -41,7 +40,7 @@ class MethodizeHashTest < Test::Unit::TestCase
41
40
  assert_equal :text , @hash["type"]
42
41
  assert_equal 3 , @hash[:size]
43
42
  assert_nil @hash[:wrong_key]
44
-
43
+
45
44
  assert @hash.keys.include?(:size)
46
45
  assert_equal 3, @hash.article.size
47
46
  end
@@ -51,10 +50,10 @@ class MethodizeHashTest < Test::Unit::TestCase
51
50
  assert_equal "Foo Bar" , @hash.article[1].author
52
51
  assert_equal :sports , @hash.article.last.info.category.first
53
52
  assert_nil @hash.wrong_key
54
-
53
+
55
54
  assert_equal 3 , @hash.size
56
55
  assert_equal :text , @hash.type
57
- assert_equal 123456789, @hash.id
56
+ assert_equal 123456789, @hash.id
58
57
  end
59
58
 
60
59
  def test_double_methodize_call_does_not_affect_anything
@@ -2,7 +2,6 @@ require 'test/unit'
2
2
  require 'methodize'
3
3
 
4
4
  require 'rubygems'
5
- require 'ruby-debug'
6
5
 
7
6
  class MethodizeTest < Test::Unit::TestCase
8
7
 
@@ -41,7 +40,7 @@ class MethodizeTest < Test::Unit::TestCase
41
40
  assert_equal :text , @hash["class"]
42
41
  assert_equal 3 , @hash[:size]
43
42
  assert_nil @hash[:wrong_key]
44
-
43
+
45
44
  assert @hash.keys.include?(:size)
46
45
  assert_equal 3, @hash.article.size
47
46
  end
@@ -70,7 +69,7 @@ class MethodizeTest < Test::Unit::TestCase
70
69
  end
71
70
  assert_not_equal 123456789, @hash.__id__
72
71
  end
73
-
72
+
74
73
  def test_should_enable_write_operations
75
74
  @hash.article.last.title = "Article 3"
76
75
  @hash.article[1].info = {
@@ -84,7 +83,7 @@ class MethodizeTest < Test::Unit::TestCase
84
83
  @hash.shift = 12
85
84
  @hash["inspect"] = false
86
85
  @hash.size = 4
87
-
86
+
88
87
  assert_equal "Article 3" , @hash.article[2].title
89
88
  assert_equal "2010-08-31" , @hash.article[1].info.published
90
89
  assert_equal "Marty Mcfly", @hash.article.last.author
metadata CHANGED
@@ -1,26 +1,26 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: methodize
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
5
- prerelease: false
4
+ hash: 21
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 0
10
- version: 0.2.0
9
+ - 1
10
+ version: 0.2.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Luis Cipriani
14
+ - Marcelo Manzan
14
15
  autorequire:
15
16
  bindir: bin
16
17
  cert_chain: []
17
18
 
18
- date: 2011-06-03 00:00:00 -03:00
19
- default_executable:
19
+ date: 2012-02-10 00:00:00 Z
20
20
  dependencies: []
21
21
 
22
22
  description:
23
- email: lfcipriani@talleye.com
23
+ email: lfcipriani@gmail.com
24
24
  executables: []
25
25
 
26
26
  extensions: []
@@ -33,7 +33,6 @@ files:
33
33
  - lib/methodize/hash.rb
34
34
  - test/methodize_test.rb
35
35
  - test/hash_test.rb
36
- has_rdoc: true
37
36
  homepage: http://blog.talleye.com
38
37
  licenses: []
39
38
 
@@ -63,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
63
62
  requirements: []
64
63
 
65
64
  rubyforge_project:
66
- rubygems_version: 1.3.7
65
+ rubygems_version: 1.8.15
67
66
  signing_key:
68
67
  specification_version: 3
69
68
  summary: Module to read from and write to the keys of a ruby Hash using methods