class Squib::Margin

Attributes

bottom[R]
left[R]
right[R]
top[R]

Public Class Methods

new(definition) click to toggle source

Create a new margin definition.

Takes definition which can either be a space-separated String or an Array of Float and will translate it to the top, right, bottom and left members.

The syntax follows how CSS parses margin shorthand strings.

# File lib/squib/commands/data/template_option.rb, line 16
def initialize(definition)
  if definition.instance_of? String
    @top, @right, @bottom, @left = expand_shorthand(
      definition.split(/\s+/).map!(&:to_f))
  elsif definition.is_a? Numeric
    @top, @right, @bottom, @left = expand_shorthand [definition]
  elsif definition.instance_of? Array
    @top, @right, @bottom, @left = expand_shorthand definition
  else
    raise ArgumentError, 'Invalid value, must be either string or array'
  end
end

Private Instance Methods

expand_shorthand(margin_arr) click to toggle source

Map out the margin array.

Takes margin_arr and attempt to expand it to a strict [top, right, bottom, left] array.

# File lib/squib/commands/data/template_option.rb, line 34
        def expand_shorthand(margin_arr)
  if margin_arr.size == 1
    all = margin_arr[0]
    [all, all, all, all]
  elsif margin_arr.size == 2
    margin_arr + margin_arr
  elsif margin_arr.size == 3
    margin_arr + [margin_arr[1]]
  elsif margin_arr.size >= 4
    margin_arr[0..3]
  else
    raise ArgumentError, 'Invalid array'
  end
end