nom-xml 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,6 +6,13 @@ module Nom::XML
6
6
  attr_writer :parent
7
7
  attr_reader :options
8
8
 
9
+ ##
10
+ # Create a new Nom::XML::Term
11
+ #
12
+ # @attr [Nom::XML::Term] parent
13
+ # @attr [String] name
14
+ # @attr [Hash] options
15
+ # @yield
9
16
  def initialize parent, name, options = {}, *args, &block
10
17
  @name = name
11
18
  @terms = {}
@@ -17,40 +24,59 @@ module Nom::XML
17
24
  end
18
25
  end
19
26
 
20
- def in_edit_context &block
21
- @edit_context = true
22
- yield
23
- @edit_context = false
24
- end
25
-
26
- def in_edit_context?
27
- @edit_context
28
- end
29
-
30
- def parent_xpath
31
- @parent_xpath ||= self.parent.xpath
32
- end
33
-
34
- def clear_parent_cache!
35
- @parent_xpath = nil
27
+ ##
28
+ # Traverse the tree to figure out what terminology this term belongs to
29
+ # @return [Nom::XML::Terminology]
30
+ def terminology
31
+ @terminology ||= parent.terminology
36
32
  end
37
33
 
34
+ ##
35
+ # Get the absolute xpath to this node
36
+ # @return [String]
38
37
  def xpath
39
38
  [parent_xpath, local_xpath].flatten.compact.join("/")
40
39
  end
41
40
 
41
+ ##
42
+ # Get the relative xpath to this node from its immediate parent's term
43
+ # @return [String]
42
44
  def local_xpath
43
45
  ("#{xmlns}:" unless xmlns.blank? ).to_s + (options[:path] || name).to_s
44
46
  end
45
47
 
46
- def xmlns
47
- (options[:xmlns] if options) || (self.parent.xmlns if self.parent)
48
- end
49
-
48
+ ##
49
+ # Get the document nodes associated with this term
50
+ # @return [Nokogiri::XML::NodeSet]
50
51
  def nodes
51
52
  terminology.document.root.xpath(xpath, terminology.namespaces)
52
53
  end
53
54
 
55
+ ##
56
+ # Does this term have a sub-term called term_name
57
+ # @attr [String] term_key
58
+ # @return [Boolean]
59
+ def key? term_key
60
+ terms.key? term_key
61
+ end
62
+
63
+ ##
64
+ # Flatten this term and all sub-terms (recursively)
65
+ # @return [Array]
66
+ def flatten
67
+ [self, terms.map { |k,v| v.flatten }].flatten
68
+ end
69
+
70
+ def respond_to? method, *args, &block
71
+ if in_edit_context?
72
+ true
73
+ elsif key? method
74
+ true
75
+ else
76
+ super
77
+ end
78
+ end
79
+
54
80
  def method_missing method, *args, &block
55
81
  if in_edit_context?
56
82
  add_term(method, *args, &block)
@@ -61,19 +87,17 @@ module Nom::XML
61
87
  end
62
88
  end
63
89
 
64
- def key? term
65
- terms.key? term
66
- end
67
-
68
- def flatten
69
- [self, terms.map { |k,v| v.flatten }].flatten
90
+ protected
91
+ def in_edit_context &block
92
+ @edit_context = true
93
+ yield
94
+ @edit_context = false
70
95
  end
71
96
 
72
- def terminology
73
- @terminology ||= parent.terminology
97
+ def in_edit_context?
98
+ @edit_context
74
99
  end
75
100
 
76
- protected
77
101
  def add_term method, options = {}, *args, &block
78
102
  terms[method] = Term.new(self, method, options, *args, &block)
79
103
  end
@@ -82,6 +106,16 @@ module Nom::XML
82
106
  terms[method]
83
107
  end
84
108
 
109
+ ##
110
+ # Get the XPath to the parent nodes for this term
111
+ def parent_xpath
112
+ @parent_xpath ||= self.parent.xpath
113
+ end
114
+
115
+ def xmlns
116
+ (options[:xmlns] if options) || (self.parent.xmlns if self.parent)
117
+ end
118
+
85
119
  end
86
120
 
87
121
  end
@@ -1,5 +1,5 @@
1
1
  module Nom
2
2
  module XML
3
- VERSION = '0.0.6'
3
+ VERSION = '0.0.7'
4
4
  end
5
5
  end
@@ -93,6 +93,24 @@ describe "Nutrition" do
93
93
 
94
94
  subject.asdf
95
95
  end
96
+
97
+ it "should raise an error on unknown accessors" do
98
+ mock_term = mock(:local_xpath => '//asdf', :options => {:accessor => 123 })
99
+ @term_accessors = { :asdf => mock_term }
100
+
101
+ subject.should_receive(:xpath).with('//asdf', anything)
102
+
103
+ expect { subject.asdf }.to raise_error
104
+ end
105
+
106
+ it "should convert single-valued objects to single values" do
107
+ mock_term = mock(:local_xpath => '//asdf', :options => {:single => true })
108
+ @term_accessors = { :asdf => mock_term }
109
+
110
+ subject.should_receive(:xpath).with('//asdf', anything).and_return([1])
111
+
112
+ subject.asdf.should == 1
113
+ end
96
114
  end
97
115
 
98
116
  describe "#terms" do
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe Nom::XML::Terminology do
4
+ describe "#namespaces" do
5
+ it "should pull the namespaces out of the terminology options" do
6
+ Nom::XML::Terminology.new(nil, :namespaces => { 'asd' => '123'}).namespaces.should == { 'asd' => '123'}
7
+ end
8
+
9
+ it "should return an empty hash if no namespace is provided" do
10
+ Nom::XML::Terminology.new.namespaces.should == {}
11
+ end
12
+ end
13
+
14
+ describe "#terminology" do
15
+ it "should be an identity function" do
16
+ a = Nom::XML::Terminology.new
17
+
18
+ a.terminology.should == a
19
+ end
20
+ end
21
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nom-xml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-05 00:00:00.000000000 Z
12
+ date: 2012-11-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -174,7 +174,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
174
174
  version: '0'
175
175
  segments:
176
176
  - 0
177
- hash: -3784330826546141673
177
+ hash: 2351930484694908071
178
178
  required_rubygems_version: !ruby/object:Gem::Requirement
179
179
  none: false
180
180
  requirements:
@@ -183,7 +183,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
183
183
  version: '0'
184
184
  segments:
185
185
  - 0
186
- hash: -3784330826546141673
186
+ hash: 2351930484694908071
187
187
  requirements: []
188
188
  rubyforge_project:
189
189
  rubygems_version: 1.8.23