Class: ActiveFedora::FilesHash

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/active_fedora/files_hash.rb

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ FilesHash

Returns a new instance of FilesHash


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

def initialize (model)
  @base = model
end

Instance Method Details

#[](name) ⇒ Object


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

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

#[]=(name, object) ⇒ Object


15
16
17
# File 'lib/active_fedora/files_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/files_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

#eachObject


29
30
31
32
33
# File 'lib/active_fedora/files_hash.rb', line 29

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

#each_valueObject


62
63
64
65
66
# File 'lib/active_fedora/files_hash.rb', line 62

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

#empty?Boolean

Returns:

  • (Boolean)

58
59
60
# File 'lib/active_fedora/files_hash.rb', line 58

def empty?
  reflections.empty?
end

#freezeObject


75
76
77
78
79
80
# File 'lib/active_fedora/files_hash.rb', line 75

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

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)

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

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

#key?(key) ⇒ Boolean

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)

42
43
44
# File 'lib/active_fedora/files_hash.rb', line 42

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

#keysObject


35
36
37
# File 'lib/active_fedora/files_hash.rb', line 35

def keys
  reflections.keys + @base.undeclared_files
end

#reflectionsObject


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

def reflections
  @base.class.child_resource_reflections
end

#selectObject


68
69
70
71
72
73
# File 'lib/active_fedora/files_hash.rb', line 68

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

#sizeObject


54
55
56
# File 'lib/active_fedora/files_hash.rb', line 54

def size
  keys.size
end

#valuesObject


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

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