dynahash 0.2 → 0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/dynahash.rb +3 -1
- data/test/test.rb +9 -4
- metadata +5 -20
- data/lib/dynahash.rb~ +0 -30
- data/test/test.rb~ +0 -68
data/lib/dynahash.rb
CHANGED
@@ -24,7 +24,9 @@ class Hash
|
|
24
24
|
# caller handle it.
|
25
25
|
name = m.to_s
|
26
26
|
self.keys.find(name) { |key|
|
27
|
-
key.to_s.gsub(/(@+|:+|\-+|\*+\.+)/, ' ').scan(/\w+/).join('_')
|
27
|
+
val = key.to_s.gsub(/(@+|:+|\-+|\*+\.+)/, ' ').gsub(/(?<before>[a-z])(?<after>[A-Z])/, '\k<before>_\k<after>').scan(/\w+/).join('_')
|
28
|
+
puts val
|
29
|
+
val.casecmp(name) == 0
|
28
30
|
}
|
29
31
|
end
|
30
32
|
end
|
data/test/test.rb
CHANGED
@@ -7,8 +7,8 @@ class TestDynaHash < Test::Unit::TestCase
|
|
7
7
|
def test_case_matches
|
8
8
|
h = {'UserName' => 'tonyheupel', 'Password' => 'yeah, right'}
|
9
9
|
|
10
|
-
assert_equal('tonyheupel', h.
|
11
|
-
assert_equal(h['UserName'], h.
|
10
|
+
assert_equal('tonyheupel', h.User_Name)
|
11
|
+
assert_equal(h['UserName'], h.User_Name)
|
12
12
|
|
13
13
|
assert_equal('yeah, right', h.Password)
|
14
14
|
assert_equal(h['Password'], h.Password)
|
@@ -18,8 +18,8 @@ class TestDynaHash < Test::Unit::TestCase
|
|
18
18
|
def test_case_insensitive
|
19
19
|
h = {'UserName' => 'tonyheupel', 'Password' => 'yeah, right'}
|
20
20
|
|
21
|
-
assert_equal('tonyheupel', h.
|
22
|
-
assert_equal('tonyheupel', h.
|
21
|
+
assert_equal('tonyheupel', h.user_name)
|
22
|
+
assert_equal('tonyheupel', h.uSER_naMe)
|
23
23
|
|
24
24
|
assert_equal('yeah, right', h.password)
|
25
25
|
assert_equal('yeah, right', h.pASSwOrD)
|
@@ -56,6 +56,11 @@ class TestDynaHash < Test::Unit::TestCase
|
|
56
56
|
assert_equal(:male, h.gender)
|
57
57
|
end
|
58
58
|
|
59
|
+
def test_replace_camel_case_with_underscore
|
60
|
+
h = { 'SomethingGreat' => 'A million bucks' }
|
61
|
+
assert_equal('A million bucks', h.something_great)
|
62
|
+
end
|
63
|
+
|
59
64
|
def test_all_removals_and_replacements_together
|
60
65
|
h = {'@name' => 'Tony', 'Person Gender' => :male, :dude_type => 'coolness', '-w:T@@F--' => 'um, ok' }
|
61
66
|
|
metadata
CHANGED
@@ -1,12 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dynahash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 2
|
9
|
-
version: "0.2"
|
4
|
+
prerelease:
|
5
|
+
version: "0.3"
|
10
6
|
platform: ruby
|
11
7
|
authors:
|
12
8
|
- Tony Heupel
|
@@ -14,11 +10,11 @@ autorequire:
|
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
12
|
|
17
|
-
date:
|
13
|
+
date: 2011-08-03 00:00:00 -07:00
|
18
14
|
default_executable:
|
19
15
|
dependencies: []
|
20
16
|
|
21
|
-
description: "Dynahash allows you to access Hash keys as if they were properties
|
17
|
+
description: "Dynahash allows you to access Hash keys as if they were properties. For example: h = {:first_name => \"Tony\", \"LastName\" => \"Heupel\", \"**the gender**\" => :male}; h.first_name; # => \"Tony\" h.last_name; # => \"Heupel\" h.the_gender # => :male"
|
22
18
|
email:
|
23
19
|
- tonyheupel@gmail.com
|
24
20
|
executables: []
|
@@ -29,10 +25,8 @@ extra_rdoc_files: []
|
|
29
25
|
|
30
26
|
files:
|
31
27
|
- lib/dynahash.rb
|
32
|
-
- lib/dynahash.rb~
|
33
28
|
- LICENSE
|
34
29
|
- test/test.rb
|
35
|
-
- test/test.rb~
|
36
30
|
has_rdoc: true
|
37
31
|
homepage: http://github.com/tonyheupel/dynahash
|
38
32
|
licenses: []
|
@@ -47,28 +41,19 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
47
41
|
requirements:
|
48
42
|
- - ">="
|
49
43
|
- !ruby/object:Gem::Version
|
50
|
-
hash: 3
|
51
|
-
segments:
|
52
|
-
- 0
|
53
44
|
version: "0"
|
54
45
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
46
|
none: false
|
56
47
|
requirements:
|
57
48
|
- - ">="
|
58
49
|
- !ruby/object:Gem::Version
|
59
|
-
hash: 21
|
60
|
-
segments:
|
61
|
-
- 1
|
62
|
-
- 3
|
63
|
-
- 7
|
64
50
|
version: 1.3.7
|
65
51
|
requirements: []
|
66
52
|
|
67
53
|
rubyforge_project: dynahash
|
68
|
-
rubygems_version: 1.
|
54
|
+
rubygems_version: 1.6.2
|
69
55
|
signing_key:
|
70
56
|
specification_version: 3
|
71
57
|
summary: Reference Hash keys as if they were dot-notation properties
|
72
58
|
test_files:
|
73
59
|
- test/test.rb
|
74
|
-
- test/test.rb~
|
data/lib/dynahash.rb~
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
# = DynaHash
|
2
|
-
# (c) 2010 Tony Heupel
|
3
|
-
#
|
4
|
-
# An extension to Ruby's Hash class that allows for dot-notation
|
5
|
-
# access to a Hash's keys as if they were just properties on the
|
6
|
-
# object.
|
7
|
-
# It strips out non-word characters and replaces them with '_'
|
8
|
-
#
|
9
|
-
# Based off of my .NET/C# HyperDynamo work at
|
10
|
-
# http://github.com/tonyheupel/hypercore
|
11
|
-
#
|
12
|
-
class Hash
|
13
|
-
|
14
|
-
def method_missing(m, *args, &block)
|
15
|
-
self.fetch(find_member(m))
|
16
|
-
end
|
17
|
-
|
18
|
-
private
|
19
|
-
|
20
|
-
def find_member(m)
|
21
|
-
# get the first key that matches the test
|
22
|
-
# if none of the keys match, just return
|
23
|
-
# the original name passed in and let the
|
24
|
-
# caller handle it.
|
25
|
-
name = m.to_s
|
26
|
-
self.keys.find(name) { |key| key.to_s.gsub(/(^@+|@+$|^:+|:+$|^\-+|\_+$|^\*+|\*+$|^\.+|\.+$)/, '').
|
27
|
-
gsub(/(@+|:+|\-+|\*+\.+)/, '_').
|
28
|
-
scan(/\w+/).join('_').casecmp(name) == 0 }
|
29
|
-
end
|
30
|
-
end
|
data/test/test.rb~
DELETED
@@ -1,68 +0,0 @@
|
|
1
|
-
# !/usr/bin/env/ ruby
|
2
|
-
require "test/unit"
|
3
|
-
|
4
|
-
require "../lib/dynahash"
|
5
|
-
|
6
|
-
class TestDynaHash < Test::Unit::TestCase
|
7
|
-
def test_case_matches
|
8
|
-
h = {'UserName' => 'tonyheupel', 'Password' => 'yeah, right'}
|
9
|
-
|
10
|
-
assert_equal('tonyheupel', h.UserName)
|
11
|
-
assert_equal(h['UserName'], h.UserName)
|
12
|
-
|
13
|
-
assert_equal('yeah, right', h.Password)
|
14
|
-
assert_equal(h['Password'], h.Password)
|
15
|
-
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_case_insensitive
|
19
|
-
h = {'UserName' => 'tonyheupel', 'Password' => 'yeah, right'}
|
20
|
-
|
21
|
-
assert_equal('tonyheupel', h.username)
|
22
|
-
assert_equal('tonyheupel', h.uSERnaMe)
|
23
|
-
|
24
|
-
assert_equal('yeah, right', h.password)
|
25
|
-
assert_equal('yeah, right', h.pASSwOrD)
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_replace_space_with_underscore
|
29
|
-
h = {'Tony Heupel' => 'cool', 'Zed Shaw' => 'coolest'}
|
30
|
-
|
31
|
-
assert_equal('cool', h.Tony_Heupel)
|
32
|
-
assert_equal(h['Tony Heupel'], h.Tony_Heupel)
|
33
|
-
|
34
|
-
assert_equal('coolest', h.Zed_Shaw)
|
35
|
-
assert_equal(h['Zed Shaw'], h.Zed_Shaw)
|
36
|
-
end
|
37
|
-
|
38
|
-
def test_replace_at_symbol
|
39
|
-
h = {'@name' => 'Tony', 'gen@der' => :male}
|
40
|
-
|
41
|
-
assert_equal('Tony', h.name)
|
42
|
-
assert_equal(:male, h.gen_der)
|
43
|
-
end
|
44
|
-
|
45
|
-
def test_replace_colon_symbol
|
46
|
-
h = {:project => 'DynaHash', 'test:db' => :success}
|
47
|
-
|
48
|
-
assert_equal('DynaHash', h.project)
|
49
|
-
assert_equal(:success, h.test_db)
|
50
|
-
end
|
51
|
-
|
52
|
-
def test_replace_dash_symbol
|
53
|
-
h = {'My-Name-Is' => 'Slim Shady', '-gender-' => :male}
|
54
|
-
|
55
|
-
assert_equal('Slim Shady', h.My_Name_Is)
|
56
|
-
assert_equal(:male, h.gender)
|
57
|
-
end
|
58
|
-
|
59
|
-
def test_all_removals_and_replacements_together
|
60
|
-
h = {'@name' => 'Tony', 'Person Gender' => :male, :dude_type => 'coolness', '-w:T@@F--' => 'um, ok' }
|
61
|
-
|
62
|
-
assert_equal('Tony', h.name)
|
63
|
-
assert_equal(:male, h.person_gender)
|
64
|
-
assert_equal('coolness', h.dude_type)
|
65
|
-
assert_equal('um, ok', h.W_T_F)
|
66
|
-
|
67
|
-
end
|
68
|
-
end
|