Module: ActiveFedora::Attributes

Extended by:
ActiveSupport::Autoload, ActiveSupport::Concern
Includes:
ActiveModel::Dirty, ActiveModel::ForbiddenAttributesProtection
Included in:
Base
Defined in:
lib/active_fedora.rb,
lib/active_fedora/attributes.rb,
lib/active_fedora/attributes/node_config.rb,
lib/active_fedora/attributes/primary_key.rb,
lib/active_fedora/attributes/serializers.rb,
lib/active_fedora/attributes/property_builder.rb

Defined Under Namespace

Modules: ClassMethods, PrimaryKey, Serializers Classes: NodeConfig, PropertyBuilder

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/active_fedora/attributes.rb', line 40

def [](key)
  if assoc = self.association(key.to_sym)
    # This is for id attributes stored in the rdf graph.
    assoc.reader
  elsif self.class.properties.key?(key.to_s)
    # Use the generated method so that single value assetions are single
    self.send(key)
  else
    # The attribute is a delegate to a datastream
    array_reader(key)
  end
end

#[]=(key, value) ⇒ Object

Raises:



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/active_fedora/attributes.rb', line 53

def []=(key, value)
  raise ReadOnlyRecord if readonly?
  if assoc = self.association(key.to_sym)
    # This is for id attributes stored in the rdf graph.
    assoc.replace(value)
  elsif self.class.properties.key?(key.to_s)
    # The attribute is stored in the RDF graph for this object
    self.send(key.to_s+"=", value)
  else
    # The attribute is a delegate to a datastream
    array_setter(key, value)
  end
end

#attribute_namesObject



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

def attribute_names
  self.class.attribute_names
end

#attributesObject



29
30
31
# File 'lib/active_fedora/attributes.rb', line 29

def attributes
  attribute_names.each_with_object({"id" => id}) {|key, hash| hash[key] = self[key] }
end

#attributes=(properties) ⇒ Object



19
20
21
22
23
# File 'lib/active_fedora/attributes.rb', line 19

def attributes=(properties)
  sanitize_for_mass_assignment(properties).each do |k, v|
    respond_to?(:"#{k}=") ? send(:"#{k}=", v) : raise(UnknownAttributeError, "#{self.class} does not have an attribute `#{k}'")
  end
end

#inspectObject

Calling inspect may trigger a bunch of datastream loads, but it’s mainly for debugging, so no worries.



34
35
36
37
38
# File 'lib/active_fedora/attributes.rb', line 34

def inspect
  values = ["id: #{id.inspect}"]
  values << self.class.attribute_names.map { |attr| "#{attr}: #{self[attr].inspect}" }
  "#<#{self.class} #{values.flatten.join(', ')}>"
end

#local_attributesObject



77
78
79
# File 'lib/active_fedora/attributes.rb', line 77

def local_attributes
  self.class.local_attributes
end

#mark_as_changed(field) ⇒ Object



73
74
75
# File 'lib/active_fedora/attributes.rb', line 73

def mark_as_changed(field)
  self.send("#{field}_will_change!")
end

#value_has_changed?(field, new_value) ⇒ Boolean

value different from the new_value.

Returns:

  • (Boolean)

    true if there is an reader method and it returns a



69
70
71
# File 'lib/active_fedora/attributes.rb', line 69

def value_has_changed?(field, new_value)
  new_value != array_reader(field)
end