class Voom::Presenters::DSL::Components::Image

Constants

DEFAULT_POSITION
VALID_FIT_TYPES

Attributes

border[RW]
border_color[RW]
border_radius[RW]
description[RW]
fit[RW]
height[RW]
image[RW]
max_height[RW]
max_width[RW]
min_height[RW]
min_width[RW]
position[RW]
url[RW]
width[RW]

Public Class Methods

new(**attribs_, &block) click to toggle source
# File lib/voom/presenters/dsl/components/image.rb, line 26
def initialize(**attribs_, &block)
  super(type: :image, **attribs_, &block)

  @image = attribs.delete(:image)
  @description = attribs.delete(:description)

  @min_width = validate_size(attribs.delete(:min_width))
  @width = validate_size(attribs.delete(:width))
  @max_width = validate_size(attribs.delete(:max_width))

  @min_height = validate_size(attribs.delete(:min_height))
  @height = validate_size(attribs.delete(:height))
  @max_height = validate_size(attribs.delete(:max_height))

  @border = attribs.delete(:border)
  @border_color = attribs.delete(:border_color) { :primary }
  @border_radius = attribs.delete(:border_radius)

  @fit = validate_fit(attribs.delete(:fit) { :contain })
  @position = Array(attribs.delete(:position) { DEFAULT_POSITION }).compact

  @url = build_url

  expand!
end

Private Instance Methods

build_url() click to toggle source
# File lib/voom/presenters/dsl/components/image.rb, line 54
def build_url
  return unless image
  return image if image.start_with?('/') || image.start_with?('http')

  @parent.router.url(render: image, context: {})
end
validate_fit(fit) click to toggle source
# File lib/voom/presenters/dsl/components/image.rb, line 61
def validate_fit(fit)
  return unless fit

  fit = fit.to_sym

  unless VALID_FIT_TYPES.include?(fit)
    raise Errors::ParameterValidation, "Invalid image fit specified: #{fit}"
  end

  fit
end
validate_size(value) click to toggle source

Ensure all size values have a unit, defaulting to pixels.

# File lib/voom/presenters/dsl/components/image.rb, line 74
def validate_size(value)
  return unless value
  return "#{value}px" if value.to_s.match?(/\A\d+\Z/)

  value
end