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

#fields ⇒ Object

Returns the value of attribute fields.



6
7
8
# File 'lib/active_fedora/metadata_datastream_helper.rb', line 6

def fields
  @fields
end

Class Method Details

.included(klass) ⇒ Object



19
20
21
22
# File 'lib/active_fedora/metadata_datastream_helper.rb', line 19

def self.included(klass)
  klass.extend(ClassMethods)
  klass.send(:include, Solrizer::FieldNameMapper)
end

Instance Method Details

#from_solr(solr_doc) ⇒ Object

** EXPERIMENTAL **

This is utilized by ActiveFedora::Base.load_instance_from_solr to set metadata values in this object using the Solr document passed in. Any keys in the solr document that map to a metadata field key within a MetadataDatastream object are set to the corresponding value. Any others are ignored. ActiveFedora::SolrService.solr_name is used to map solr key to field key name.

====Warning Solr must be synchronized with data in Fedora.



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/active_fedora/metadata_datastream_helper.rb', line 63

def from_solr(solr_doc)
  fields.each do |field_key, field_info|
    field_symbol = ActiveFedora::SolrService.solr_name(field_key, field_info[:type])
    value = (solr_doc[field_symbol].nil? ? solr_doc[field_symbol.to_s]: solr_doc[field_symbol]) 
    unless value.nil?
      if value.is_a? Array
        update_attributes({field_key=>value})
      else
        update_indexed_attributes({field_key=>{0=>value}})
      end
    end
  end
end

#initialize(attrs = nil) ⇒ Object

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



25
26
27
28
# File 'lib/active_fedora/metadata_datastream_helper.rb', line 25

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

#save ⇒ Object

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



31
32
33
34
# File 'lib/active_fedora/metadata_datastream_helper.rb', line 31

def save
  self.set_blob_for_save
  super
end

#set_blob_for_save ⇒ Object

:nodoc:



36
37
38
# File 'lib/active_fedora/metadata_datastream_helper.rb', line 36

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

#to_solr(solr_doc = Hash.new) ⇒ Object

:nodoc:



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

def to_solr(solr_doc = Hash.new) # :nodoc:
  fields.each do |field_key, field_info|
    if field_info.has_key?(:values) && !field_info[:values].nil?
      field_symbol = ActiveFedora::SolrService.solr_name(field_key, field_info[:type])
      field_info[:values].each do |val|    
        ::Solrizer::Extractor.insert_solr_field_value(solr_doc, field_symbol, val )         
      end
    end
  end

  return solr_doc
end

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

:nodoc:



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/active_fedora/metadata_datastream_helper.rb', line 77

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)"
        eval(builder_arg)
      end
    end
  end
  return builder.to_xml
end