xmlparsable 1.5.3 → 1.5.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -39,11 +39,26 @@ module XmlParsable
39
39
  @parsers ||= Hash.new{|h,k| parent[k] }
40
40
  @parsers[elem_name.to_s] = [elem_type.parsable, arguments]
41
41
 
42
- # Define accessor method (or don't, see if I care!)
43
- attr_name = arguments.fetch(:accessor, elem_name)
42
+ # Define reader and writer methods (or don't, see if I care!)
43
+ accessor = arguments.fetch(:accessor, elem_name)
44
+ reader = arguments.fetch(:reader, accessor)
45
+ writer = arguments.fetch(:writer, accessor)
46
+
47
+ if reader and not method_defined?(reader)
48
+ define_method(reader) do
49
+ hash = instance_variable_get(:@xmlelems)
50
+ hash and hash[elem_name.to_sym]
51
+ end
52
+ end
44
53
 
45
- if attr_name and not method_defined?(attr_name)
46
- define_method(attr_name) { instance_variable_get(:@xmlelems)[elem_name.to_sym] }
54
+ if writer and not method_defined?("#{writer}=")
55
+ define_method("#{writer}=") do |value|
56
+ unless hash = instance_variable_get(:@xmlelems)
57
+ hash = Hash.new{|h,k| h.fetch(k.to_sym, nil) }
58
+ instance_variable_set(:@xmlelems, hash)
59
+ end
60
+ hash.update(elem_name.to_sym => value)
61
+ end
47
62
  end
48
63
  end
49
64
 
@@ -13,6 +13,37 @@ describe XmlParsable, "accessors" do
13
13
  XML
14
14
  end
15
15
 
16
+ context "uninitialized" do
17
+ let(:parser) do
18
+ Class.new do
19
+ class Inner
20
+ attr_reader :xmlattrs
21
+ attr_reader :xmlelems
22
+
23
+ include XmlParsable
24
+ element :size
25
+ element :wise
26
+ element "a-z"
27
+ end
28
+
29
+ include XmlParsable
30
+ element :root, Inner
31
+ end
32
+ end
33
+
34
+ it "returns nil for elements" do
35
+ xml = parser.new
36
+ xml.root.should be_nil
37
+ end
38
+
39
+ it "returns nil for elements" do
40
+ xml = parser::Inner.new
41
+ xml.size.should be_nil
42
+ xml.wise.should be_nil
43
+ xml.send("a-z").should be_nil
44
+ end
45
+ end
46
+
16
47
  context "default" do
17
48
  let(:parser) do
18
49
  Class.new do
@@ -70,9 +101,9 @@ describe XmlParsable, "accessors" do
70
101
 
71
102
  include XmlParsable
72
103
  element :size
73
- element :wise, accessor: :wisdom
74
- element "a-z", accessor: "atoz"
75
- element :secret, accessor: false
104
+ element :wise, reader: :wisdom
105
+ element "a-z", reader: "atoz"
106
+ element :secret, reader: false
76
107
  end
77
108
 
78
109
  include XmlParsable
@@ -0,0 +1,38 @@
1
+ require "spec_helper"
2
+
3
+ describe XmlParsable, "mutators" do
4
+
5
+ context "default" do
6
+ let(:parser) do
7
+ Class.new do
8
+ attr_reader :xmlelems
9
+
10
+ include XmlParsable
11
+ element :size
12
+ element :wise
13
+ element "a-z"
14
+ end
15
+ end
16
+
17
+ let(:xml) do
18
+ parser.new.tap do |xml|
19
+ xml.size = 100
20
+ xml.wise = 200
21
+ xml.send("a-z=", 300)
22
+ end
23
+ end
24
+
25
+ it "stores elements in `xmlelems`" do
26
+ expected = Hash[:size => 100, :wise => 200, :"a-z" => 300]
27
+ xml.xmlelems.should == expected
28
+ end
29
+
30
+ it "stores elements with 'indifferent access'" do
31
+ pp xml
32
+ xml.size.should == 100
33
+ xml.wise.should == 200
34
+ xml.send("a-z").should == 300
35
+ end
36
+ end
37
+
38
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xmlparsable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.3
4
+ version: 1.5.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -35,30 +35,31 @@ extra_rdoc_files: []
35
35
  files:
36
36
  - README.md
37
37
  - Rakefile
38
+ - lib/xmlparsable.rb
39
+ - lib/xmlparsable/elements.rb
40
+ - lib/xmlparsable/parser.rb
41
+ - lib/xmlparsable/elements/string.rb
42
+ - lib/xmlparsable/elements/time.rb
43
+ - lib/xmlparsable/elements/date.rb
38
44
  - lib/xmlparsable/elements/abstract.rb
45
+ - lib/xmlparsable/elements/text.rb
46
+ - lib/xmlparsable/elements/xml.rb
39
47
  - lib/xmlparsable/elements/array.rb
40
- - lib/xmlparsable/elements/date.rb
41
48
  - lib/xmlparsable/elements/numeric.rb
42
49
  - lib/xmlparsable/elements/record.rb
43
- - lib/xmlparsable/elements/string.rb
44
- - lib/xmlparsable/elements/text.rb
45
- - lib/xmlparsable/elements/time.rb
46
- - lib/xmlparsable/elements/xml.rb
47
- - lib/xmlparsable/elements.rb
48
- - lib/xmlparsable/parser.rb
49
- - lib/xmlparsable.rb
50
- - spec/examples/accessors.example
50
+ - spec/spec_helper.rb
51
+ - spec/examples/repeated.example
52
+ - spec/examples/elements/string.example
53
+ - spec/examples/elements/xml.example
51
54
  - spec/examples/elements/array.example
52
55
  - spec/examples/elements/date.example
53
- - spec/examples/elements/numeric.example
56
+ - spec/examples/elements/time.example
54
57
  - spec/examples/elements/record.example
55
- - spec/examples/elements/string.example
56
58
  - spec/examples/elements/text.example
57
- - spec/examples/elements/time.example
58
- - spec/examples/elements/xml.example
59
+ - spec/examples/elements/numeric.example
60
+ - spec/examples/attributes/readers.example
61
+ - spec/examples/attributes/writers.example
59
62
  - spec/examples/inheritance.example
60
- - spec/examples/repeated.example
61
- - spec/spec_helper.rb
62
63
  homepage: https://github.com/kputnam/xmlparsable
63
64
  licenses: []
64
65
  post_install_message:
@@ -84,14 +85,15 @@ signing_key:
84
85
  specification_version: 3
85
86
  summary: Simple declarative XML parsing
86
87
  test_files:
87
- - spec/examples/accessors.example
88
+ - spec/examples/repeated.example
89
+ - spec/examples/elements/string.example
90
+ - spec/examples/elements/xml.example
88
91
  - spec/examples/elements/array.example
89
92
  - spec/examples/elements/date.example
90
- - spec/examples/elements/numeric.example
93
+ - spec/examples/elements/time.example
91
94
  - spec/examples/elements/record.example
92
- - spec/examples/elements/string.example
93
95
  - spec/examples/elements/text.example
94
- - spec/examples/elements/time.example
95
- - spec/examples/elements/xml.example
96
+ - spec/examples/elements/numeric.example
97
+ - spec/examples/attributes/readers.example
98
+ - spec/examples/attributes/writers.example
96
99
  - spec/examples/inheritance.example
97
- - spec/examples/repeated.example