Class: ActiveFedora::OmDatastream

Inherits:
File
  • Object
show all
Includes:
Datastreams::NokogiriDatastreams, OM::XML::Document, OM::XML::TerminologyBasedSolrizer
Defined in:
lib/active_fedora/om_datastream.rb

Constant Summary

Constants included from AttributeMethods

AttributeMethods::AttrNames, AttributeMethods::BLACKLISTED_CLASS_METHODS

Constants included from Callbacks

Callbacks::CALLBACKS

Instance Method Summary collapse

Methods included from Datastreams::NokogiriDatastreams

#autocreate?, #content, #content=, #content_changed?, #ng_xml, #ng_xml=, #ng_xml_changed?, #ng_xml_doesnt_change!, #ng_xml_will_change!, #refresh_attributes, #remote_content, #to_xml, #xml_loaded

Methods inherited from File

#attribute_will_change!, #changed?, #check_fixity, #checksum, #content, #content=, #content_changed?, #datastream_will_change!, #described_by, #exists!, #initialize, #inspect, #ldp_connection, #metadata, #metadata_changed?, #new_record?, #refresh, #reload, #remote_content, #save, #serialize!, #uri=

Methods included from Querying

#default_sort_params, extended

Methods included from Scoping

#initialize_internals_callback, #populate_with_current_scope_attributes

Methods included from Identifiable

#id, #id=, #pid, #uri

Methods included from AttributeMethods

#[], #[]=, #attribute_for_inspect, #attribute_names, #attribute_present?, #attributes, #has_attribute?

Methods included from Callbacks

#destroy

Methods included from Versionable

#create_version, #has_versions?, #model_type, #restore_version, #versions

Methods included from Persistence

#delete, #destroy, #destroy!, #destroyed?, #eradicate, #new_record?, #persisted?, #save, #save!, #update

Methods included from File::Streaming

#headers, #stream

Methods included from File::Attributes

#create_date, #digest, #dirty_size, #empty?, #has_content?, #mime_type, #original_name, #original_name=, #persisted_size, #size

Methods included from Common

#<=>, #==, #freeze, #frozen?, #ldp_source, #readonly!, #readonly?

Constructor Details

This class inherits a constructor from ActiveFedora::File

Instance Method Details

#default_mime_typeObject



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

def default_mime_type
  'text/xml'
end

#find_by_terms(*termpointer) ⇒ Object



109
110
111
# File 'lib/active_fedora/om_datastream.rb', line 109

def find_by_terms(*termpointer)
  super
end

#get_values(field_key, _default = []) ⇒ Object



105
106
107
# File 'lib/active_fedora/om_datastream.rb', line 105

def get_values(field_key, _default = [])
  term_values(*field_key)
end

#has_solr_name?(name, solr_doc = {}) ⇒ Boolean

** Experimental **

Parameters:

  • name (String)

    Name of key to look for

  • solr_doc (Solr::Document) (defaults to: {})

    Solr doc to query

Returns:

  • (Boolean)

    true if either the key for name exists in solr or if its string value exists



40
41
42
# File 'lib/active_fedora/om_datastream.rb', line 40

def has_solr_name?(name, solr_doc = {})
  !solr_doc[name].nil? || !solr_doc[name.to_s].nil?
end

#is_hierarchical_term_pointer?(*term_pointer) ⇒ Boolean

** Experimental **

Example:

[:image, {:title_set=>1}, :title] return true
[:image, :title_set, :title]      return false

Returns:

  • (Boolean)

    true if the term_pointer contains an index



49
50
51
52
53
54
55
56
# File 'lib/active_fedora/om_datastream.rb', line 49

def is_hierarchical_term_pointer?(*term_pointer)
  if term_pointer.length > 1
    term_pointer.each do |pointer|
      return true if pointer.is_a?(Hash)
    end
  end
  false
end

#metadata?Boolean

Indicates that this datastream has metadata content.

Returns:

  • (Boolean)

    true



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

