Class: Uniword::Docx::PartCollection
- Inherits:
-
Object
- Object
- Uniword::Docx::PartCollection
- Includes:
- Enumerable
- Defined in:
- lib/uniword/docx/part_collection.rb
Overview
Keyed collection of Docx::Part objects.
Backs chart_parts (keyed by relationship id) and embeddings
(keyed by relationship target). Keeps the legacy Hash-like
access patterns working: assignment accepts Part objects, the
former raw hashes ({ xml:, target: }) and raw content Strings,
normalizing them into Part objects.
Instance Method Summary collapse
- #[](key) ⇒ Part?
-
#[]=(key, value) ⇒ Object
Store a part, normalizing raw hashes and raw content.
-
#delete(key) ⇒ Part?
The removed part.
-
#each(&block) ⇒ Object
Iterate over [key, Part] pairs.
-
#each_key(&block) ⇒ Object
Iterate over collection keys.
-
#each_value(&block) ⇒ Object
Iterate over stored parts.
- #empty? ⇒ Boolean
-
#initialize(key_attribute, part_class) ⇒ PartCollection
constructor
A new instance of PartCollection.
- #key?(key) ⇒ Boolean
- #keys ⇒ Array<String>
-
#replace_all(value) ⇒ void
Replace the whole collection from a Hash (or another PartCollection); nil clears it.
- #size ⇒ Integer
- #values ⇒ Array<Part>
Constructor Details
#initialize(key_attribute, part_class) ⇒ PartCollection
Returns a new instance of PartCollection.
23 24 25 26 27 |
# File 'lib/uniword/docx/part_collection.rb', line 23 def initialize(key_attribute, part_class) @key_attribute = key_attribute @part_class = part_class @parts = {} end |
Instance Method Details
#[](key) ⇒ Part?
31 32 33 |
# File 'lib/uniword/docx/part_collection.rb', line 31 def [](key) @parts[key] end |
#[]=(key, value) ⇒ Object
Store a part, normalizing raw hashes and raw content.
39 40 41 |
# File 'lib/uniword/docx/part_collection.rb', line 39 def []=(key, value) @parts[key] = wrap(key, value) end |
#delete(key) ⇒ Part?
Returns the removed part.
84 85 86 |
# File 'lib/uniword/docx/part_collection.rb', line 84 def delete(key) @parts.delete(key) end |
#each(&block) ⇒ Object
Iterate over [key, Part] pairs.
44 45 46 |
# File 'lib/uniword/docx/part_collection.rb', line 44 def each(&block) @parts.each(&block) end |
#each_key(&block) ⇒ Object
Iterate over collection keys.
49 50 51 |
# File 'lib/uniword/docx/part_collection.rb', line 49 def each_key(&block) @parts.each_key(&block) end |
#each_value(&block) ⇒ Object
Iterate over stored parts.
54 55 56 |
# File 'lib/uniword/docx/part_collection.rb', line 54 def each_value(&block) @parts.each_value(&block) end |
#empty? ⇒ Boolean
74 75 76 |
# File 'lib/uniword/docx/part_collection.rb', line 74 def empty? @parts.empty? end |
#key?(key) ⇒ Boolean
79 80 81 |
# File 'lib/uniword/docx/part_collection.rb', line 79 def key?(key) @parts.key?(key) end |
#keys ⇒ Array<String>
64 65 66 |
# File 'lib/uniword/docx/part_collection.rb', line 64 def keys @parts.keys end |
#replace_all(value) ⇒ void
This method returns an undefined value.
Replace the whole collection from a Hash (or another PartCollection); nil clears it.
93 94 95 96 97 98 |
# File 'lib/uniword/docx/part_collection.rb', line 93 def replace_all(value) @parts.clear return if value.nil? value.each { |key, part| self[key] = part } end |
#size ⇒ Integer
69 70 71 |
# File 'lib/uniword/docx/part_collection.rb', line 69 def size @parts.size end |
#values ⇒ Array<Part>
59 60 61 |
# File 'lib/uniword/docx/part_collection.rb', line 59 def values @parts.values end |