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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/thumbo/proxy.rb', line 72

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



45
46
47
# File 'lib/thumbo/proxy.rb', line 45

def convert_format_for_website
  image.format = 'PNG' unless ['GIF', 'JPEG'].include?(image.format)
end

#createObject

create thumbnails in the image list (Magick::ImageList)



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/thumbo/proxy.rb', line 50

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



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

def delete
  storage.delete(filename)
end

#dimension(img = image.first) ⇒ Object

attribute



110
111
112
# File 'lib/thumbo/proxy.rb', line 110

def dimension img = image.first
  [img.columns, img.rows]
end

#fileextObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/thumbo/proxy.rb', line 118

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



101
102
103
# File 'lib/thumbo/proxy.rb', line 101

def filename
  owner.thumbo_filename self
end

#from_blob(blob, &block) ⇒ Object

e.g., thumbnails.from_blob uploaded_file.read



35
36
37
38
# File 'lib/thumbo/proxy.rb', line 35

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



21
22
23
24
# File 'lib/thumbo/proxy.rb', line 21

def image= new_image
  release
  @image = new_image
end

#image_with_timeout(time_limit = 5) ⇒ Object



17
18
19
# File 'lib/thumbo/proxy.rb', line 17

def image_with_timeout time_limit = 5
  @image || (self.image = read_image_with_timeout(time_limit))
end

#mime_typeObject



114
115
116
# File 'lib/thumbo/proxy.rb', line 114

def mime_type
  image.first.mime_type
end

#pathsObject



92
93
94
# File 'lib/thumbo/proxy.rb', line 92

def paths
  storage.paths(filename)
end

#releaseObject

is this helpful or not?



27
28
29
30
31
# File 'lib/thumbo/proxy.rb', line 27

def release
  @image = nil
  GC.start
  self
end

#storageObject

storage related



88
89
90
# File 'lib/thumbo/proxy.rb', line 88

def storage
  owner.class.thumbo_storage
end

#to_blobObject



40
41
42
# File 'lib/thumbo/proxy.rb', line 40

def to_blob
  self.image.to_blob
end

#uriObject



105
106
107
# File 'lib/thumbo/proxy.rb', line 105

def uri
  owner.thumbo_uri self
end

#writeObject



67
68
69
# File 'lib/thumbo/proxy.rb', line 67

def write
  storage.write(self.filename, to_blob)
end