Class: ActiveFedora::AssociationHash

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

Overview

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

Direct Known Subclasses

FilesHash, Merged

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, reflections) ⇒ AssociationHash

Returns a new instance of AssociationHash.



8
9
10
11
# File 'lib/active_fedora/association_hash.rb', line 8

def initialize(model, reflections)
  @base = model
  @reflections = reflections
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



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

def base
  @base
end

#reflectionsObject (readonly)

Returns the value of attribute reflections.



27
28
29
# File 'lib/active_fedora/association_hash.rb', line 27

def reflections
  @reflections
end

Instance Method Details

#[](name) ⇒ Object



13
14
15
# File 'lib/active_fedora/association_hash.rb', line 13

def [](name)
  association(name)&.reader
end

#[]=(name, object) ⇒ Object



17
18
19
# File 'lib/active_fedora/association_hash.rb', line 17

def []=(name, object)
  association(name)&.writer(object)
end

#association(name) ⇒ Object



21
22
23
24
25
# 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

#changedObject



64
65
66
67
68
# File 'lib/active_fedora/association_hash.rb', line 64

def changed
  select do |_, obj|
    obj.changed?
  end
end

#eachObject



33
34
35
36
37
# File 'lib/active_fedora/association_hash.rb', line 33

def each
  keys.each do |k|
    yield k, self[k]
  end
end

#each_valueObject



58
59
60
61
62
# File 'lib/active_fedora/association_hash.rb', line 58

def each_value
  keys.each do |k|
    yield self[k]
  end
end

#freezeObject



80
81
82
83
84
85
# 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

#key?(key) ⇒ Boolean Also known as: include?, has_key?

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.

Returns:

  • (Boolean)


44
45
46
# File 'lib/active_fedora/association_hash.rb', line 44

def key?(key)
  keys.include?(key) || keys.map(&:to_s).include?(key)
end

#merge(other_hash) ⇒ Object



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

def merge(other_hash)
  Merged.new(self, other_hash)
end

#selectObject

returns the loaded files for with the passed block returns true



71
72
73
74
75
76
77
78
# 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

#valuesObject



50
51
52
# File 'lib/active_fedora/association_hash.rb', line 50

def values
  keys.map { |k| self[k] }
end