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 Method Summary collapse

Constructor Details

#initialize(model, reflections) ⇒ AssociationHash

Returns a new instance of AssociationHash.



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

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

Instance Method Details

#[](name) ⇒ Object



11
12
13
# File 'lib/active_fedora/association_hash.rb', line 11

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

#[]=(name, object) ⇒ Object



15
16
17
# File 'lib/active_fedora/association_hash.rb', line 15

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

#association(name) ⇒ Object



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

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



73
74
75
76
77
# File 'lib/active_fedora/association_hash.rb', line 73

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



67
68
69
70
71
# File 'lib/active_fedora/association_hash.rb', line 67

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

#empty?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/active_fedora/association_hash.rb', line 63

def empty?
  reflections.empty?
end

#freezeObject



86
87
88
89
90
91
# File 'lib/active_fedora/association_hash.rb', line 86

def freeze
  keys.each do |name|
    association(name).reader.freeze if association(name).loaded?
  end
  super
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/active_fedora/association_hash.rb', line 55

def has_key?(key)
  keys.include?(key)
end

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

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)


46
47
48
# File 'lib/active_fedora/association_hash.rb', line 46

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

#keysObject



39
40
41
# File 'lib/active_fedora/association_hash.rb', line 39

def keys
  reflections.keys
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

#reflectionsObject



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

def reflections
  @reflections
end

#selectObject



79
80
81
82
83
84
# File 'lib/active_fedora/association_hash.rb', line 79

def select
  keys.each_with_object({}) do |k, h|
    val = self[k]
    h[k] = val if yield k, val
  end
end

#sizeObject



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

def size
  keys.size
end

#valuesObject



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

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