From c15ff33f9906395524c57e6d7b5ae0b45d849e5f Mon Sep 17 00:00:00 2001 From: Matt Zumwalt Date: Tue, 22 Jun 2010 15:48:03 -0500 Subject: [PATCH] nokogiri datastream integration --- History.txt | 6 +++++ lib/active_fedora/base.rb | 5 ++++- .../metadata_datastream_helper.rb | 16 -------------- lib/active_fedora/nokogiri_datastream.rb | 22 +++++++++++++++++++ lib/hydra.rb | 4 ++-- lib/{hydra_libs => hydra}/mods_article.rb | 2 ++ .../opinionated_mods_document.rb | 0 spec/integration/full_featured_model_spec.rb | 7 ++++++ 8 files changed, 43 insertions(+), 19 deletions(-) rename lib/{hydra_libs => hydra}/mods_article.rb (99%) rename lib/{hydra_libs => hydra}/opinionated_mods_document.rb (100%) diff --git a/History.txt b/History.txt index ce0905fb5..40982a256 100644 --- a/History.txt +++ b/History.txt @@ -1,3 +1,9 @@ +1.1.7 + +Nokogiri Datastream support +You now have the option of calling has_metadata without passing in a block + + 1.1.6 Bug #959: Base.delete fails to delete objects from Solr even when ENABLE_SOLR_UPDATES == true diff --git a/lib/active_fedora/base.rb b/lib/active_fedora/base.rb index 571376589..40f68a7d5 100644 --- a/lib/active_fedora/base.rb +++ b/lib/active_fedora/base.rb @@ -529,7 +529,10 @@ def configure_defined_datastreams attributes = {:label=>""} end ds = ar.first.new(:dsid=>name) - ar.last.call(ds) + # If you called has_metadata with a block, pass the block into the Datastream class + if ar.last.class == Proc + ar.last.call(ds) + end ds.attributes = attributes.merge(ds.attributes) self.add_datastream(ds) end diff --git a/lib/active_fedora/metadata_datastream_helper.rb b/lib/active_fedora/metadata_datastream_helper.rb index 5874c9cd1..73eebda82 100644 --- a/lib/active_fedora/metadata_datastream_helper.rb +++ b/lib/active_fedora/metadata_datastream_helper.rb @@ -65,22 +65,6 @@ def to_xml(xml = Nokogiri::XML::Document.parse("")) #:nodoc: end end end - - # fields.each_pair do |field,field_info| - # el = REXML::Element.new("#{field.to_s}") - # if field_info[:element_attrs] - # field_info[:element_attrs].each{|k,v| el.add_attribute(k.to_s, v.to_s)} - # end - # field_info[:values].each do |val| - # el = el.clone - # el.text = val.to_s - # if xml.class == REXML::Document - # xml.root.elements.add(el) - # else - # xml.add(el) - # end - # end - # end return builder.to_xml end diff --git a/lib/active_fedora/nokogiri_datastream.rb b/lib/active_fedora/nokogiri_datastream.rb index e73d12bb9..4fb7fbeb7 100644 --- a/lib/active_fedora/nokogiri_datastream.rb +++ b/lib/active_fedora/nokogiri_datastream.rb @@ -15,6 +15,28 @@ def initialize(attrs=nil) self.class.from_xml(blob, self) end + def to_xml(xml = self.ng_xml) + ng_xml = self.ng_xml + if ng_xml.root.nil? && self.class.respond_to?(:root_property_ref) && !self.class.root_property_ref.nil? + ng_xml = self.class.generate(self.class.root_property_ref, "") + if xml.root.nil? + xml = ng_xml + end + end + + unless xml == ng_xml || ng_xml.root.nil? + if xml.kind_of?(Nokogiri::XML::Document) + xml.root.add_child(ng_xml.root) + elsif xml.kind_of?(Nokogiri::XML::Node) + xml.add_child(ng_xml.root) + else + raise "You can only pass instances of Nokogiri::XML::Node into this method. You passed in #{xml}" + end + end + + return xml.to_xml {|config| config.no_declaration} + end + def to_solr(solr_doc = Solr::Document.new) # :nodoc: unless self.class.accessors.nil? diff --git a/lib/hydra.rb b/lib/hydra.rb index 2f7e3950f..45d27624a 100644 --- a/lib/hydra.rb +++ b/lib/hydra.rb @@ -1,2 +1,2 @@ -# module Hydra;end -require "hydra_libs/mods_datastream" \ No newline at end of file +module Hydra;end +require "hydra/mods_article" \ No newline at end of file diff --git a/lib/hydra_libs/mods_article.rb b/lib/hydra/mods_article.rb similarity index 99% rename from lib/hydra_libs/mods_article.rb rename to lib/hydra/mods_article.rb index 3153ffb60..c52333ddc 100644 --- a/lib/hydra_libs/mods_article.rb +++ b/lib/hydra/mods_article.rb @@ -1,3 +1,4 @@ +module Hydra class ModsArticle < ActiveFedora::NokogiriDatastream # have to call this in order to set namespace & schema @@ -44,4 +45,5 @@ class ModsArticle < ActiveFedora::NokogiriDatastream ]}} ] +end end \ No newline at end of file diff --git a/lib/hydra_libs/opinionated_mods_document.rb b/lib/hydra/opinionated_mods_document.rb similarity index 100% rename from lib/hydra_libs/opinionated_mods_document.rb rename to lib/hydra/opinionated_mods_document.rb diff --git a/spec/integration/full_featured_model_spec.rb b/spec/integration/full_featured_model_spec.rb index b0d04cd39..4e1cf6fff 100644 --- a/spec/integration/full_featured_model_spec.rb +++ b/spec/integration/full_featured_model_spec.rb @@ -1,6 +1,7 @@ require File.join( File.dirname(__FILE__), "../spec_helper" ) require 'rexml/document' +require "hydra" include ActiveFedora @@ -30,6 +31,12 @@ class OralHistory < ActiveFedora::Base m.field "location", :string end + # has_metadata :name=>"arbitrary_xml", :type=> ActiveFedora::NokogiriDatastream do |m| + # m.root_property :myxml, "my_xml", "http://www.example.gov/schema/v3" + # end + + has_metadata :name=>"mods_article", :type=> Hydra::ModsArticle + has_metadata :name => "dublin_core", :type => ActiveFedora::QualifiedDublinCoreDatastream do |m| # Default :multiple => true #