Class: Thumbo::Proxy
- Inherits:
-
Object
- Object
- Thumbo::Proxy
- Defined in:
- lib/thumbo/proxy.rb
Instance Attribute Summary collapse
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Instance Method Summary collapse
-
#convert_format_for_website ⇒ Object
convert format to website displable image format.
-
#create ⇒ Object
create thumbnails in the image list (Magick::ImageList).
- #delete ⇒ Object
-
#dimension(img = image.first) ⇒ Object
attribute.
- #fileext ⇒ Object
-
#filename ⇒ Object
owner delegate.
-
#from_blob(blob, &block) ⇒ Object
e.g., thumbnails.from_blob uploaded_file.read.
-
#image ⇒ Object
image processing.
- #image=(new_image) ⇒ Object
-
#image? ⇒ Boolean
check if image exists in memory.
- #image_with_timeout(time_limit = 5) ⇒ Object
-
#initialize(owner, title) ⇒ Proxy
constructor
A new instance of Proxy.
-
#method_missing(msg, *args, &block) ⇒ Object
delegate all.
- #mime_type ⇒ Object
- #paths ⇒ Object
-
#release ⇒ Object
is this helpful or not?.
-
#storage ⇒ Object
storage related.
- #to_blob(&block) ⇒ Object
- #uri ⇒ Object
- #write(filename = nil, &block) ⇒ Object
Constructor Details
#initialize(owner, title) ⇒ Proxy
Returns a new instance of Proxy.
7 8 9 10 |
# File 'lib/thumbo/proxy.rb', line 7 def initialize owner, title @owner, @title = owner, title @image = nil # please stop warning me @image is not defined end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(msg, *args, &block) ⇒ Object
delegate all
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/thumbo/proxy.rb', line 77 def method_missing msg, *args, &block raise 'fetch image first if you want to operate the image' unless @image if image.__respond_to__?(msg) # operate ImageList, a dirty way because of RMagick... [image.__send__(msg, *args, &block)] elsif image.first.respond_to?(msg) # operate each Image in ImageList image.to_a.map{ |layer| layer.__send__(msg, *args, &block) } else # no such method... super(msg, *args, &block) end end |
Instance Attribute Details
#title ⇒ Object (readonly)
Returns the value of attribute title.
6 7 8 |
# File 'lib/thumbo/proxy.rb', line 6 def title @title end |
Instance Method Details
#convert_format_for_website ⇒ Object
convert format to website displable image format
50 51 52 |
# File 'lib/thumbo/proxy.rb', line 50 def convert_format_for_website image.format = 'PNG' unless ['GIF', 'JPEG'].include?(image.format) end |
#create ⇒ Object
create thumbnails in the image list (Magick::ImageList)
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/thumbo/proxy.rb', line 55 def create return if title == :original release limit = owner.class.thumbo_common[title] if limit create_common(limit) else limit = owner.class.thumbo_square[title] create_square(limit) end self end |
#delete ⇒ Object
101 102 103 |
# File 'lib/thumbo/proxy.rb', line 101 def delete storage.delete(filename) end |
#dimension(img = image.first) ⇒ Object
attribute
115 116 117 |
# File 'lib/thumbo/proxy.rb', line 115 def dimension img = image.first [img.columns, img.rows] end |
#fileext ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/thumbo/proxy.rb', line 123 def fileext if @image case ext = image.first.format when 'PNG8'; 'png' when 'PNG24'; 'png' when 'PNG32'; 'png' when 'GIF87'; 'gif' when 'JPEG'; 'jpg' when 'PJPEG'; 'jpg' when 'BMP2'; 'bmp' when 'BMP3'; 'bmp' when 'TIFF64'; 'tiff' else; ext.downcase end elsif owner.respond_to?(:thumbo_default_fileext) owner.thumbo_default_fileext else raise "please implement #{owner.class}\#thumbo_default_fileext or Thumbo can't guess the file extension" end end |
#filename ⇒ Object
owner delegate
106 107 108 |
# File 'lib/thumbo/proxy.rb', line 106 def filename owner.thumbo_filename self end |
#from_blob(blob, &block) ⇒ Object
e.g., thumbnails.from_blob uploaded_file.read
40 41 42 43 |
# File 'lib/thumbo/proxy.rb', line 40 def from_blob blob, &block self.image = Magick::ImageList.new.from_blob(blob, &block) self end |
#image ⇒ Object
image processing
13 14 15 |
# File 'lib/thumbo/proxy.rb', line 13 def image @image || (self.image = read_image) end |
#image=(new_image) ⇒ Object
26 27 28 29 |
# File 'lib/thumbo/proxy.rb', line 26 def image= new_image release @image = new_image end |
#image? ⇒ Boolean
check if image exists in memory
18 19 20 |
# File 'lib/thumbo/proxy.rb', line 18 def image? @image end |
#image_with_timeout(time_limit = 5) ⇒ Object
22 23 24 |
# File 'lib/thumbo/proxy.rb', line 22 def image_with_timeout time_limit = 5 @image || (self.image = read_image_with_timeout(time_limit)) end |
#mime_type ⇒ Object
119 120 121 |
# File 'lib/thumbo/proxy.rb', line 119 def mime_type image.first.mime_type end |
#paths ⇒ Object
97 98 99 |
# File 'lib/thumbo/proxy.rb', line 97 def paths storage.paths(filename) end |
#release ⇒ Object
is this helpful or not?
32 33 34 35 36 |
# File 'lib/thumbo/proxy.rb', line 32 def release @image = nil GC.start self end |
#storage ⇒ Object
storage related
93 94 95 |
# File 'lib/thumbo/proxy.rb', line 93 def storage owner.class.thumbo_storage end |
#to_blob(&block) ⇒ Object
45 46 47 |
# File 'lib/thumbo/proxy.rb', line 45 def to_blob &block self.image.to_blob(&block) end |
#uri ⇒ Object
110 111 112 |
# File 'lib/thumbo/proxy.rb', line 110 def uri owner.thumbo_uri self end |
#write(filename = nil, &block) ⇒ Object
72 73 74 |
# File 'lib/thumbo/proxy.rb', line 72 def write filename = nil, &block storage.write(filename || self.filename, to_blob(&block)) end |