om 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/History.textile CHANGED
@@ -1,3 +1,9 @@
1
+ 0.1.5
2
+
3
+ * root_property now inserts an entry into the properties hash
4
+ * added .generate method for building new instances of declared properties
5
+ * refinements to accessor_xpath
6
+
1
7
  0.1.4
2
8
 
3
9
  * made attribute_xpath idempotent
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.4
1
+ 0.1.5
@@ -83,7 +83,11 @@ module OM::XML::Accessors
83
83
  # key_index = keys.index(k)
84
84
  pointer_index = pointers.index(pointer)
85
85
  # accessor_info = accessor_info(*keys[0..key_index])
86
- accessor_info = accessor_info(*keys)
86
+ accessor_info = accessor_info(*keys)
87
+
88
+ # Return nil if there is no accessor info to work with
89
+ if accessor_info.nil? then return nil end
90
+
87
91
  relative_path = accessor_info[:relative_xpath]
88
92
 
89
93
  if relative_path.kind_of?(Hash)
@@ -0,0 +1,26 @@
1
+ module OM::XML::Generator
2
+
3
+ attr_accessor :ng_xml
4
+
5
+ # Class Methods -- These methods will be available on classes that include this Module
6
+
7
+ module ClassMethods
8
+
9
+ def generate(property_ref, builder_new_value, opts={})
10
+ template = builder_template(property_ref, opts)
11
+ builder_call_body = eval('"' + template + '"')
12
+ builder = Nokogiri::XML::Builder.new do |xml|
13
+ eval( builder_call_body )
14
+ end
15
+
16
+ return builder.doc
17
+ end
18
+
19
+ end
20
+
21
+ # Instance Methods -- These methods will be available on instances of classes that include this module
22
+
23
+ def self.included(klass)
24
+ klass.extend(ClassMethods)
25
+ end
26
+ end
@@ -9,7 +9,7 @@ module OM::XML::Properties
9
9
  attr_reader :properties
10
10
 
11
11
  def root_property( property_ref, path, namespace, opts={})
12
- # property property_ref, path, opts.merge({:path=>path, :ref=>property_ref})
12
+ property property_ref, opts.merge({:path=>path, :ref=>property_ref})
13
13
  @root_config = opts.merge({:namespace=>namespace, :path=>path, :ref=>property_ref})
14
14
  @root_property_ref = property_ref
15
15
  @ox_namespaces = {'oxns'=>root_config[:namespace]}
@@ -69,7 +69,7 @@ module OM::XML::Properties
69
69
  end
70
70
  end
71
71
 
72
- if properties[:unresolved].has_key?(prop_hash[:ref])
72
+ if properties.fetch(:unresolved, {}).has_key?(prop_hash[:ref])
73
73
  ref = prop_hash[:ref]
74
74
  properties[:unresolved][ref].each do |parent_prop_hash|
75
75
  logger.debug "Resolving #{ref} subelement for #{parent_prop_hash[:ref]} property"
@@ -276,6 +276,8 @@ module OM::XML::Properties
276
276
  # Builder Support
277
277
  #
278
278
 
279
+ # @property_ref reference to the property you want to generate a builder template for
280
+ # @opts
279
281
  def builder_template(property_ref, opts={})
280
282
  property_info = property_info_for(property_ref)
281
283
 
data/lib/om/xml.rb CHANGED
@@ -3,6 +3,7 @@ require "om/xml/accessors"
3
3
  require "om/xml/validation"
4
4
  require "om/xml/properties"
5
5
  require "om/xml/property_value_operators"
6
+ require "om/xml/generator"
6
7
 
7
8
  module OM::XML
8
9
 
@@ -16,6 +17,7 @@ module OM::XML
16
17
  klass.send(:include, OM::XML::Validation)
17
18
  klass.send(:include, OM::XML::Properties)
18
19
  klass.send(:include, OM::XML::PropertyValueOperators)
20
+ klass.send(:include, OM::XML::Generator)
19
21
 
20
22
  # klass.send(:include, OM::XML::Schema)
21
23
  end
data/om.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{om}
8
- s.version = "0.1.4"
8
+ s.version = "0.1.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Matt Zumwalt"]
12
- s.date = %q{2010-06-21}
12
+ s.date = %q{2010-06-23}
13
13
  s.description = %q{OM (Opinionated Metadata): A library to help you tame sprawling XML schemas like MODS. Wraps Nokogiri documents in objects with miscellaneous helper methods for doing things like retrieve generated xpath queries or look up properties based on a simplified DSL}
