class Sablon::Content::Image
Handles reading image data and inserting it into the document
Attributes
local_rid[RW]
rid_by_file[R]
Public Class Methods
id()
click to toggle source
# File lib/sablon/content.rb, line 180 def self.id; :image end
new(source, attributes = {})
click to toggle source
Calls superclass method
# File lib/sablon/content.rb, line 187 def initialize(source, attributes = {}) attributes = Hash[attributes.map { |k, v| [k.to_s, v] }] # If the source object is readable, use it as such otherwise open # and read the content if source.respond_to?(:read) name, img_data = process_readable(source, attributes) else name = File.basename(source) img_data = IO.binread(source) end # super name, img_data @attributes = attributes @properties = @attributes.fetch("properties", {}) # rId's are separate for each XML file but I want to be able # to reuse the actual image file itself. @rid_by_file = {} end
wraps?(value)
click to toggle source
# File lib/sablon/content.rb, line 181 def self.wraps?(value) false end
Public Instance Methods
append_to(paragraph, display_node, env)
click to toggle source
# File lib/sablon/content.rb, line 217 def append_to(paragraph, display_node, env) end
height()
click to toggle source
# File lib/sablon/content.rb, line 212 def height return unless (height_str = @properties[:height]) convert_to_emu(height_str) end
inspect()
click to toggle source
# File lib/sablon/content.rb, line 183 def inspect "#<Image #{name}:#{@rid_by_file}>" end
width()
click to toggle source
# File lib/sablon/content.rb, line 207 def width return unless (width_str = @properties[:width]) convert_to_emu(width_str) end
Private Instance Methods
convert_to_emu(dim_str)
click to toggle source
Convert centimeters or inches to Word specific emu format
# File lib/sablon/content.rb, line 241 def convert_to_emu(dim_str) value, unit = dim_str.match(/(^\.?\d+\.?\d*)(\w+)/).to_a[1..-1] value = value.to_f if unit == "cm" value = value * 360000 elsif unit == "in" value = value * 914400 else throw ArgumentError, "Unsupported unit '#{unit}', only 'cm' and 'in' are permitted." end value.round() end
process_readable(source, attributes)
click to toggle source
Reads the data and attempts to find a filename from either the attributes hash or a filename method on the source object itself. A filename is required inorder for MS Word to know the content type.
# File lib/sablon/content.rb, line 224 def process_readable(source, attributes) if attributes['filename'] name = attributes['filename'] elsif source.respond_to?(:filename) name = source.filename else begin name = File.basename(source) rescue TypeError raise ArgumentError, "Error: Could not determine filename from source, try: `Sablon.content(readable_obj, filename: '...')`" end end # [File.basename(name), source.read] end