class ActiveFedora::AssociationHash

Used as an access method for associations on a model, given some reflections.

Attributes

base[R]
reflections[R]

Public Class Methods

new(model, reflections) click to toggle source
# File lib/active_fedora/association_hash.rb, line 8
def initialize(model, reflections)
  @base = model
  @reflections = reflections
end

Public Instance Methods

[](name) click to toggle source
# File lib/active_fedora/association_hash.rb, line 13
def [](name)
  association(name)&.reader
end
[]=(name, object) click to toggle source
# File lib/active_fedora/association_hash.rb, line 17
def []=(name, object)
  association(name)&.writer(object)
end
association(name) click to toggle source
# File lib/active_fedora/association_hash.rb, line 21
def association(name)
  # Check to see if the key exists before casting to a symbol, because symbols
  # are not garbage collected in earlier versions of Ruby
  base.association(name.to_sym) if key?(name)
end
changed() click to toggle source
# File lib/active_fedora/association_hash.rb, line 64
def changed
  select do |_, obj|
    obj.changed?
  end
end
each() { |k, self| ... } click to toggle source
# File lib/active_fedora/association_hash.rb, line 33
def each
  keys.each do |k|
    yield k, self[k]
  end
end
each_value() { |self| ... } click to toggle source
# File lib/active_fedora/association_hash.rb, line 58
def each_value
  keys.each do |k|
    yield self[k]
  end
end
freeze() click to toggle source
Calls superclass method
# File lib/active_fedora/association_hash.rb, line 80
def freeze
  keys.each do |name|
    association(name).reader.freeze if association(name).loaded?
  end
  super
end
has_key?(key)
Alias for: key?
include?(key)
Alias for: key?
key?(key) click to toggle source

Check that the key exists with indifferent access (symbol or string) in a manner that avoids generating extra symbols. Symbols are not garbage collected in earlier versions of ruby.

# File lib/active_fedora/association_hash.rb, line 44
def key?(key)
  keys.include?(key) || keys.map(&:to_s).include?(key)
end
Also aliased as: include?, has_key?
merge(other_hash) click to toggle source
# File lib/active_fedora/association_hash.rb, line 29
def merge(other_hash)
  Merged.new(self, other_hash)
end
select() { |k, val| ... } click to toggle source

returns the loaded files for with the passed block returns true

# File lib/active_fedora/association_hash.rb, line 71
def select
  keys.each_with_object({}) do |k, h|
    if association(k).loaded?
      val = self[k]
      h[k] = val if yield k, val
    end
  end
end
values() click to toggle source
# File lib/active_fedora/association_hash.rb, line 50
def values
  keys.map { |k| self[k] }
end