Module: Plushie::Type::Padding

Defined in:
lib/plushie/type/padding.rb

Overview

Padding specification with per-side values.

Accepts a uniform number, [vertical, horizontal], [top, right, bottom, left], a Hash with :top/:right/:bottom/:left keys, or a Padding struct.

Examples:

Uniform

column(padding: 16)

Vertical/horizontal

column(padding: [8, 16])

Per-side

column(padding: { top: 8, bottom: 16 })

Four values

column(padding: [4, 8, 12, 16])

Defined Under Namespace

Classes: Pad

Constant Summary collapse

FIELD_KEYS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Recognized field keys for padding specs.

%i[top right bottom left].freeze

Class Method Summary collapse

Class Method Details

.cast(value) ⇒ Hash

Normalise any padding input to a canonical four-side hash.

Parameters:

  • value (Numeric, Array, Hash, Pad)

    the padding value

Returns:

  • (Hash)

    { top:, right:, bottom:, left: }



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/plushie/type/padding.rb', line 59

def cast(value)
  result =
    case value
    when Numeric
      {top: value, right: value, bottom: value, left: value}
    when Array
      case value.length
      when 2 then {top: value[0], right: value[1], bottom: value[0], left: value[1]}
      when 4 then {top: value[0], right: value[1], bottom: value[2], left: value[3]}
      else raise ArgumentError, "padding array must have 2 or 4 elements, got #{value.length}"
      end
    when Pad
      value.to_wire
    when Hash
      value.slice(:top, :right, :bottom, :left).compact
    else
      raise ArgumentError, "invalid padding: #{value.inspect}"
    end
  result.each do |side, n|
    if n.is_a?(Numeric) && n < 0
      raise ArgumentError, "padding must be non-negative, got #{side}=#{n}"
    end
  end
  result
end

.encode(value) ⇒ Numeric, Hash

Encode a padding value for the wire protocol.

Parameters:

  • value (Numeric, Array, Hash, Pad)

Returns:

  • (Numeric, Hash)


89
90
91
92
93
94
95
# File 'lib/plushie/type/padding.rb', line 89

def encode(value)
  case value
  when Numeric then value
  when Pad then cast(value)
  else cast(value)
  end
end

.from_opts(opts) ⇒ Pad

Construct a Padding struct from keyword options.

Parameters:

  • opts (Hash)

    :top, :right, :bottom, :left (all optional)

Returns:

Raises:

  • (ArgumentError)


49
50
51
52
53
# File 'lib/plushie/type/padding.rb', line 49

def from_opts(opts)
  unknown = opts.keys - FIELD_KEYS
  raise ArgumentError, "unknown padding fields: #{unknown.inspect}. Valid: #{FIELD_KEYS.inspect}" if unknown.any?
  Pad.new(**opts.slice(*FIELD_KEYS))
end