nom-xml 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,24 +1,22 @@
1
1
  module Nom::XML::Decorators::Terminology
2
-
3
- METHODS_WE_NEED_TO_BE_CAREFUL_OF = [:id, :type]
4
-
5
- METHODS_WE_NEED_TO_BE_CAREFUL_OF.each do |t|
6
- class_eval <<-eos
7
- def #{t.to_s} *args, &block
8
- if self.term_accessors[:#{t}]
9
- lookup_term(self.term_accessors[:#{t}], *args, &block)
10
- else
11
- super
12
- end
2
+
3
+ def self.extended node
4
+ node.add_terminology_method_overrides!
5
+ end
6
+
7
+ def add_terminology_method_overrides!
8
+ self.term_accessors.each do |k, term|
9
+ if self.respond_to_without_terms? k and not term.options[:override]
10
+ raise "Trying to redefine method #{k} on #{self.to_s}"
13
11
  end
14
- eos
12
+ end.select { |k, term| term.options[:override] }.each do |method, term|
13
+ define_term_method(method, self.term_accessors[method.to_sym])
14
+ end
15
15
  end
16
16
 
17
17
  def method_missing method, *args, &block
18
18
  if self.term_accessors[method.to_sym]
19
- (class << self; self; end).send(:define_method, method.to_sym) do |*local_args|
20
- lookup_term(self.term_accessors[method.to_sym], *local_args)
21
- end
19
+ define_term_method(method, self.term_accessors[method.to_sym])
22
20
 
23
21
  self.send(method, *args, &block)
24
22
  else
@@ -26,6 +24,8 @@ module Nom::XML::Decorators::Terminology
26
24
  end
27
25
  end
28
26
 
27
+ alias_method :respond_to_without_terms?, :respond_to?
28
+
29
29
  def respond_to? method
30
30
  super || self.term_accessors[method.to_sym]
31
31
  end
@@ -81,6 +81,12 @@ module Nom::XML::Decorators::Terminology
81
81
  h
82
82
  end
83
83
 
84
+ def define_term_method method, term
85
+ (class << self; self; end).send(:define_method, method.to_sym) do |*local_args|
86
+ lookup_term(self.term_accessors[method.to_sym], *local_args)
87
+ end
88
+ end
89
+
84
90
  def lookup_term term, *args
85
91
  options = args.extract_options!
86
92
 
@@ -116,7 +122,7 @@ module Nom::XML::Decorators::Terminology
116
122
  raise "Unknown accessor class: #{m.class}"
117
123
  end
118
124
 
119
- if return_value and (term.options[:single] or (return_value.length == 1 and return_value.first.is_a? Nokogiri::XML::Attr))
125
+ if return_value and (term.options[:single] or (result.length == 1 and result.first.is_a? Nokogiri::XML::Attr))
120
126
  return return_value.first
121
127
  end
122
128
 
@@ -1,5 +1,5 @@
1
1
  module Nom
2
2
  module XML
3
- VERSION = '0.2.0'
3
+ VERSION = '0.3.0'
4
4
  end
5
5
  end
@@ -9,7 +9,11 @@ describe "Namespaces example" do
9
9
 
10
10
  xml.set_terminology(:namespaces => { 'mods' => 'http://www.loc.gov/mods/v3'}) do |t|
11
11
 
12
+ t.genre :path => "mods:genre", :accessor => lambda { |e| e.text }
13
+
12
14
  t.author :path => '//mods:name' do |n|
15
+ n.authorityURI :path => '@authorityURI', :accessor => lambda { |e| e.text }
16
+ n.description :path => 'mods:description', :override => true
13
17
  n.valueURI :path => '@valueURI'
14
18
  n.namePart :path => 'mods:namePart', :single => true, :index_as => [:type_1]
15
19
  end
@@ -35,6 +39,10 @@ describe "Namespaces example" do
35
39
  xml
36
40
  }
37
41
 
42
+ it "should work with existing reserved method names when override is present" do
43
+ subject.author.first.description.text.should include('asdf')
44
+ end
45
+
38
46
  it "should return nodesets by default" do
39
47
  subject.personal_authors.should be_a_kind_of(Nokogiri::XML::NodeSet)
40
48
  end
@@ -47,6 +55,11 @@ describe "Namespaces example" do
47
55
  subject.personal_authors.first.valueURI.should be_a_kind_of(Nokogiri::XML::Attr)
48
56
  end
49
57
 
58
+
59
+ it "should treat attributes with an accessor as a single-able element" do
60
+ subject.author.first.authorityURI.should == 'http://id.loc.gov/authorities/names'
61
+ end
62
+
50
63
  it "should share terms over matched nodes" do
51
64
  subject.personal_authors.first.namePart.text.should == "Alterman, Eric"
52
65
  end
@@ -89,7 +102,7 @@ describe "Namespaces example" do
89
102
  end
90
103
 
91
104
  it "should let you go from a terminology to nodes" do
92
- subject.terminology.flatten.length.should == 11
105
+ subject.terminology.flatten.length.should == 14
93
106
 
94
107
  subject.terminology.flatten.select { |x| x.options[:index_as] }.should have(2).terms
95
108
  subject.terminology.flatten.select { |x| x.options[:index_as] }.map { |x| x.nodes }.flatten.should have(2).nodes
@@ -11,6 +11,7 @@
11
11
  <name type="personal" authorityURI="http://id.loc.gov/authorities/names"
12
12
  valueURI="http://id.loc.gov/authorities/names/n92101908">
13
13
  <namePart>Alterman, Eric</namePart>
14
+ <description>asdf</description>
14
15
  <role>
15
16
  <roleTerm type="text">creator</roleTerm>
16
17
  </role>
@@ -44,7 +44,7 @@ describe "Nutrition" do
44
44
  end
45
45
 
46
46
  it "should define terminology accessors" do
47
- mock_term = mock
47
+ mock_term = mock(:options => {})
48
48
  @term_accessors = { :asdf => mock_term }
49
49
 
50
50
  subject.should respond_to(:asdf)
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.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -159,7 +159,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
159
159
  version: '0'
160
160
  segments:
161
161
  - 0
162
- hash: -597609388430309558
162
+ hash: -2552568584722504681
163
163
  required_rubygems_version: !ruby/object:Gem::Requirement
164
164
  none: false
165
165
  requirements:
@@ -168,7 +168,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
168
168
  version: '0'
169
169
  segments:
170
170
  - 0
171
- hash: -597609388430309558
171
+ hash: -2552568584722504681
172
172
  requirements: []
173
173
  rubyforge_project:
174
174
  rubygems_version: 1.8.23