Class: Uniword::Docx::PartCollection

Inherits:
Object
  • Object
show all
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.

Examples:

parts = PartCollection.new(:r_id)
parts["rIdChart1"] = { xml: "...", target: "charts/chart1.xml" }
parts.values.first[:target] # => "charts/chart1.xml"

Instance Method Summary collapse

Constructor Details

#initialize(key_attribute, part_class) ⇒ PartCollection

Returns a new instance of PartCollection.

Parameters:

  • key_attribute (Symbol)

    part attribute serving as the collection key (:r_id for charts, :target for embeddings)

  • part_class (Class)

    Part subclass used to wrap raw values



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?

Parameters:

  • key (String)

    collection key (rId or target)

Returns:



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.

Parameters:

  • key (String)

    collection key

  • value (Part, Hash, String)


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.

Returns:

  • (Part, nil)

    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

Returns:

  • (Boolean)


74
75
76
# File 'lib/uniword/docx/part_collection.rb', line 74

def empty?
  @parts.empty?
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/uniword/docx/part_collection.rb', line 79

def key?(key)
  @parts.key?(key)
end

#keysArray<String>

Returns:

  • (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.

Parameters:



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

#sizeInteger

Returns:

  • (Integer)


69
70
71
# File 'lib/uniword/docx/part_collection.rb', line 69

def size
  @parts.size
end

#valuesArray<Part>

Returns:



59
60
61
# File 'lib/uniword/docx/part_collection.rb', line 59

def values
  @parts.values
end