Class: ActiveFedora::NomDatastream

Inherits:
Datastream
  • Object
show all
Defined in:
lib/active_fedora/nom_datastream.rb

Instance Attribute Summary

Attributes inherited from Datastream

#digital_object, #last_modified

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Datastream

#create, #initialize, #inspect, #label, #metadata?, #profile_from_hash, #realLabel, #save, #solrize_profile, #to_param, #validate_content_present

Constructor Details

This class inherits a constructor from ActiveFedora::Datastream

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/active_fedora/nom_datastream.rb', line 80

def method_missing method, *args, &block
  if ng_xml.respond_to? method
    ng_xml.send(method, *args, &block)
  else
    super
  end
end

Class Method Details

.default_attributesObject



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

def self.default_attributes
  super.merge(:controlGroup => 'M', :mimeType => 'text/xml')
end

.from_xml(xml, tmpl = nil) ⇒ Object

Create an instance of this class based on xml content Careful! If you call this from a constructor, be sure to provide something ‘ie. self’ as the @tmpl. Otherwise, you will get an infinite loop!

Parameters:

  • xml (String, File, Nokogiri::XML::Node)

    the xml content to build from

  • tmpl (ActiveFedora::MetadataDatastream) (defaults to: nil)

    the Datastream object that you are building @default a new instance of this class



26
27
28
29
30
# File 'lib/active_fedora/nom_datastream.rb', line 26

def self.from_xml(xml, tmpl=nil)
  ds = self.new nil, nil
  ds.content = xml.to_s
  ds
end

.set_terminology(options = {}, &block) ⇒ Object



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

def self.set_terminology(options = {}, &block)
  @terminology_options = options || {}
  @terminology = block
end

.terminologyObject



14
15
16
# File 'lib/active_fedora/nom_datastream.rb', line 14

def self.terminology
  @terminology
end

.terminology_optionsObject



10
11
12
# File 'lib/active_fedora/nom_datastream.rb', line 10

def self.terminology_options
  @terminology_options
end

Instance Method Details

#contentObject



52
53
54
# File 'lib/active_fedora/nom_datastream.rb', line 52

def content
  @content || super
end

#content=(content) ⇒ Object



56
57
58
59
# File 'lib/active_fedora/nom_datastream.rb', line 56

def content=(content)
  super
  @ng_xml = nil
end

#ng_xmlObject



32
33
34
35
36
37
38
39
# File 'lib/active_fedora/nom_datastream.rb', line 32

def ng_xml
  @ng_xml ||= begin
     xml = Nokogiri::XML content
     xml.set_terminology self.class.terminology_options, &self.class.terminology
     xml.nom!
     xml
  end
end

#ng_xml=(ng_xml) ⇒ Object



41
42
43
44
45
46
# File 'lib/active_fedora/nom_datastream.rb', line 41

def ng_xml= ng_xml
  @ng_xml = ng_xml
  @ng_xml.set_terminology self.class.terminology_options, &self.class.terminology
  content_will_change!
  @ng_xml
end

#respond_to?(*args) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/active_fedora/nom_datastream.rb', line 88

def respond_to? *args
  super || self.class.terminology.respond_to?(*args)
end

#serialize!Object



48
49
50
# File 'lib/active_fedora/nom_datastream.rb', line 48

def serialize!
   self.content = @ng_xml.to_s if @ng_xml
end

#to_solrObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/active_fedora/nom_datastream.rb', line 61

def to_solr
  solr_doc = {}

  ng_xml.terminology.flatten.select { |x| x.options[:index] }.each do |term|
    term.values.each do |v|
      Array(term.options[:index]).each do |index_as|
        solr_doc[index_as] ||= []
        if v.is_a? Nokogiri::XML::Node
          solr_doc[index_as] << v.text
        else
          solr_doc[index_as] << v
        end
      end
    end
  end

  solr_doc 
end