nom-xml 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -40,7 +40,7 @@ module Nom::XML::Decorators::Terminology
40
40
  #find this self in the terminology
41
41
  def collect_terms_for_node
42
42
  self.parent.terms.select do |k,term|
43
- self.parent.xpath(term.local_xpath).include? self
43
+ self.parent.xpath(term.local_xpath, self.document.terminology_namespaces).include? self
44
44
  end
45
45
  end
46
46
 
@@ -17,10 +17,15 @@ module Nom::XML
17
17
  self
18
18
  end
19
19
 
20
- def set_terminology &block
20
+ def set_terminology options = {}, &block
21
+ @terminology_namespaces = options[:namespaces]
21
22
  @terminology = Nom::XML::Terminology.new &block
22
23
  end
23
24
 
25
+ def terminology_namespaces
26
+ @terminology_namespaces ||= {}
27
+ end
28
+
24
29
  def terminology
25
30
  @terminology ||= Nom::XML::Terminology.new
26
31
  end
@@ -29,8 +34,12 @@ module Nom::XML
29
34
  # Add terminology accessors for querying child terms
30
35
  # @param [Nokogiri::XML::Node] node
31
36
  def add_terminology_methods node
37
+ terms_to_add = node.terms
38
+ node.ancestors.each do |a|
39
+ a.terms.each { |k,t| terms_to_add[k] ||= t if t.options[:global] }
40
+ end
32
41
 
33
- node.terms.each do |k, t|
42
+ terms_to_add.each do |k, t|
34
43
  (class << node; self; end).send(:define_method, k.to_sym) do |*args|
35
44
  options = args.extract_options!
36
45
 
@@ -38,13 +47,14 @@ module Nom::XML
38
47
 
39
48
  xpath = t.local_xpath
40
49
 
50
+
41
51
  xpath += "[#{args.join('][')}]" unless args.empty?
42
52
 
43
53
  result = case self
44
54
  when Nokogiri::XML::Document
45
- self.root.xpath(xpath)
55
+ self.root.xpath(xpath, node.document.terminology_namespaces)
46
56
  else
47
- result = self.xpath(xpath)
57
+ self.xpath(xpath, node.document.terminology_namespaces)
48
58
  end
49
59
 
50
60
  m = t.options[:accessor]
@@ -46,7 +46,11 @@ module Nom::XML
46
46
  end
47
47
 
48
48
  def local_xpath
49
- (options[:path] || name).to_s
49
+ ("#{xmlns}:" unless xmlns.blank? ).to_s + (options[:path] || name).to_s
50
+ end
51
+
52
+ def xmlns
53
+ (options[:xmlns] if options) || (self.parent.xmlns if self.parent)
50
54
  end
51
55
 
52
56
  def method_missing method, *args, &block
@@ -77,11 +81,7 @@ module Nom::XML
77
81
 
78
82
  protected
79
83
  def add_term method, options = {}, *args, &block
80
- terms[method] = if options[:ref]
81
- TermRef.new(self, method, options, *args, &block)
82
- else
83
- Term.new(self, method, options, *args, &block)
84
- end
84
+ terms[method] = Term.new(self, method, options, *args, &block)
85
85
  end
86
86
 
87
87
  def term method, *args, &block
@@ -89,30 +89,6 @@ module Nom::XML
89
89
  end
90
90
  end
91
91
 
92
- class TermRef < Term
93
- def ref
94
- elements = Array(options[:ref])
95
-
96
- elements.inject(parent) { |memo, mtd| memo.send(mtd) }
97
- end
98
-
99
- def key? term
100
- ref.key? term
101
- end
102
-
103
- def local_xpath
104
- (options[:path] || ref.local_xpath).to_s
105
- end
106
-
107
- def method_missing *args, &block
108
- if in_edit_context?
109
- super
110
- else
111
- ref.method_missing(*args, &block).substitute_parent(self)
112
- end
113
- end
114
- end
115
-
116
92
  class Terminology < Term
117
93
  def initialize *args, &block
118
94
  @terms = {}
@@ -1,5 +1,5 @@
1
1
  module Nom
2
2
  module XML
3
- VERSION = '0.0.2'
3
+ VERSION = '0.0.3'
4
4
  end
5
5
  end
data/nom.gemspec CHANGED
@@ -13,6 +13,7 @@ Gem::Specification.new do |s|
13
13
  s.description = %q{ asdfgh }
14
14
 
15
15
  s.add_dependency 'activesupport'
16
+ s.add_dependency 'i18n'
16
17
  s.add_dependency 'nokogiri'
17
18
 
