Class: Artwork::DesiredThumbnail

Inherits:
Thumbnail show all
Defined in:
lib/artwork/desired_thumbnail.rb

Constant Summary

Constants inherited from Thumbnail

Thumbnail::STYLE_PATTERN

Instance Attribute Summary

Attributes inherited from Thumbnail

#aspect_ratio, #height, #label, #name, #retina, #width

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Thumbnail

#<=>, #compatible?, #eq, #is_like?, #same_aspect_ratio_with?

Constructor Details

#initialize(data = {}) ⇒ DesiredThumbnail

Returns a new instance of DesiredThumbnail.



28
29
30
31
32
33
34
35
36
# File 'lib/artwork/desired_thumbnail.rb', line 28

def initialize(data = {})
  data = data.dup

  @base_resolution = data.delete(:base_resolution)

  data[:name] = "#{data[:width]}x" if data[:name].nil? and data[:width]

  super(data)
end

Class Method Details

.compatible?(style) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/artwork/desired_thumbnail.rb', line 23

def compatible?(style)
  from_style(style).compatible?
end

.from_style(style) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/artwork/desired_thumbnail.rb', line 4

def from_style(style)
  normal_style, base_resolution = style.to_s.split('@')

  data = {}

  if match = normal_style.match(Thumbnail::STYLE_PATTERN) and match[:retina_flag].nil?
    data[:name]   = match[:name]
    data[:width]  = match[:width].to_i
    data[:height] = match[:height].to_i
    data[:label]  = match[:label] ? match[:label].gsub(/^_|_$/, '') : nil
  else
    data[:name] = normal_style.to_s
  end

  data[:base_resolution] = base_resolution.to_i if base_resolution

  new(data)
end

Instance Method Details

#base_resolutionObject



43
44
45
# File 'lib/artwork/desired_thumbnail.rb', line 43

def base_resolution
  @base_resolution ||= Artwork.base_resolution
end

#base_resolution=(value) ⇒ Object



47
48
49
50
# File 'lib/artwork/desired_thumbnail.rb', line 47

def base_resolution=(value)
  reset_cached_widths
  @base_resolution = value
end

#expected_widthObject



64
65
66
67
68
69
70
# File 'lib/artwork/desired_thumbnail.rb', line 64

def expected_width
  return @expected_width if @expected_width

  result = width_in_current_resolution

  @expected_width = retina? ? result * 2 : result
end

#retina?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/artwork/desired_thumbnail.rb', line 52

def retina?
  Artwork.load_2x_images?
end

#width=(value) ⇒ Object



38
39
40
41
# File 'lib/artwork/desired_thumbnail.rb', line 38

def width=(value)
  reset_cached_widths
  super
end

#width_in_current_resolutionObject



56
57
58
59
60
61
62
# File 'lib/artwork/desired_thumbnail.rb', line 56

def width_in_current_resolution
  return @width_in_current_resolution if @width_in_current_resolution

  resolution_ratio = base_resolution.to_f / Artwork.current_resolution.to_f

  @width_in_current_resolution = width.to_f / resolution_ratio
end