Class: ActiveFedora::AssociationHash
- Inherits:
-
Object
- Object
- ActiveFedora::AssociationHash
- Defined in:
- lib/active_fedora/association_hash.rb
Overview
Used as an access method for associations on a model, given some reflections.
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Returns the value of attribute base.
-
#reflections ⇒ Object
readonly
Returns the value of attribute reflections.
Instance Method Summary collapse
- #[](name) ⇒ Object
- #[]=(name, object) ⇒ Object
- #association(name) ⇒ Object
- #changed ⇒ Object
- #each ⇒ Object
- #each_value ⇒ Object
- #freeze ⇒ Object
-
#initialize(model, reflections) ⇒ AssociationHash
constructor
A new instance of AssociationHash.
-
#key?(key) ⇒ Boolean
(also: #include?, #has_key?)
Check that the key exists with indifferent access (symbol or string) in a manner that avoids generating extra symbols.
- #merge(other_hash) ⇒ Object
-
#select ⇒ Object
returns the loaded files for with the passed block returns true.
- #values ⇒ Object
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
#base ⇒ Object (readonly)
Returns the value of attribute base.
6 7 8 |
# File 'lib/active_fedora/association_hash.rb', line 6 def base @base end |
#reflections ⇒ Object (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 |
#changed ⇒ Object
64 65 66 67 68 |
# File 'lib/active_fedora/association_hash.rb', line 64 def changed select do |_, obj| obj.changed? end end |
#each ⇒ Object
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_value ⇒ Object
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 |
#freeze ⇒ Object
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.
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 |
#select ⇒ Object
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 |
#values ⇒ Object
50 51 52 |
# File 'lib/active_fedora/association_hash.rb', line 50 def values keys.map { |k| self[k] } end |