Class: Artwork::Thumbnail

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

Constant Summary collapse

NAME_PATTERN =
/^(\d+)x(\d+)?((?!_2x)_\w*?)?(_2x)?$/i.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Thumbnail

Returns a new instance of Thumbnail.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/artwork/thumbnail.rb', line 13

def initialize(name)
  @name = name.to_s

  if match = @name.match(NAME_PATTERN)
    @width       = match[1].to_i
    @height      = match[2].to_i
    @label       = match[3] ? match[3].gsub(/^_|_$/, '') : nil
    @retina_flag = match[4]
  end

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

Instance Attribute Details

#aspect_ratioObject (readonly)

Returns the value of attribute aspect_ratio.



11
12
13
# File 'lib/artwork/thumbnail.rb', line 11

def aspect_ratio
  @aspect_ratio
end

#heightObject (readonly)

Returns the value of attribute height.



9
10
11
# File 'lib/artwork/thumbnail.rb', line 9

def height
  @height
end

#labelObject (readonly)

Returns the value of attribute label.



10
11
12
# File 'lib/artwork/thumbnail.rb', line 10

def label
  @label
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/artwork/thumbnail.rb', line 7

def name
  @name
end

#widthObject (readonly)

Returns the value of attribute width.



8
9
10
# File 'lib/artwork/thumbnail.rb', line 8

def width
  @width
end

Class Method Details

.compatible?(name) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/artwork/thumbnail.rb', line 55

def self.compatible?(name)
  name.to_s =~ NAME_PATTERN ? true : false
end

Instance Method Details

#<=>(other_thumb) ⇒ Object



41
42
43
# File 'lib/artwork/thumbnail.rb', line 41

def <=>(other_thumb)
  width <=> other_thumb.width
end

#compatible?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/artwork/thumbnail.rb', line 27

def compatible?
  not width.nil?
end

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



45
46
47
48
49
50
51
# File 'lib/artwork/thumbnail.rb', line 45

def eq(other)
  name    == other.name and \
  width   == other.width and \
  height  == other.height and \
  label   == other.label and \
  retina? == other.retina?
end

#retina?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/artwork/thumbnail.rb', line 31

def retina?
  @retina_flag == '_2x'
end

#same_aspect_ratio_with?(other_thumb) ⇒ Boolean

Returns:

  • (Boolean)


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

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