Class: Goldendocx::Documents::Body

Inherits:
Object
  • Object
show all
Includes:
Element
Defined in:
lib/goldendocx/documents/body.rb

Constant Summary collapse

XML_PATH =
'word/document.xml'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Element

#build_element, #namespace, #tag, #tag_name, #to_xml

Methods included from HasChildren

#children, #read_child, #read_children

Methods included from HasAttributes

#assign_attributes, #attributes, #read_attributes

Constructor Details

#initializeBody

Returns a new instance of Body.



36
37
38
39
# File 'lib/goldendocx/documents/body.rb', line 36

def initialize
  @components = []
  @charts = []
end

Instance Attribute Details

#chartsObject (readonly)

Returns the value of attribute charts.



10
11
12
# File 'lib/goldendocx/documents/body.rb', line 10

def charts
  @charts
end

#componentsObject (readonly)

Returns the value of attribute components.



10
11
12
# File 'lib/goldendocx/documents/body.rb', line 10

def components
  @components
end

Class Method Details

.read_from(xml_node) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/goldendocx/documents/body.rb', line 24

def read_from(xml_node)
  document = super(xml_node)

  component_tags = %w[w:p w:tbl]
  xml_node.children.map do |node|
    document.components << node if component_tags.include?(node.tag_name)
  end

  document
end

Instance Method Details

#create_chart(chart_type, chart_id, relationship_id, options) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/goldendocx/documents/body.rb', line 87

def create_chart(chart_type, chart_id, relationship_id, options)
  chart_class = ensure_chart_type(chart_type)

  chart = chart_class.new(chart_id, relationship_id, options)

  charts << chart
  components << chart

  chart
end

#create_embed_image(relationship_id, options) ⇒ Object



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

def create_embed_image(relationship_id, options)
  Goldendocx::Components::Image.new(relationship_id:, width: options[:width], height: options[:height])
end

#create_image(relationship_id, options) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/goldendocx/documents/body.rb', line 68

def create_image(relationship_id, options)
  image = build_image(relationship_id:, width: options[:width], height: options[:height])

  components << image

  image
end

#create_table(options = {}) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/goldendocx/documents/body.rb', line 59

def create_table(options = {})
  table = build_table
  table.width = options[:width] if options[:width]

  components << table

  table
end

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



51
52
53
54
55
56
57
# File 'lib/goldendocx/documents/body.rb', line 51

def create_text(text, options = {})
  text = build_text(text:, **options.slice(:align, :color, :bold))

  components << text

  text
end

#ensure_chart_type(chart_type) ⇒ Object



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

def ensure_chart_type(chart_type)
  chart_class = "::Goldendocx::Components::#{chart_type.capitalize}Chart"
  raise Goldendocx::Charts::InvalidChartType unless Kernel.const_defined?(chart_class)

  Kernel.const_get(chart_class)
end

#to_element(**_context) ⇒ Object

FIXME: Override for children not in correctly order



42
43
44
45
46
47
48
49
# File 'lib/goldendocx/documents/body.rb', line 42

def to_element(**_context)
  Goldendocx.xml_serializer.build_element(tag_name) do |xml|
    components.each { |component| xml << component }
    xml << section_property if section_property

    yield(xml) if block_given?
  end
end