module Paperclip::Meta::Attachment

Public Instance Methods

aspect_ratio(style = default_style) click to toggle source
# File lib/paperclip-meta/attachment.rb, line 34
def aspect_ratio(style = default_style)
  width(style).to_f / height(style).to_f
end
assign_attributes() click to toggle source
Calls superclass method
# File lib/paperclip-meta/attachment.rb, line 4
def assign_attributes
  super
  assign_meta
end
height(style = default_style) click to toggle source
# File lib/paperclip-meta/attachment.rb, line 26
def height(style = default_style)
  read_meta style, :height
end
image_size(style = default_style) click to toggle source

Return image dimesions (“WxH”) for given style name. If style name not given, return dimesions for default_style.

# File lib/paperclip-meta/attachment.rb, line 40
def image_size(style = default_style)
  "#{width(style)}x#{height(style)}"
end
post_process_styles(*styles) click to toggle source
Calls superclass method
# File lib/paperclip-meta/attachment.rb, line 16
def post_process_styles(*styles)
  super(*styles)
  assign_meta
end
save() click to toggle source
Calls superclass method
# File lib/paperclip-meta/attachment.rb, line 9
def save
  if @queued_for_delete.any? && @queued_for_write.empty?
    instance_write(:meta, meta_encode({}))
  end
  super
end
size(style = nil) click to toggle source

Use meta info for style if required

Calls superclass method
# File lib/paperclip-meta/attachment.rb, line 22
def size(style = nil)
  style ? read_meta(style, :size) : super()
end
width(style = default_style) click to toggle source
# File lib/paperclip-meta/attachment.rb, line 30
def width(style = default_style)
  read_meta style, :width
end

Private Instance Methods

assign_meta() click to toggle source
# File lib/paperclip-meta/attachment.rb, line 46
def assign_meta
  return unless instance.respond_to?(:"#{name}_meta=")
  meta = populate_meta(@queued_for_write)
  return if meta == {}

  write_meta(meta)
end
merge_existing_meta_hash(meta) click to toggle source

Retain existing meta values that will not be recalculated when reprocessing a subset of styles

# File lib/paperclip-meta/attachment.rb, line 93
def merge_existing_meta_hash(meta)
  return unless (original_meta = instance.send("#{name}_meta"))
  meta.reverse_merge! meta_decode(original_meta)
end
meta_decode(meta) click to toggle source

Return decoded metadata as Object

# File lib/paperclip-meta/attachment.rb, line 87
def meta_decode(meta)
  Marshal.load(Base64.decode64(meta))
end
meta_encode(meta) click to toggle source

Return encoded metadata as String

# File lib/paperclip-meta/attachment.rb, line 82
def meta_encode(meta)
  Base64.encode64(Marshal.dump(meta))
end
populate_meta(queue) click to toggle source
# File lib/paperclip-meta/attachment.rb, line 54
def populate_meta(queue)
  meta = {}
  queue.each do |style, file|
    begin
      geo = Geometry.from_file file
      meta[style] = { width: geo.width.to_i, height: geo.height.to_i, size: file.size }
    rescue Paperclip::Errors::NotIdentifiedByImageMagickError
      meta[style] = {}
    end
  end
  meta
end
read_meta(style, item) click to toggle source

Return meta data for given style

# File lib/paperclip-meta/attachment.rb, line 73
def read_meta(style, item)
  if instance.respond_to?(:"#{name}_meta") && instance_read(:meta)
    if (meta = meta_decode(instance_read(:meta)))
      meta[style] && meta[style][item]
    end
  end
end
write_meta(meta) click to toggle source
# File lib/paperclip-meta/attachment.rb, line 67
def write_meta(meta)
  merge_existing_meta_hash meta
  instance.send("#{name}_meta=", meta_encode(meta))
end