Module: ActiveFedora::MetadataDatastreamHelper

Included in:
MetadataDatastream, NokogiriDatastream
Defined in:
lib/active_fedora/metadata_datastream_helper.rb

Overview

this class represents a MetadataDatastream, a special case of ActiveFedora::Datastream

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fieldsObject

Returns the value of attribute fields.



4
5
6
# File 'lib/active_fedora/metadata_datastream_helper.rb', line 4

def fields
  @fields
end

Class Method Details

.included(klass) ⇒ Object



17
18
19
20
# File 'lib/active_fedora/metadata_datastream_helper.rb', line 17

def self.included(klass)
  klass.extend(ClassMethods)
  klass.send(:include, ActiveFedora::SolrMapper)
end

Instance Method Details

#initialize(attrs = nil) ⇒ Object

constructor, calls up to ActiveFedora::Datastream’s constructor



23
24
25
26
# File 'lib/active_fedora/metadata_datastream_helper.rb', line 23

def initialize(attrs=nil)
  super
  @fields={}
end

#saveObject

sets the blob, which in this case is the xml version of self, then calls ActiveFedora::Datastream.save



29
30
31
32
# File 'lib/active_fedora/metadata_datastream_helper.rb', line 29

def save
  self.set_blob_for_save
  super
end

#set_blob_for_saveObject

:nodoc:



34
35
36
# File 'lib/active_fedora/metadata_datastream_helper.rb', line 34

def set_blob_for_save # :nodoc:
  self.blob = self.to_xml
end

#to_solr(solr_doc = Solr::Document.new) ⇒ Object

:nodoc:



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/active_fedora/metadata_datastream_helper.rb', line 38

def to_solr(solr_doc = Solr::Document.new) # :nodoc:
  fields.each do |field_key, field_info|
    if field_info.has_key?(:values) && !field_info[:values].nil?
      field_symbol = generate_solr_symbol(field_key, field_info[:type])
      field_info[:values].each do |val|             
        solr_doc << Solr::Field.new(field_symbol => val)
      end
    end
  end

  return solr_doc
end

#to_xml(xml = Nokogiri::XML::Document.parse("<fields />")) ⇒ Object

:nodoc:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/active_fedora/metadata_datastream_helper.rb', line 51

def to_xml(xml = Nokogiri::XML::Document.parse("<fields />")) #:nodoc:
  if xml.instance_of?(Nokogiri::XML::Builder)
    xml_insertion_point = xml.doc.root 
  elsif xml.instance_of?(Nokogiri::XML::Document) 
    xml_insertion_point = xml.root
  else
    xml_insertion_point = xml
  end
  
  builder = Nokogiri::XML::Builder.with(xml_insertion_point) do |xml|
    fields.each_pair do |field,field_info|
      element_attrs = field_info[:element_attrs].nil? ? {} : field_info[:element_attrs]
      field_info[:values].each do |val|
        builder_arg = "xml.#{field}(val, element_attrs)"
        puts builder_arg.inspect
        eval(builder_arg)
      end
    end
  end
  return builder.to_xml
end