14
14
  s.email = %q{matt.zumwalt@yourmediashelf.com}
15
15
  s.extra_rdoc_files = [
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
28
28
  "lib/om/xml.rb",
29
29
  "lib/om/xml/accessors.rb",
30
30
  "lib/om/xml/container.rb",
31
+ "lib/om/xml/generator.rb",
31
32
  "lib/om/xml/properties.rb",
32
33
  "lib/om/xml/property_value_operators.rb",
33
34
  "lib/om/xml/validation.rb",
@@ -41,6 +42,7 @@ Gem::Specification.new do |s|
41
42
  "spec/spec_helper.rb",
42
43
  "spec/unit/accessors_spec.rb",
43
44
  "spec/unit/container_spec.rb",
45
+ "spec/unit/generator_spec.rb",
44
46
  "spec/unit/properties_spec.rb",
45
47
  "spec/unit/property_value_operators_spec.rb",
46
48
  "spec/unit/validation_spec.rb",
@@ -55,6 +57,7 @@ Gem::Specification.new do |s|
55
57
  "spec/spec_helper.rb",
56
58
  "spec/unit/accessors_spec.rb",
57
59
  "spec/unit/container_spec.rb",
60
+ "spec/unit/generator_spec.rb",
58
61
  "spec/unit/properties_spec.rb",
59
62
  "spec/unit/property_value_operators_spec.rb",
60
63
  "spec/unit/validation_spec.rb",
@@ -132,6 +132,9 @@ describe "OM::XML::Accessors" do
132
132
  it "should prepend the xpath for any parent nodes, inserting calls to xpath:position() function where necessary" do
133
133
  AccessorTest.accessor_xpath( {:conference=>0}, {:role=>1}, :text ).should == '//oxns:name[@type="conference" and position()=1]/oxns:role[position()=2]/oxns:roleTerm[@type="text"]'
134
134
  end
135
+ it "should return nil if no accessor_info is available" do
136
+ AccessorTest.accessor_xpath( :sample_undeclared_accessor ).should == nil
137
+ end
135
138
  it "should be idempotent" do
136
139
  AccessorTest.accessor_xpath( *[{:title_info=>2}, :main_title] ).should == "//oxns:titleInfo[position()=3]/oxns:title"
137
140
  AccessorTest.accessor_xpath( *[{:title_info=>2}, :main_title] ).should == "//oxns:titleInfo[position()=3]/oxns:title"
@@ -0,0 +1,61 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require "om"
3
+
4
+ describe "OM::XML::Generator" do
5
+
6
+ before(:all) do
7
+ #ModsHelpers.name_("Beethoven, Ludwig van", :date=>"1770-1827", :role=>"creator")
8
+ class GeneratorTest
9
+
10
+ include OM::XML::Container
11
+ include OM::XML::Properties
12
+ include OM::XML::Generator
13
+
14
+ # Could add support for multiple root declarations.
15
+ # For now, assume that any modsCollections have already been broken up and fed in as individual mods documents
16
+ # root :mods_collection, :path=>"modsCollection",
17
+ # :attributes=>[],
18
+ # :subelements => :mods
19
+
20
+ root_property :mods, "mods", "http://www.loc.gov/mods/v3", :attributes=>["id", "version"], :schema=>"http://www.loc.gov/standards/mods/v3/mods-3-2.xsd"
21
+
22
+
23
+ property :name_, :path=>"name",
24
+ :attributes=>[:xlink, :lang, "xml:lang", :script, :transliteration, {:type=>["personal", "enumerated", "corporate"]} ],
25
+ :subelements=>["namePart", "displayForm", "affiliation", :role, "description"],
26
+ :default_content_path => "namePart",
27
+ :convenience_methods => {
28
+ :date => {:path=>"namePart", :attributes=>{:type=>"date"}},
29
+ :family_name => {:path=>"namePart", :attributes=>{:type=>"family"}},
30
+ :given_name => {:path=>"namePart", :attributes=>{:type=>"given"}},
31
+ :terms_of_address => {:path=>"namePart", :attributes=>{:type=>"termsOfAddress"}}
32
+ }
33
+ property :person, :variant_of=>:name_, :attributes=>{:type=>"personal"}
34
+ property :role, :path=>"role",
35
+ :parents=>[:name_],
36
+ :attributes=>[ { "type"=>["text", "code"] } , "authority"],
37
+ :default_content_path => "roleTerm"
38
+
39
+ end
40
+
41
+ end
42
+
43
+ before(:each) do
44
+ @sample = GeneratorTest.from_xml( fixture( File.join("test_dummy_mods.xml") ) )
45
+ end
46
+
47
+ after(:all) do
48
+ Object.send(:remove_const, :GeneratorTest)
49
+ end
50
+
51
+ describe '#generate' do
52
+ it "should use the corresponding builder template(s) to generate the node" do
53
+ GeneratorTest.generate(:mods, "foo").root.to_xml.should == "<mods>foo</mods>"
54
+ GeneratorTest.generate([:person,:role], "creator", {:attributes=>{"type"=>"code", "authority"=>"marcrelator"}}).root.to_xml.should == "<role type=\"code\" authority=\"marcrelator\">\n <roleTerm>creator</roleTerm>\n</role>"
55
+ end
56
+ it "should return Nokogiri Documents" do
57
+ GeneratorTest.generate(:mods, "foo").class.should == Nokogiri::XML::Document
58
+ end
59
+ end
60
+
61
+ end
@@ -74,6 +74,10 @@ describe "OM::XML::Properties" do
74
74
  FakeOtherOx.root_property_ref.should == :other
75
75
  FakeOtherOx.root_config.should == {:namespace=>"http://www.foo.com", :path=>"other", :ref=>:other}
76
76
  end
77
+ it "should add a corresponding entry into the properties hash" do
78
+ FakeOxMods.property_info_for(FakeOxMods.root_property_ref).should == {:schema=>"http://www.loc.gov/standards/mods/v3/mods-3-2.xsd", :xpath_relative=>"oxns:mods", :path=>"mods", :xpath_constrained=>"//oxns:mods[contains(\\\"\#{constraint_value}\\\")]", :xpath=>"//oxns:mods", :ref=>:mods, :convenience_methods=>{}, :attributes=>["id", "version"]}
79
+ FakeOxMods.builder_template(FakeOxMods.root_property_ref).should == "xml.mods( '\#{builder_new_value}' )"
80
+ end
77
81
  end
78
82
 
79
83
  describe "#property" do
@@ -123,6 +127,16 @@ describe "OM::XML::Properties" do
123
127
  FakeOxMods.properties[:name_][:convenience_methods][:role][:xpath_constrained].should == '//oxns:name[contains(oxns:role/oxns:roleTerm, "#{constraint_value}")]'.gsub('"', '\"')
124
128
  end
125
129
 
130
+ it "should create an accessor for the created property" do
131
+ pending
132
+ FakeOXMods.accessors.should have_key(:name_)
133
+ end
134
+
135
+ it "should create accessors for subelements" do
136
+ pending
137
+ FakeOXMods.accessors[:name_][:children][:role][:children].should have_key(:family_name)
138
+ end
139
+
126
140
  it "should not overwrite default property info when adding a variant property" do
127
141
  FakeOxMods.properties[:name_].should_not equal(FakeOxMods.properties[:person])
128
142
  FakeOxMods.properties[:name_][:convenience_methods].should_not equal(FakeOxMods.properties[:person][:convenience_methods])
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: om
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 4
10
- version: 0.1.4
9
+ - 5
10
+ version: 0.1.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Matt Zumwalt
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-06-21 00:00:00 -05:00
18
+ date: 2010-06-23 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -113,6 +113,7 @@ files:
113
113
  - lib/om/xml.rb
114
114
  - lib/om/xml/accessors.rb
115
115
  - lib/om/xml/container.rb
116
+ - lib/om/xml/generator.rb
116
117
  - lib/om/xml/properties.rb
117
118
  - lib/om/xml/property_value_operators.rb
118
119
  - lib/om/xml/validation.rb
@@ -126,6 +127,7 @@ files:
126
127
  - spec/spec_helper.rb
127
128
  - spec/unit/accessors_spec.rb
128
129
  - spec/unit/container_spec.rb
130
+ - spec/unit/generator_spec.rb
129
131
  - spec/unit/properties_spec.rb
130
132
  - spec/unit/property_value_operators_spec.rb
131
133
  - spec/unit/validation_spec.rb
@@ -168,6 +170,7 @@ test_files:
168
170
  - spec/spec_helper.rb
169
171
  - spec/unit/accessors_spec.rb
170
172
  - spec/unit/container_spec.rb
173
+ - spec/unit/generator_spec.rb
171
174
  - spec/unit/properties_spec.rb
172
175
  - spec/unit/property_value_operators_spec.rb
173
176
  - spec/unit/validation_spec.rb