class Imgproxy::Options

Formats and regroups processing options

Constants

CASTERS
META

Public Class Methods

new(options) click to toggle source

@param options [Hash] raw processing options

# File lib/imgproxy/options.rb, line 72
def initialize(options)
  # Options order hack: initialize known and meta options with nil value to preserve order
  CASTERS.each_key { |n| self[n] = nil if options.key?(n) || META.include?(n) }

  options.each do |name, value|
    caster = CASTERS[name]
    self[name] = caster ? caster.cast(value) : unwrap_hash(value)
  end

  group_resizing_opts
  group_adjust_opts

  compact!
end

Private Instance Methods

extract_and_trim_nils(*keys) click to toggle source
# File lib/imgproxy/options.rb, line 111
def extract_and_trim_nils(*keys)
  keys.map { |k| delete(k) }.trim!
end
group_adjust_opts() click to toggle source
# File lib/imgproxy/options.rb, line 104
def group_adjust_opts
  return if self[:adjust]
  return unless values_at(:brightness, :contrast, :saturation).count { |o| o } > 1

  self[:adjust] = extract_and_trim_nils(:brightness, :contrast, :saturation)
end
group_resizing_opts() click to toggle source
# File lib/imgproxy/options.rb, line 97
def group_resizing_opts
  return unless self[:width] && self[:height] && !self[:size] && !self[:resize]

  self[:size] = extract_and_trim_nils(:width, :height, :enlarge, :extend)
  self[:resize] = [delete(:resizing_type), *delete(:size)] if self[:resizing_type]
end
unwrap_hash(raw) click to toggle source
# File lib/imgproxy/options.rb, line 89
def unwrap_hash(raw)
  return raw unless raw.is_a?(Hash)

  raw.flat_map do |_key, val|
    unwrap_hash(val)
  end
end