Class: ODFReport::Image

Inherits:
Field
  • Object
show all
Defined in:
lib/odf-report/image.rb

Constant Summary collapse

IMAGE_DIR_NAME =
"Pictures"

Constants inherited from Field

Field::DELIMITERS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Field

#set_source

Constructor Details

#initialize(opts, &block) ⇒ Image

Returns a new instance of Image.



8
9
10
11
# File 'lib/odf-report/image.rb', line 8

def initialize(opts, &block)
  @files = []
  super
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



6
7
8
# File 'lib/odf-report/image.rb', line 6

def files
  @files
end

Class Method Details

.include_image_file(zip_file, image_file) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/odf-report/image.rb', line 33

def self.include_image_file(zip_file, image_file)
  return unless image_file

  href = File.join(IMAGE_DIR_NAME, File.basename(image_file))

  zip_file.update_file(href, File.read(image_file))
end

.include_manifest_entry(content, image_file) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/odf-report/image.rb', line 41

def self.include_manifest_entry(content, image_file)
  return unless image_file

  return unless root_node = content.at("//manifest:manifest")

  href = File.join(IMAGE_DIR_NAME, File.basename(image_file))

  entry = content.create_element('manifest:file-entry')
  entry['manifest:full-path']  = href
  entry['manifest:media-type'] = MIME::Types.type_for(href)[0].content_type

  root_node.add_child entry

end

Instance Method Details

#replace!(doc, data_item = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/odf-report/image.rb', line 13

def replace!(doc, data_item = nil)

  frame = doc.xpath("//draw:frame[@draw:name='#{@name}']").first
  image = doc.xpath("//draw:frame[@draw:name='#{@name}']/draw:image").first

  return unless image

  file = @data_source.value

  if file
    image.attribute('href').content = File.join(IMAGE_DIR_NAME, File.basename(file))
    frame.attribute('name').content = SecureRandom.uuid

    @files << file
  else
    frame.remove
  end
  
end