Class: Goldendocx::Docx

Inherits:
Object
  • Object
show all
Includes:
HasAssociations
Defined in:
lib/goldendocx/docx.rb

Defined Under Namespace

Classes: UnStructuredEntry

Constant Summary collapse

RELATIONSHIPS_XML_PATH =
'_rels/.rels'
STRUCTURED_ENTRIES =
[
  RELATIONSHIPS_XML_PATH,

  Goldendocx::Parts::ContentTypes::XML_PATH,
  Goldendocx::Parts::App::XML_PATH,
  Goldendocx::Parts::Core::XML_PATH,

  Goldendocx::Parts::Documents::RELATIONSHIPS_XML_PATH,

  Goldendocx::Documents::Document::XML_PATH,
  Goldendocx::Documents::Settings::XML_PATH,
  Goldendocx::Documents::Styles::XML_PATH
].freeze

Constants included from HasAssociations

HasAssociations::Options

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HasAssociations

#read_associations, #read_relationships, #relationships, #write_relationships

Constructor Details

#initialize(file_path = nil) ⇒ Docx

Returns a new instance of Docx.



35
36
37
# File 'lib/goldendocx/docx.rb', line 35

def initialize(file_path = nil)
  file_path.present? ? read_from(file_path) : build_default
end

Instance Attribute Details

#documentsObject (readonly)

Returns the value of attribute documents.



12
13
14
# File 'lib/goldendocx/docx.rb', line 12

def documents
  @documents
end

#unstructured_entriesObject (readonly)

Returns the value of attribute unstructured_entries.



12
13
14
# File 'lib/goldendocx/docx.rb', line 12

def unstructured_entries
  @unstructured_entries
end

Instance Method Details

#add_style(style_path) ⇒ Object



76
77
78
# File 'lib/goldendocx/docx.rb', line 76

def add_style(style_path)
  documents.add_style(style_path)
end

#create_chart(chart_type, width: nil, height: nil) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/goldendocx/docx.rb', line 80

def create_chart(chart_type, width: nil, height: nil)
  chart = documents.create_chart(
    chart_type, width || Charts::DEFAULT_WIDTH, height || Charts::DEFAULT_HEIGHT
  )
  ensure_chart_content_type!(chart)
  chart
end

#create_embed_image(image_data, options = {}) ⇒ Object



71
72
73
74
# File 'lib/goldendocx/docx.rb', line 71

def create_embed_image(image_data, options = {})
  ensure_image_content_type!(image_data)
  documents.create_embed_image(image_data, options)
end

#create_image(image_data, options = {}) ⇒ Object



66
67
68
69
# File 'lib/goldendocx/docx.rb', line 66

def create_image(image_data, options = {})
  ensure_image_content_type!(image_data)
  documents.create_image(image_data, options)
end

#create_table(options = {}) ⇒ Object



62
63
64
# File 'lib/goldendocx/docx.rb', line 62

def create_table(options = {})
  documents.create_table(options)
end

#create_text(text, options = {}) ⇒ Object



58
59
60
# File 'lib/goldendocx/docx.rb', line 58

def create_text(text, options = {})
  documents.create_text(text, options)
end

#read_from(file_path) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/goldendocx/docx.rb', line 39

def read_from(file_path)
  docx_file = Zip::File.new(file_path)

  read_relationships(docx_file)
  read_associations(docx_file)

  @documents = Goldendocx::Parts::Documents.read_from(docx_file)

  @unstructured_entries = docx_file.entries.filter_map do |entry|
    UnStructuredEntry.new(entry) unless STRUCTURED_ENTRIES.include?(entry.name)
  end

  self
end

#write_to(new_path) ⇒ Object



54
55
56
# File 'lib/goldendocx/docx.rb', line 54

def write_to(new_path)
  File.binwrite(new_path, to_stream.string)
end