def metadata?
  true
end

#om_update_valuesObject



17
# File 'lib/active_fedora/om_datastream.rb', line 17

alias om_update_values update_values

#to_solr(solr_doc = {}, opts = {}) ⇒ Object

Return a hash suitable for indexing in solr. Every field name is prefixed with the value returned by the prefix method.



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

def to_solr(solr_doc = {}, opts = {})
  prefix = self.prefix(opts[:name])
  solr_doc.merge super({}).each_with_object({}) { |(key, value), new| new[[prefix, key].join] = value }
end

#update_indexed_attributes(params = {}, _opts = {}) ⇒ Object

Update field values within the current datastream using #update_values, which is a wrapper for OM::TermValueOperators#update_values Ignores any fields from params that this datastream’s Terminology doesn’t recognize

Example:

@mods_ds.update_indexed_attributes( {[{":person"=>"0"}, "role"]=>{"0"=>"role1", "1"=>"role2", "2"=>"role3"} })
=> {"person_0_role"=>{"0"=>"role1", "1"=>"role2", "2"=>"role3"}}

@mods_ds.to_xml # (the following is an approximation)
<mods>
  <mods:name type="person">
  <mods:role>
    <mods:roleTerm>role1</mods:roleTerm>
  </mods:role>
  <mods:role>
    <mods:roleTerm>role2</mods:roleTerm>
  </mods:role>
  <mods:role>
    <mods:roleTerm>role3</mods:roleTerm>
  </mods:role>
  </mods:name>
</mods>

Parameters:

  • params (Hash) (defaults to: {})

    The params specifying which fields to update and their new values. The syntax of the params Hash is the same as that expected by term_pointers must be a valid OM Term pointers (ie. [:name]). Strings will be ignored.

  • _opts (Hash) (defaults to: {})

    This is not currently used by the datastream-level update_indexed_attributes method



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/active_fedora/om_datastream.rb', line 83

def update_indexed_attributes(params = {}, _opts = {})
  if self.class.terminology.nil?
    raise "No terminology is set for this OmDatastream class.  Cannot perform update_indexed_attributes"
  end
  # remove any fields from params that this datastream doesn't recognize
  # make sure to make a copy of params so not to modify hash that might be passed to other methods
  current_params = params.clone
  current_params.delete_if do |term_pointer, new_values|
    if term_pointer.is_a?(String)
      ActiveFedora::Base.logger.warn "WARNING: #{self.class.name} ignoring {#{term_pointer.inspect} => #{new_values.inspect}} because #{term_pointer.inspect} is a String (only valid OM Term Pointers will be used).  Make sure your html has the correct field_selector tags in it." if ActiveFedora::Base.logger
      true
    else
      !self.class.terminology.has_term?(*OM.destringify(term_pointer))
    end
  end

  result = {}
  result = update_values(current_params) unless current_params.empty?

  result
end

#update_values(params = {}) ⇒ Object

Update values in the datastream’s xml This wraps OM::TermValueOperators#update_values so that returns an error if we have loaded from solr since datastreams loaded that way should be read-only

Examples:

Updating multiple values with a Hash of Term pointers and values

ds.update_values( {[{":person"=>"0"}, "role", "text"]=>{"0"=>"role1", "1"=>"role2", "2"=>"role3"}, [{:person=>1}, :family_name]=>"Andronicus", [{"person"=>"1"},:given_name]=>["Titus"],[{:person=>1},:role,:text]=>["otherrole1","otherrole2"] } )
=> {"person_0_role_text"=>{"0"=>"role1", "1"=>"role2", "2"=>"role3"}, "person_1_role_text"=>{"0"=>"otherrole1", "1"=>"otherrole2"}}


119
120
121
122
123
124
# File 'lib/active_fedora/om_datastream.rb', line 119

def update_values(params = {})
  raise "can't modify frozen #{self.class}" if frozen?
  ng_xml_will_change!
  result = om_update_values(params)
  result
end