Class: Artwork::Thumbnail
- Inherits:
-
Object
- Object
- Artwork::Thumbnail
- Includes:
- Comparable
- Defined in:
- lib/artwork/thumbnail.rb
Direct Known Subclasses
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
-
#aspect_ratio ⇒ Object
Returns the value of attribute aspect_ratio.
-
#height ⇒ Object
Returns the value of attribute height.
-
#label ⇒ Object
Returns the value of attribute label.
-
#name ⇒ Object
Returns the value of attribute name.
-
#retina ⇒ Object
writeonly
Sets the attribute retina.
-
#width ⇒ Object
Returns the value of attribute width.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #compatible? ⇒ Boolean
- #eq(other) ⇒ Object (also: #==)
-
#initialize(name:, width: nil, height: nil, label: nil, retina: false) ⇒ Thumbnail
constructor
A new instance of Thumbnail.
- #is_like?(other) ⇒ Boolean
- #retina? ⇒ Boolean
- #same_aspect_ratio_with?(other_thumb) ⇒ Boolean
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_ratio ⇒ Object
Returns the value of attribute aspect_ratio.
50 51 52 |
# File 'lib/artwork/thumbnail.rb', line 50 def aspect_ratio @aspect_ratio end |
#height ⇒ Object
Returns the value of attribute height.
50 51 52 |
# File 'lib/artwork/thumbnail.rb', line 50 def height @height end |
#label ⇒ Object
Returns the value of attribute label.
50 51 52 |
# File 'lib/artwork/thumbnail.rb', line 50 def label @label end |
#name ⇒ Object
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
51 52 53 |
# File 'lib/artwork/thumbnail.rb', line 51 def retina=(value) @retina = value end |
#width ⇒ Object
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
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
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
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
57 58 59 |
# File 'lib/artwork/thumbnail.rb', line 57 def retina? @retina end |
#same_aspect_ratio_with?(other_thumb) ⇒ 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 |