dynahash 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +27 -0
- data/lib/dynahash.rb +34 -0
- data/test/test.rb +68 -0
- metadata +71 -0
data/LICENSE
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
DynaHash Ruby Gem
|
2
|
+
Copyright (c) 2010, Tony Heupel, All Rights Reserved
|
3
|
+
|
4
|
+
=====================================================
|
5
|
+
Licensed under the MIT License:
|
6
|
+
|
7
|
+
The MIT License
|
8
|
+
|
9
|
+
Copyright (c) 2010 Tony Heupel
|
10
|
+
|
11
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
12
|
+
of this software and associated documentation files (the "Software"), to deal
|
13
|
+
in the Software without restriction, including without limitation the rights
|
14
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
15
|
+
copies of the Software, and to permit persons to whom the Software is
|
16
|
+
furnished to do so, subject to the following conditions:
|
17
|
+
|
18
|
+
The above copyright notice and this permission notice shall be included in
|
19
|
+
all copies or substantial portions of the Software.
|
20
|
+
|
21
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
22
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
23
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
24
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
25
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
26
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
27
|
+
THE SOFTWARE.
|
data/lib/dynahash.rb
ADDED
@@ -0,0 +1,34 @@
|
|
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 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.
|
14
|
+
#
|
15
|
+
# Based off of my .NET/C# HyperDynamo work at
|
16
|
+
# http://github.com/tonyheupel/hypercore
|
17
|
+
#
|
18
|
+
class Hash
|
19
|
+
|
20
|
+
def method_missing(m, *args, &block)
|
21
|
+
self.fetch(find_member(m))
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def find_member(m)
|
27
|
+
# get the first key that matches the test
|
28
|
+
# if none of the keys match, just return
|
29
|
+
# the original name passed in and let the
|
30
|
+
# caller handle it.
|
31
|
+
name = m.to_s
|
32
|
+
self.keys.find(name) { |key| key.to_s.gsub(/(@|:|\-)/, '').gsub(' ', '_').casecmp(name) == 0 }
|
33
|
+
end
|
34
|
+
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_remove_at_symbol
|
39
|
+
h = {'@name' => 'Tony', 'gen@der' => :male}
|
40
|
+
|
41
|
+
assert_equal('Tony', h.name)
|
42
|
+
assert_equal(:male, h.gender)
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_remove_colon_symbol
|
46
|
+
h = {:name => 'Tony', 'gen:der' => :male}
|
47
|
+
|
48
|
+
assert_equal('Tony', h.name)
|
49
|
+
assert_equal(:male, h.gender)
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_remove_dash_symbol
|
53
|
+
h = {'My-Name-Is' => 'Slim Shady', '-gender-' => :male}
|
54
|
+
|
55
|
+
assert_equal('Slim Shady', h.MyNameIs)
|
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.WTF)
|
66
|
+
|
67
|
+
end
|
68
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dynahash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 9
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: "0.1"
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Tony Heupel
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-10-17 00:00:00 -07:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
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.
|
22
|
+
email:
|
23
|
+
- tonyheupel@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- lib/dynahash.rb
|
32
|
+
- LICENSE
|
33
|
+
- test/test.rb
|
34
|
+
has_rdoc: true
|
35
|
+
homepage: http://github.com/tonyheupel/dynahash
|
36
|
+
licenses: []
|
37
|
+
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
hash: 3
|
49
|
+
segments:
|
50
|
+
- 0
|
51
|
+
version: "0"
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 21
|
58
|
+
segments:
|
59
|
+
- 1
|
60
|
+
- 3
|
61
|
+
- 7
|
62
|
+
version: 1.3.7
|
63
|
+
requirements: []
|
64
|
+
|
65
|
+
rubyforge_project: dynahash
|
66
|
+
rubygems_version: 1.3.7
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: Reference Hash keys as if they were dot-notation properties
|
70
|
+
test_files:
|
71
|
+
- test/test.rb
|