Class: Artwork::Thumbnail

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/artwork/thumbnail.rb

Direct Known Subclasses

DesiredThumbnail

Constant Summary collapse

STYLE_PATTERN =
/\A
  (?<name>
    (?<width>\d+)x(?<height>\d+)?
    (?<label>(?!_2x)_\w*?)?
    (?<retina_flag>_2x)?
  )
\z/ix.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, width: nil, height: nil, label: nil, retina: false) ⇒ Thumbnail

Returns a new instance of Thumbnail.



40
41
42
43
44
45
46
47
48
# File 'lib/artwork/thumbnail.rb', line 40

def initialize(name:, width: nil, height: nil, label: nil, retina: false)
  @name   = name
  @width  = width
  @height = height == 0 ? nil : height
  @label  = label
  @retina = retina

  @aspect_ratio = @width.to_f / @height if @height
end

Instance Attribute Details

#aspect_ratioObject

Returns the value of attribute aspect_ratio.



50
51
52
# File 'lib/artwork/thumbnail.rb', line 50

def aspect_ratio
  @aspect_ratio
end

#heightObject

Returns the value of attribute height.



50
51
52
# File 'lib/artwork/thumbnail.rb', line 50

def height
  @height
end

#labelObject

Returns the value of attribute label.



50
51
52
# File 'lib/artwork/thumbnail.rb', line 50

def label
  @label
end

#nameObject

Returns the value of attribute name.



50
51
52
# File 'lib/artwork/thumbnail.rb', line 50

def name
  @name
end

#retina=(value) ⇒ Object (writeonly)

Sets the attribute retina

Parameters:

  • value

    the value to set the attribute retina to.



51
52
53
# File 'lib/artwork/thumbnail.rb', line 51

def retina=(value)
  @retina = value
end

#widthObject

Returns the value of attribute width.



50
51
52
# File 'lib/artwork/thumbnail.rb', line 50

def width
  @width
end

Class Method Details

.compatible?(style) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/artwork/thumbnail.rb', line 35

def compatible?(style)
  style.to_s =~ STYLE_PATTERN ? true : false
end

.from_style(style) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/artwork/thumbnail.rb', line 14

def from_style(style)
  data = {}

  if match = style.to_s.match(STYLE_PATTERN)
    data[:name]   = match[:name]
    data[:width]  = match[:width].to_i
    data[:height] = match[:height].to_i
    data[:label]  = match[:label] ? match[:label].gsub(/^_|_$/, '') : nil
    data[:retina] = !!match[:retina_flag]

    if data[:retina]
      data[:width]  *= 2
      data[:height] *= 2
    end
  else
    data[:name] = style.to_s
  end

  new(data)
end

Instance Method Details

#<=>(other) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/artwork/thumbnail.rb', line 67

def <=>(other)
  if self.height.nil? and other.height.nil?
    (self.width || -1) <=> (other.width || -1)
  elsif !self.height.nil? and !other.height.nil?
    result = (self.width || -1) <=> (other.width || -1)

    if result == 0
      result = self.height <=> other.height
    end

    result
  elsif self.height.nil?
    -1
  else
    1
  end
end

#compatible?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/artwork/thumbnail.rb', line 53

def compatible?
  not width.nil?
end

#eq(other) ⇒ Object Also known as: ==



85
86
87
88
89
90
91
# File 'lib/artwork/thumbnail.rb', line 85

def eq(other)
  self.name == other.name &&
    self.width   == other.width &&
    self.height  == other.height &&
    self.label   == other.label &&
    self.retina? == other.retina?
end

#is_like?(other) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
98
# File 'lib/artwork/thumbnail.rb', line 95

def is_like?(other)
  self.label == other.label &&
    (self.aspect_ratio.nil? || self.same_aspect_ratio_with?(other))
end

#retina?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/artwork/thumbnail.rb', line 57

def retina?
  @retina
end

#same_aspect_ratio_with?(other_thumb) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
# File 'lib/artwork/thumbnail.rb', line 61

def same_aspect_ratio_with?(other_thumb)
  return unless aspect_ratio and other_thumb.aspect_ratio

  (0.0..0.1).include? (aspect_ratio - other_thumb.aspect_ratio).abs
end