18
19
  s.add_development_dependency "rspec"
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Namespaces example" do
4
+ let(:file) { File.open(File.join(File.dirname(__FILE__), '..', 'fixtures', 'xml_namespaces.xml'), 'r') }
5
+ let(:xml) { Nokogiri::XML(file) }
6
+
7
+ subject {
8
+ xml.set_terminology(:namespaces => { 'html4' => 'http://www.w3.org/TR/html4/', 'furniture' => "http://www.w3schools.com/furniture"}) do |t|
9
+ t.table :xmlns => 'html4' do |t|
10
+ t.tr do |tr|
11
+ tr.td
12
+ end
13
+ end
14
+
15
+ t.furniture_table :path => 'table', :xmlns => 'furniture' do |f|
16
+ f._name :path => 'name'
17
+ f.width
18
+ f.length
19
+ end
20
+ end
21
+
22
+ xml.nom!
23
+
24
+ xml
25
+ }
26
+
27
+ it "should get nodes" do
28
+ subject.table.tr.td.map { |x| x.text }.should include("Apples", "Bananas")
29
+ end
30
+
31
+ it "should get nodes from the other namespace" do
32
+ subject.furniture_table._name.text.should include("African Coffee Table")
33
+ end
34
+ end
@@ -6,11 +6,12 @@ describe "Nutrition" do
6
6
 
7
7
  subject {
8
8
  xml.set_terminology do |t|
9
+ t.unit_value :path => '.', :accessor => lambda { |node| "#{node.text}#{node['units']}" }, :global => true
10
+
9
11
  t.daily_values :path => 'daily-values' do |dv|
10
12
  dv.total_fat :path => 'total-fat' do |tf|
11
13
  tf.value :path => '.', :accessor => lambda { |node| node.text.to_i }
12
14
  tf.units :path => '@units'
13
- tf.display_value :path => '.', :accessor => lambda { |node| "#{node.value.first}#{node.units}" }
14
15
  end
15
16
  end
16
17
 
@@ -37,7 +38,6 @@ describe "Nutrition" do
37
38
  subject.daily_values.total_fat.text.should == "65"
38
39
  subject.daily_values.total_fat.value.should include(65)
39
40
  subject.daily_values.total_fat.units.text.should =='g'
40
- subject.daily_values.total_fat.display_value.should include('65g')
41
41
 
42
42
  subject.foods.total_fat.value.inject(:+).should == 117
43
43
  end
@@ -50,4 +50,9 @@ describe "Nutrition" do
50
50
  subject.foods(:name => 'Avocado Dip').total_fat.value.first.should == 11
51
51
  subject.foods('total-fat/text() = 11')._name.text.should == "Avocado Dip"
52
52
  end
53
+
54
+ it "should have global terms" do
55
+ subject.daily_values.total_fat.unit_value.should include('65g')
56
+ subject.foods.first.at('serving').unit_value.should include('29g')
57
+ end
53
58
  end
@@ -0,0 +1,17 @@
1
+ <root xmlns:h="http://www.w3.org/TR/html4/"
2
+ xmlns:f="http://www.w3schools.com/furniture">
3
+
4
+ <h:table>
5
+ <h:tr>
6
+ <h:td>Apples</h:td>
7
+ <h:td>Bananas</h:td>
8
+ </h:tr>
9
+ </h:table>
10
+
11
+ <f:table>
12
+ <f:name>African Coffee Table</f:name>
13
+ <f:width>80</f:width>
14
+ <f:length>120</f:length>
15
+ </f:table>
16
+
17
+ </root>
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.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -27,6 +27,22 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: i18n
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
30
46
  - !ruby/object:Gem::Dependency
31
47
  name: nokogiri
32
48
  requirement: !ruby/object:Gem::Requirement
@@ -128,8 +144,10 @@ files:
128
144
  - lib/nom/xml/terminology.rb
129
145
  - lib/nom/xml/version.rb
130
146
  - nom.gemspec
147
+ - spec/examples/namespaced_example_spec.rb
131
148
  - spec/examples/nutrition_example_spec.rb
132
149
  - spec/fixtures/nutrition.xml
150
+ - spec/fixtures/xml_namespaces.xml
133
151
  - spec/lib/nodeset_decorator_spec.rb
134
152
  - spec/lib/nokogiri_extension_spec.rb
135
153
  - spec/lib/terminology_decorator_spec.rb
@@ -150,7 +168,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
150
168
  version: '0'
151
169
  segments:
152
170
  - 0
153
- hash: 880844590018492615
171
+ hash: 3518149504485723530
154
172
  required_rubygems_version: !ruby/object:Gem::Requirement
155
173
  none: false
156
174
  requirements:
@@ -159,7 +177,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
159
177
  version: '0'
160
178
  segments:
161
179
  - 0
162
- hash: 880844590018492615
180
+ hash: 3518149504485723530
163
181
  requirements: []
164
182
  rubyforge_project:
165
183
  rubygems_version: 1.8.23
@@ -167,8 +185,10 @@ signing_key:
167
185
  specification_version: 3
168
186
  summary: asdf
169
187
  test_files:
188
+ - spec/examples/namespaced_example_spec.rb
170
189
  - spec/examples/nutrition_example_spec.rb
171
190
  - spec/fixtures/nutrition.xml
191
+ - spec/fixtures/xml_namespaces.xml
172
192
  - spec/lib/nodeset_decorator_spec.rb
173
193
  - spec/lib/nokogiri_extension_spec.rb
174
194
  - spec/lib/terminology_decorator_spec.rb