Module: ActiveFedora::Datastreams::NokogiriDatastreams

Extended by:
ActiveSupport::Concern
Included in:
NomDatastream, OmDatastream
Defined in:
lib/active_fedora/datastreams/nokogiri_datastreams.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#autocreate?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/active_fedora/datastreams/nokogiri_datastreams.rb', line 114

def autocreate?
  changed_attributes.has_key? :profile
end

#contentObject



109
110
111
112
# File 'lib/active_fedora/datastreams/nokogiri_datastreams.rb', line 109

def content
  @content = to_xml if ng_xml_changed? || autocreate?
  super
end

#content=(new_content) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/active_fedora/datastreams/nokogiri_datastreams.rb', line 71

def content=(new_content)
  if remote_content != new_content.to_s
    ng_xml_will_change!
    @ng_xml = Nokogiri::XML::Document.parse(new_content)
    super(@ng_xml.to_s.strip)
  end
  self.class.decorate_ng_xml @ng_xml
end

#content_changed?Boolean

Returns:

  • (Boolean)


80
81
82
83
84
# File 'lib/active_fedora/datastreams/nokogiri_datastreams.rb', line 80

def content_changed?
  return true if autocreate? && new_record?
  return false unless xml_loaded
  ng_xml_changed?
end

#ng_xmlObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/active_fedora/datastreams/nokogiri_datastreams.rb', line 18

def ng_xml 
  @ng_xml ||= begin
    if new_record?
      ## Load up the template
      xml = self.class.xml_template
    else
      xml = Nokogiri::XML::Document.parse(remote_content)
    end
    self.class.decorate_ng_xml xml
  end
end

#ng_xml=(new_xml) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/active_fedora/datastreams/nokogiri_datastreams.rb', line 30

def ng_xml=(new_xml)
  # before we set ng_xml, we load the datastream so we know if the new value differs.
  # TODO reinstate this
  #local_or_remote_content(true)

  case new_xml 
  when Nokogiri::XML::Document
    self.content=new_xml.to_xml
  when  Nokogiri::XML::Node 
    ## Cast a fragment to a document
    self.content=new_xml.to_s
  when String 
    self.content=new_xml
  else
    raise TypeError, "You passed a #{new_xml.class} into the ng_xml of the #{self.dsid} datastream. OmDatastream.ng_xml= only accepts Nokogiri::XML::Document, Nokogiri::XML::Element, Nokogiri::XML::Node, or raw XML (String) as inputs."
  end
end

#ng_xml_changed?Boolean

don’t want content eagerly loaded by proxy, so implementing methods that would be implemented by define_attribute_methods

Returns:

  • (Boolean)


63
64
65
# File 'lib/active_fedora/datastreams/nokogiri_datastreams.rb', line 63

def ng_xml_changed?
  changed_attributes.has_key? 'ng_xml'
end

#ng_xml_doesnt_change!Object



58
59
60
# File 'lib/active_fedora/datastreams/nokogiri_datastreams.rb', line 58

def ng_xml_doesnt_change!
  changed_attributes.delete('ng_xml')
end

#ng_xml_will_change!Object

don’t want content eagerly loaded by proxy, so implementing methods that would be implemented by define_attribute_methods



54
55
56
# File 'lib/active_fedora/datastreams/nokogiri_datastreams.rb', line 54

def ng_xml_will_change!
  changed_attributes['ng_xml'] = nil
end

#refresh_attributesObject



48
49
50
51
# File 'lib/active_fedora/datastreams/nokogiri_datastreams.rb', line 48

def refresh_attributes
  changed_attributes.clear
  @ng_xml = nil
end

#remote_contentObject



67
68
69
# File 'lib/active_fedora/datastreams/nokogiri_datastreams.rb', line 67

def remote_content
  @datastream_content ||= Nokogiri::XML(super).to_xml {|config| config.no_declaration}.strip
end

#to_xml(xml = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/active_fedora/datastreams/nokogiri_datastreams.rb', line 86

def to_xml(xml = nil)
  xml = self.ng_xml if xml.nil?
  ng_xml = self.ng_xml
  if ng_xml.respond_to?(:root) && 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.strip
end

#xml_loadedObject



118
119
120
# File 'lib/active_fedora/datastreams/nokogiri_datastreams.rb', line 118

def xml_loaded
  instance_variable_defined? :@ng_xml
end