Class: ActiveFedora::SimpleDatastream

Inherits:
OmDatastream show all
Defined in:
lib/active_fedora/simple_datastream.rb

Overview

This class represents a simple xml datastream.

Constant Summary

Constants included from AttributeMethods

AttributeMethods::AttrNames

Instance Attribute Summary collapse

Attributes included from File::Attributes

#mime_type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from OmDatastream

#default_mime_type, #find_by_terms, #get_values, #has_solr_name?, #is_hierarchical_term_pointer?, #metadata?, #om_update_values, #update_values

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, #content, #content=, #content_changed?, #datastream_will_change!, #described_by, #exists!, #freeze, #frozen?, #inspect, #ldp_connection, #ldp_source, #metadata, #metadata?, #new_record?, #readonly?, #refresh, #reload, #remote_content, #serialize!, #uri=

Methods included from Querying

#default_sort_params, extended

Methods included from Identifiable

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

Methods included from AttributeMethods

#[], #[]=, #attribute_names, #attributes

Methods included from Versionable

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

Methods included from Persistence

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

Methods included from File::Streaming

#headers, #stream

Methods included from File::Attributes

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

Constructor Details

#initialize(digital_object = nil, dsid = nil, options = {}, &block) ⇒ SimpleDatastream

Constructor. this class will call self.field for each DCTERM. In short, all DCTERMS fields will already exist when this method returns. Each term is marked as a multivalue string.



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

def initialize(digital_object=nil, dsid=nil, options={}, &block)
  self.fields={}
  super
end

Instance Attribute Details

#fieldsObject

Returns the value of attribute fields.



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

def fields
  @fields
end

Class Method Details

.xml_templateObject



79
80
81
# File 'lib/active_fedora/simple_datastream.rb', line 79

def self.xml_template
   Nokogiri::XML::Document.parse("<fields/>")
end

Instance Method Details

#field(name, datatype = :string, opts = {}) ⇒ Object

This method generates the various accessor and mutator methods on self for the datastream metadata attributes. each field will have the 2 magic methods:

name=(arg)
name

‘datatype’ is a datatype, currently :string, :integer and :date are supported.

opts is an options hash, which will affect the generation of the xml representation of this datastream.

Currently supported modifiers: For SimpleDatastream:

:element_attrs =>{:foo=>:bar} -  hash of xml element attributes
:xml_node => :nodename  - The xml node to be used to represent this object (in dcterms namespace)
:encoding=>foo, or encodings_scheme  - causes an xsi:type attribute to be set to 'foo'
:multiple=>true -  mark this field as a multivalue field (on by default)

There is quite a good example of this class in use in spec/examples/oral_history.rb

!! Careful: If you declare two fields that correspond to the same xml node without any qualifiers to differentiate them, you will end up replicating the values in the underlying datastream, resulting in mysterious dubling, quadrupling, etc. whenever you edit the field’s values.



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/active_fedora/simple_datastream.rb', line 51

def field(name, datatype=:string, opts={})
  fields ||= {}
  @fields[name.to_s.to_sym]={:type=>datatype, :values=>[]}.merge(opts)
  # add term to template
  self.class.class_fields << name.to_s
  # add term to terminology
  unless self.class.terminology.has_term?(name.to_sym)
    term = OM::XML::Term.new(name.to_sym, {:type=>datatype}, self.class.terminology)
    self.class.terminology.add_term(term)
    term.generate_xpath_queries!
  end
end

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

:nodoc:



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/active_fedora/simple_datastream.rb', line 83

def to_solr(solr_doc = Hash.new, opts = {}) # :nodoc:
  @fields.each do |field_key, field_info|
    next if field_key == :location ## FIXME HYDRA-825
    things = send(field_key)
    if things
      field_symbol = ActiveFedora::SolrQueryBuilder.solr_name(field_key, type: field_info[:type])
      things.val.each do |val|
        ::Solrizer::Extractor.insert_solr_field_value(solr_doc, field_symbol, val.to_s )
      end
    end
  end
  return solr_doc
end

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



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

def update_indexed_attributes(params={}, opts={})
  raise "can't modify frozen #{self.class}" if frozen?
  # if the params are just keys, not an array, make then into an array.
  new_params = {}
  params.each do |key, val|
    if key.is_a? Array
      new_params[key] = val
    else
      new_params[[key.to_sym]] = val
    end
  end
  super(new_params, opts)
end