dynahash 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/dynahash.rb CHANGED
@@ -4,13 +4,7 @@
4
4
  # An extension to Ruby's Hash class that allows for dot-notation
5
5
  # access to a Hash's keys as if they were just properties on the
6
6
  # object.
7
- # It ignores:
8
- # * @ (xml-simple gem attribute prefix)
9
- # * : (symbol notation)
10
- # * - (dash in name)
11
- #
12
- # It replaces spaces with underscores to more closely resemble
13
- # a property name.
7
+ # It strips out non-word characters and replaces them with '_'
14
8
  #
15
9
  # Based off of my .NET/C# HyperDynamo work at
16
10
  # http://github.com/tonyheupel/hypercore
@@ -29,6 +23,8 @@ class Hash
29
23
  # the original name passed in and let the
30
24
  # caller handle it.
31
25
  name = m.to_s
32
- self.keys.find(name) { |key| key.to_s.gsub(/(@|:|\-)/, '').gsub(' ', '_').casecmp(name) == 0 }
26
+ self.keys.find(name) { |key|
27
+ key.to_s.gsub(/(@+|:+|\-+|\*+\.+)/, ' ').scan(/\w+/).join('_').casecmp(name) == 0
28
+ }
33
29
  end
34
- end
30
+ end
data/lib/dynahash.rb~ ADDED
@@ -0,0 +1,30 @@
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 CHANGED
@@ -35,24 +35,24 @@ class TestDynaHash < Test::Unit::TestCase
35
35
  assert_equal(h['Zed Shaw'], h.Zed_Shaw)
36
36
  end
37
37
 
38
- def test_remove_at_symbol
38
+ def test_replace_at_symbol
39
39
  h = {'@name' => 'Tony', 'gen@der' => :male}
40
40
 
41
41
  assert_equal('Tony', h.name)
42
- assert_equal(:male, h.gender)
42
+ assert_equal(:male, h.gen_der)
43
43
  end
44
44
 
45
- def test_remove_colon_symbol
46
- h = {:name => 'Tony', 'gen:der' => :male}
45
+ def test_replace_colon_symbol
46
+ h = {:project => 'DynaHash', 'test::db' => :success}
47
47
 
48
- assert_equal('Tony', h.name)
49
- assert_equal(:male, h.gender)
48
+ assert_equal('DynaHash', h.project)
49
+ assert_equal(:success, h.test_db)
50
50
  end
51
51
 
52
- def test_remove_dash_symbol
53
- h = {'My-Name-Is' => 'Slim Shady', '-gender-' => :male}
52
+ def test_replace_dash_symbol
53
+ h = {'My-Name-Is' => 'Slim Shady', '---gender-' => :male}
54
54
 
55
- assert_equal('Slim Shady', h.MyNameIs)
55
+ assert_equal('Slim Shady', h.My_Name_Is)
56
56
  assert_equal(:male, h.gender)
57
57
  end
58
58
 
@@ -62,7 +62,7 @@ class TestDynaHash < Test::Unit::TestCase
62
62
  assert_equal('Tony', h.name)
63
63
  assert_equal(:male, h.person_gender)
64
64
  assert_equal('coolness', h.dude_type)
65
- assert_equal('um, ok', h.WTF)
65
+ assert_equal('um, ok', h.W_T_F)
66
66
 
67
67
  end
68
- end
68
+ end
data/test/test.rb~ ADDED
@@ -0,0 +1,68 @@
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
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynahash
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
- version: "0.1"
8
+ - 2
9
+ version: "0.2"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Tony Heupel
@@ -14,11 +14,11 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-10-17 00:00:00 -07:00
17
+ date: 2010-11-06 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
21
- description: DynaHash changs the Hash class method_missing to see if it can map the property being accessed to a key in the Hash itself. It saves typing and looks more like a regular object instance.
21
+ description: "Dynahash allows you to access Hash keys as if they were properties with some logical replacements. For example:\\n\\nh = {:first_name => \"Tony\", \"**the gender**\" => :male}\\nh.name # => \"Tony\"\\nh.the_gender # => :male"
22
22
  email:
23
23
  - tonyheupel@gmail.com
24
24
  executables: []
@@ -29,8 +29,10 @@ extra_rdoc_files: []
29
29
 
30
30
  files:
31
31
  - lib/dynahash.rb
32
+ - lib/dynahash.rb~
32
33
  - LICENSE
33
34
  - test/test.rb
35
+ - test/test.rb~
34
36
  has_rdoc: true
35
37
  homepage: http://github.com/tonyheupel/dynahash
36
38
  licenses: []
@@ -69,3 +71,4 @@ specification_version: 3
69
71
  summary: Reference Hash keys as if they were dot-notation properties
70
72
  test_files:
71
73
  - test/test.rb
74
+ - test/test.rb~