Class: ActiveFedora::DatastreamAttribute

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

Overview

Represents the mapping between a model attribute and a field in a datastream

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field, dsid, datastream_class, args = {}) ⇒ DatastreamAttribute

Returns a new instance of DatastreamAttribute.



7
8
9
10
11
12
13
# File 'lib/active_fedora/datastream_attribute.rb', line 7

def initialize(field, dsid, datastream_class, args={})
  self.field = field
  self.dsid = dsid
  self.datastream_class = datastream_class
  self.multiple = args[:multiple].nil? ? false : args[:multiple]
  self.at = args[:at]
end

Instance Attribute Details

#atObject

Returns the value of attribute at.



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

def at
  @at
end

#datastream_classObject

Returns the value of attribute datastream_class.



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

def datastream_class
  @datastream_class
end

#dsidObject

Returns the value of attribute dsid.



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

def dsid
  @dsid
end

#fieldObject

Returns the value of attribute field.



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

def field
  @field
end

#multipleObject

Returns the value of attribute multiple.



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

def multiple
  @multiple
end

Instance Method Details

#primary_solr_nameObject

Gives the primary solr name for a column. If there is more than one indexer on the field definition, it gives the first



16
17
18
19
20
21
22
23
# File 'lib/active_fedora/datastream_attribute.rb', line 16

def primary_solr_name
  @datastream ||= datastream_class.new(nil, dsid)
  if @datastream.respond_to?(:primary_solr_name)
    @datastream.primary_solr_name(field)
  else
    raise NoMethodError, "the datastream '#{datastream_class}' doesn't respond to 'primary_solr_name'"
  end
end

#reader(obj, *opts) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/active_fedora/datastream_attribute.rb', line 44

def reader(obj, *opts)
  if obj.inner_object.is_a? SolrDigitalObject
    begin
      # Look in the cache
      return obj.inner_object.fetch(field)
    rescue NoMethodError => e
      # couldn't get it from solr, so try from fedora.
      ActiveFedora::Base.logger.info "Couldn't get #{field} out of solr, because #{e.message}. Trying another way." if ActiveFedora::Base.logger
    end
  end
  # Load from fedora
  ds = datastream_for_attribute(obj, dsid)
  if ds.kind_of?(ActiveFedora::RDFDatastream)
    ds.send(field)
  else
    terminology = at || [field]
    if terminology.length == 1 && opts.present?
      ds.send(terminology.first, *opts)
    else
      ds.send(:term_values, *terminology)
    end
  end
end

#typeObject



25
26
27
28
29
30
31
# File 'lib/active_fedora/datastream_attribute.rb', line 25

def type
  if datastream_class.respond_to?(:type)
    datastream_class.type(field)
  else
    raise NoMethodError, "the datastream '#{datastream_class}' doesn't respond to 'type'"
  end
end

#writer(obj, v) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/active_fedora/datastream_attribute.rb', line 33

def writer(obj, v)
  ds = datastream_for_attribute(obj, dsid)
  obj.mark_as_changed(field) if obj.value_has_changed?(field, v)
  if ds.kind_of?(ActiveFedora::RDFDatastream)
    ds.send("#{field}=", v)
  else
    terminology = at || [field]
    ds.send(:update_indexed_attributes, {terminology => v})
  end
end