Module: Imgproxy::OptionsCasters::Padding

Defined in:
lib/imgproxy/options_casters/padding.rb

Overview

Casts ‘padding` info option

Constant Summary collapse

CASTER =
Imgproxy::OptionsCasters::Group.new(
  top: Imgproxy::OptionsCasters::Integer,
  right: Imgproxy::OptionsCasters::Integer,
  bottom: Imgproxy::OptionsCasters::Integer,
  left: Imgproxy::OptionsCasters::Integer,
).freeze

Class Method Summary collapse

Class Method Details

.cast(raw) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/imgproxy/options_casters/padding.rb', line 17

def self.cast(raw)
  # Allow padding to be just a number
  return Imgproxy::OptionsCasters::Integer.cast(raw) if raw.is_a?(Numeric)

  return raw.map { |v| Imgproxy::OptionsCasters::Integer.cast(v) } if raw.is_a?(::Array)

  return raw unless raw.is_a?(Hash)

  compact CASTER.cast(raw)
end

.compact(values) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/imgproxy/options_casters/padding.rb', line 28

def self.compact(values)
  if values[0] == values[1] && values[0] == values[2] && values[0] == values[3]
    return values[0]
  end

  return values[0, 2] if values[0] == values[2] && values[1] == values[3]

  return values[0, 3] if values[1] == values[3]

  values
end