xml_hate 0.8.0 → 0.8.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/xml_hate/node.rb +24 -7
- data/lib/xml_hate/version.rb +1 -1
- data/spec/xml_hate/node_spec.rb +40 -0
- metadata +61 -37
data/lib/xml_hate/node.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
module XmlHate
|
2
2
|
class Node
|
3
3
|
def initialize(hash)
|
4
|
-
hash.each
|
4
|
+
all_items_in_the_hash_that_are_not_nil(hash).each do |k, v|
|
5
|
+
create_accessor_for k, convert_the_value_to_the_appropriate_form(v)
|
6
|
+
end
|
5
7
|
end
|
6
8
|
|
7
9
|
def method_missing(meth, *args, &blk)
|
@@ -10,22 +12,37 @@ module XmlHate
|
|
10
12
|
|
11
13
|
private
|
12
14
|
|
15
|
+
def all_items_in_the_hash_that_are_not_nil(the_hash)
|
16
|
+
the_hash.select{|k, v| !v.nil? }
|
17
|
+
end
|
18
|
+
|
13
19
|
def an_empty_array_for_plurals_or_empty_string_for_singulars(meth)
|
14
20
|
meth.to_s == meth.to_s.pluralize ? [] : ""
|
15
21
|
end
|
16
22
|
|
17
23
|
def create_accessor_for(k, v)
|
18
|
-
|
24
|
+
property_name = k.to_s.gsub('-', '_').to_sym
|
25
|
+
self.instance_variable_set("@#{property_name}", v)
|
19
26
|
self.instance_eval("
|
20
27
|
class << self
|
21
|
-
attr_accessor :#{
|
28
|
+
attr_accessor :#{property_name}
|
22
29
|
end")
|
23
30
|
end
|
24
31
|
|
25
|
-
def convert_the_value_to_the_appropriate_form(
|
26
|
-
return Node.new(
|
27
|
-
return
|
28
|
-
|
32
|
+
def convert_the_value_to_the_appropriate_form(the_value)
|
33
|
+
return Node.new(the_value) if the_value.class == Hashie::Mash
|
34
|
+
return the_value.map {|i| i.class == Hashie::Mash ? Node.new(i) : i} if the_value.class == Array
|
35
|
+
attempt_to_attach_content_singleton_to_the_value the_value
|
36
|
+
the_value
|
37
|
+
end
|
38
|
+
|
39
|
+
def attempt_to_attach_content_singleton_to_the_value(the_value)
|
40
|
+
begin
|
41
|
+
def the_value.content
|
42
|
+
self
|
43
|
+
end
|
44
|
+
rescue
|
45
|
+
end
|
29
46
|
end
|
30
47
|
end
|
31
48
|
end
|
data/lib/xml_hate/version.rb
CHANGED
data/spec/xml_hate/node_spec.rb
CHANGED
@@ -77,4 +77,44 @@ describe XmlHate::Node do
|
|
77
77
|
@object2.respond_to?(:last_name).must_equal false
|
78
78
|
end
|
79
79
|
end
|
80
|
+
|
81
|
+
describe "passing nil values as strings and sets" do
|
82
|
+
before do
|
83
|
+
@object = XmlHate::Node.new({:person => nil, :products => nil})
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should return an empty string for the singular value" do
|
87
|
+
@object.person.must_equal ""
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should return an empty set for the plural value" do
|
91
|
+
@object.products.must_equal []
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe "using .content to access string values" do
|
96
|
+
before do
|
97
|
+
@object = XmlHate::Node.new({:firstname => "Ellis", :lastname => "Wyatt"})
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should return strings for the first and last names" do
|
101
|
+
@object.firstname.must_equal "Ellis"
|
102
|
+
@object.lastname.must_equal "Wyatt"
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should allow me to acces the value for the string using .content as well" do
|
106
|
+
@object.firstname.content.must_equal "Ellis"
|
107
|
+
@object.lastname.content.must_equal "Wyatt"
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
describe "passing a key with dashes" do
|
112
|
+
before do
|
113
|
+
@object = XmlHate::Node.new({:"test-this" => "ok"})
|
114
|
+
end
|
115
|
+
|
116
|
+
it "should replace the dash with an underscore" do
|
117
|
+
@object.test_this.must_equal "ok"
|
118
|
+
end
|
119
|
+
end
|
80
120
|
end
|
metadata
CHANGED
@@ -1,45 +1,60 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: xml_hate
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 57
|
5
5
|
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 8
|
9
|
+
- 3
|
10
|
+
version: 0.8.3
|
6
11
|
platform: ruby
|
7
|
-
authors:
|
12
|
+
authors:
|
8
13
|
- Darren Cauthon
|
9
14
|
autorequire:
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
17
|
+
|
18
|
+
date: 2012-03-11 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
15
21
|
name: xml-simple
|
16
|
-
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
17
24
|
none: false
|
18
|
-
requirements:
|
19
|
-
- -
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
22
32
|
type: :runtime
|
23
|
-
|
24
|
-
|
25
|
-
- !ruby/object:Gem::Dependency
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
26
35
|
name: hashie
|
27
|
-
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
28
38
|
none: false
|
29
|
-
requirements:
|
30
|
-
- -
|
31
|
-
- !ruby/object:Gem::Version
|
32
|
-
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 3
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
version: "0"
|
33
46
|
type: :runtime
|
34
|
-
|
35
|
-
version_requirements: *70214612533220
|
47
|
+
version_requirements: *id002
|
36
48
|
description: I hate xml.
|
37
|
-
email:
|
49
|
+
email:
|
38
50
|
- darren@cauthon.com
|
39
51
|
executables: []
|
52
|
+
|
40
53
|
extensions: []
|
54
|
+
|
41
55
|
extra_rdoc_files: []
|
42
|
-
|
56
|
+
|
57
|
+
files:
|
43
58
|
- .gitignore
|
44
59
|
- .rvmrc
|
45
60
|
- Gemfile
|
@@ -54,31 +69,40 @@ files:
|
|
54
69
|
- spec/xml_hate/document_spec.rb
|
55
70
|
- spec/xml_hate/node_spec.rb
|
56
71
|
- xml_hate.gemspec
|
57
|
-
homepage:
|
72
|
+
homepage: ""
|
58
73
|
licenses: []
|
74
|
+
|
59
75
|
post_install_message:
|
60
76
|
rdoc_options: []
|
61
|
-
|
77
|
+
|
78
|
+
require_paths:
|
62
79
|
- lib
|
63
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
81
|
none: false
|
65
|
-
requirements:
|
66
|
-
- -
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
|
69
|
-
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
hash: 3
|
86
|
+
segments:
|
87
|
+
- 0
|
88
|
+
version: "0"
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
90
|
none: false
|
71
|
-
requirements:
|
72
|
-
- -
|
73
|
-
- !ruby/object:Gem::Version
|
74
|
-
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
hash: 3
|
95
|
+
segments:
|
96
|
+
- 0
|
97
|
+
version: "0"
|
75
98
|
requirements: []
|
99
|
+
|
76
100
|
rubyforge_project: xml_hate
|
77
101
|
rubygems_version: 1.8.10
|
78
102
|
signing_key:
|
79
103
|
specification_version: 3
|
80
104
|
summary: Handling xml with my own ten-foot poll.
|
81
|
-
test_files:
|
105
|
+
test_files:
|
82
106
|
- spec/spec_helper.rb
|
83
107
|
- spec/xml_hate/document_spec.rb
|
84
108
|
- spec/xml_hate/node_spec.rb
|