Class: Thumbo::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/thumbo/proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#titleObject (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_websiteObject

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

#createObject

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

#deleteObject



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

#fileextObject



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

#filenameObject

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

#imageObject

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

Returns:

  • (Boolean)


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_typeObject



119
120
121
# File 'lib/thumbo/proxy.rb', line 119

def mime_type
  image.first.mime_type
end

#pathsObject



97
98
99
# File 'lib/thumbo/proxy.rb', line 97

def paths
  storage.paths(filename)
end

#releaseObject

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

#storageObject

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

#uriObject



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