Class: Fireimg::Size

Inherits:
Object
  • Object
show all
Defined in:
lib/fireimg/size.rb

Overview

A variant to pre-generate after upload. Matches FireImg sizes JSON (width/height, quality, fmt, fit, pos, fill).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width: nil, height: nil, quality: nil, fmt: nil, fit: nil, pos: nil, fill: nil) ⇒ Size

Returns a new instance of Size.



9
10
11
12
13
14
15
16
17
# File 'lib/fireimg/size.rb', line 9

def initialize(width: nil, height: nil, quality: nil, fmt: nil, fit: nil, pos: nil, fill: nil)
  @width = width
  @height = height
  @quality = quality
  @fmt = fmt
  @fit = fit
  @pos = pos
  @fill = fill
end

Instance Attribute Details

#fillObject (readonly)

Returns the value of attribute fill.



7
8
9
# File 'lib/fireimg/size.rb', line 7

def fill
  @fill
end

#fitObject (readonly)

Returns the value of attribute fit.



7
8
9
# File 'lib/fireimg/size.rb', line 7

def fit
  @fit
end

#fmtObject (readonly)

Returns the value of attribute fmt.



7
8
9
# File 'lib/fireimg/size.rb', line 7

def fmt
  @fmt
end

#heightObject (readonly)

Returns the value of attribute height.



7
8
9
# File 'lib/fireimg/size.rb', line 7

def height
  @height
end

#posObject (readonly)

Returns the value of attribute pos.



7
8
9
# File 'lib/fireimg/size.rb', line 7

def pos
  @pos
end

#qualityObject (readonly)

Returns the value of attribute quality.



7
8
9
# File 'lib/fireimg/size.rb', line 7

def quality
  @quality
end

#widthObject (readonly)

Returns the value of attribute width.



7
8
9
# File 'lib/fireimg/size.rb', line 7

def width
  @width
end

Class Method Details

.from(value) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fireimg/size.rb', line 19

def self.from(value)
  case value
  when Size
    value
  when Hash
    h = value.transform_keys { |k| k.to_s.tr('-', '_') }
    new(
      width: h['width'],
      height: h['height'],
      quality: h['quality'],
      fmt: h['fmt'] || h['format'],
      fit: h['fit'],
      pos: h['pos'] || h['position'],
      fill: h['fill']
    )
  else
    raise ArgumentError, "expected Fireimg::Size or Hash, got #{value.class}"
  end
end

Instance Method Details

#to_hObject



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fireimg/size.rb', line 39

def to_h
  out = {}
  out[:width] = Integer(width) if width && Integer(width) > 0
  out[:height] = Integer(height) if height && Integer(height) > 0
  out[:quality] = quality.to_s unless quality.nil? || quality.to_s.empty?
  out[:fmt] = fmt.to_s unless fmt.nil? || fmt.to_s.empty?
  out[:fit] = fit.to_s unless fit.nil? || fit.to_s.empty?
  out[:pos] = pos.to_s unless pos.nil? || pos.to_s.empty?
  out[:fill] = fill.to_s unless fill.nil? || fill.to_s.empty?
  out
end