nom-xml 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,15 +4,44 @@ module Nom::XML::Decorators::Terminology
4
4
  klass.add_terminology_methods!
5
5
  end
6
6
 
7
+ def add_terminology_methods!
8
+ document.add_terminology_methods(self)
9
+ end
10
+
7
11
  def terms
8
- @terms ||= []
12
+ if self == self.document.root or self.is_a? Nokogiri::XML::Document
13
+ root_terms
14
+ elsif not self.parent.terms.empty?
15
+ child_terms
16
+ else
17
+ []
18
+ end
9
19
  end
10
20
 
11
- def terms= terms
12
- @terms = terms
21
+ private
22
+
23
+ def root_terms
24
+ self.document.terminology.terms
13
25
  end
14
26
 
15
- def add_terminology_methods!
16
- document.add_terminology_methods(self)
27
+ def child_terms
28
+ h = {}
29
+
30
+ collect_terms_for_node.each do |k,v|
31
+ v.terms.each do |k1, v1|
32
+ h[k1] = v1
33
+ end
34
+ end
35
+
36
+ h
17
37
  end
38
+
39
+ ##
40
+ #find this self in the terminology
41
+ def collect_terms_for_node
42
+ self.parent.terms.select do |k,term|
43
+ self.parent.xpath(term.local_xpath).include? self
44
+ end
45
+ end
46
+
18
47
  end
@@ -1,3 +1,5 @@
1
+ require 'active_support/core_ext/array'
2
+
1
3
  module Nom::XML
2
4
  module NokogiriExtension
3
5
 
@@ -27,19 +29,24 @@ module Nom::XML
27
29
  # Add terminology accessors for querying child terms
28
30
  # @param [Nokogiri::XML::Node] node
29
31
  def add_terminology_methods node
30
- if node == node.document.root or node.is_a? Nokogiri::XML::Document
31
- node.terms = root_terms(node)
32
- elsif not node.parent.terms.empty?
33
- node.terms = child_terms(node)
34
- end
35
32
 
36
33
  node.terms.each do |k, t|
37
- (class << node; self; end).send(:define_method, k.to_sym) do
38
- if self.is_a? Nokogiri::XML::Document
39
- result = self.root.xpath(t.local_xpath)
40
- else
41
- result = self.xpath(t.local_xpath)
42
- end
34
+ (class << node; self; end).send(:define_method, k.to_sym) do |*args|
35
+ options = args.extract_options!
36
+
37
+ args += options.map { |key, value| %{#{key}="#{value.gsub(/"/, '\\\"') }"} }
38
+
39
+ xpath = t.local_xpath
40
+
41
+ xpath += "[#{args.join('][')}]" unless args.empty?
42
+
43
+ result = case self
44
+ when Nokogiri::XML::Document
45
+ self.root.xpath(xpath)
46
+ else
47
+ result = self.xpath(xpath)
48
+ end
49
+
43
50
  m = t.options[:accessor]
44
51
  case
45
52
  when m.nil?
@@ -54,31 +61,5 @@ module Nom::XML
54
61
  end
55
62
  end
56
63
  end
57
-
58
- private
59
-
60
- def root_terms node
61
- node.document.terminology.terms
62
- end
63
-
64
- def child_terms node
65
- h = {}
66
-
67
- terms_for_node(node).each do |k,v|
68
- v.terms.each do |k1, v1|
69
- h[k1] = v1
70
- end
71
- end
72
-
73
- h
74
- end
75
-
76
- ##
77
- #find this node in the terminology
78
- def terms_for_node node
79
- node.parent.terms.select do |k,term|
80
- node.parent.xpath(term.local_xpath).include? node
81
- end
82
- end
83
64
  end
84
65
  end
@@ -1,5 +1,5 @@
1
1
  module Nom
2
2
  module XML
3
- VERSION = '0.0.1'
3
+ VERSION = '0.0.2'
4
4
  end
5
5
  end
data/nom.gemspec CHANGED
@@ -12,6 +12,7 @@ Gem::Specification.new do |s|
12
12
  s.summary = %q{ asdf }
13
13
  s.description = %q{ asdfgh }
14
14
 
15
+ s.add_dependency 'activesupport'
15
16
  s.add_dependency 'nokogiri'
16
17
 
17
18
  s.add_development_dependency "rspec"
@@ -10,6 +10,7 @@ describe "Nutrition" do
10
10
  dv.total_fat :path => 'total-fat' do |tf|
11
11
  tf.value :path => '.', :accessor => lambda { |node| node.text.to_i }
12
12
  tf.units :path => '@units'
13
+ tf.display_value :path => '.', :accessor => lambda { |node| "#{node.value.first}#{node.units}" }
13
14
  end
14
15
  end
15
16
 
@@ -28,10 +29,15 @@ describe "Nutrition" do
28
29
  xml
29
30
  }
30
31
 
32
+ it "should be able to access things via xpath, and then continue with terminology selectors" do
33
+ subject.xpath('//food').first._name.text.should == "Avocado Dip"
34
+ end
35
+
31
36
  it "should have total fat information" do
32
37
  subject.daily_values.total_fat.text.should == "65"
33
38
  subject.daily_values.total_fat.value.should include(65)
34
39
  subject.daily_values.total_fat.units.text.should =='g'
40
+ subject.daily_values.total_fat.display_value.should include('65g')
35
41
 
36
42
  subject.foods.total_fat.value.inject(:+).should == 117
37
43
  end
@@ -39,4 +45,9 @@ describe "Nutrition" do
39
45
  it "should have food names" do
40
46
  subject.foods._name.text.should include("Avocado Dip")
41
47
  end
48
+
49
+ it "should have xpath selectors" do
50
+ subject.foods(:name => 'Avocado Dip').total_fat.value.first.should == 11
51
+ subject.foods('total-fat/text() = 11')._name.text.should == "Avocado Dip"
52
+ end
42
53
  end
data/spec/test_spec.rb CHANGED
@@ -68,13 +68,7 @@ describe Nom do
68
68
  # </level2>
69
69
  # </level1>
70
70
 
71
- it "Should be able to query for all the 'level3' nodes (leafs with different parents)"
72
- it "Should be able to query for all the 'level3' nodes that are children of parent1 (leafs with same parent)"
73
- it "Should be able to query for the nth child of a specific node"
74
- it "Should be able to update the inner text of a specific target node"
75
71
  it "Should be able to update the inner texts of a bunch of nodes????? (e.g. find('level2 > level3').innerText=['one', 'two', 'three']"
76
72
  it "Should be able to add a subtree at a specific point in the graph"
77
- it "Should be able to remove a subtree from the graph"
78
- it "should be able to set the text content of a node to nil"
79
73
 
80
74
  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.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,6 +11,22 @@ bindir: bin
11
11
  cert_chain: []
12
12
  date: 2012-09-13 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
14
30
  - !ruby/object:Gem::Dependency
15
31
  name: nokogiri
16
32
  requirement: !ruby/object:Gem::Requirement
@@ -134,7 +150,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
134
150
  version: '0'
135
151
  segments:
136
152
  - 0
137
- hash: 1208570413108466077
153
+ hash: 880844590018492615
138
154
  required_rubygems_version: !ruby/object:Gem::Requirement
139
155
  none: false
140
156
  requirements:
@@ -143,7 +159,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
143
159
  version: '0'
144
160
  segments:
145
161
  - 0
146
- hash: 1208570413108466077
162
+ hash: 880844590018492615
147
163
  requirements: []
148
164
  rubyforge_project:
149
165
  rubygems_version: 1.8.23