Class: Sevgi::Graphics::Margin

Inherits:
Data
  • Object
show all
Includes:
Comparable
Defined in:
lib/sevgi/graphics/auxiliary/margin.rb

Overview

Four-sided margin with CSS-like shorthand normalization.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(top: nil, right: nil, bottom: nil, left: nil) ⇒ void

Creates a margin from one to four shorthand values.

Parameters:

  • top (Numeric, nil) (defaults to: nil)

    top value or all-sides shorthand

  • right (Numeric, nil) (defaults to: nil)

    right value or horizontal shorthand

  • bottom (Numeric, nil) (defaults to: nil)

    bottom value

  • left (Numeric, nil) (defaults to: nil)

    left value



24
25
26
# File 'lib/sevgi/graphics/auxiliary/margin.rb', line 24

def initialize(top: nil, right: nil, bottom: nil, left: nil)
  super(**normalize(top, right, bottom, left))
end

Instance Attribute Details

#bottomFloat (readonly)

Returns bottom margin.

Returns:

  • (Float)

    bottom margin



6
7
8
# File 'lib/sevgi/graphics/auxiliary/margin.rb', line 6

def bottom
  @bottom
end

#leftFloat (readonly)

Returns left margin.

Returns:

  • (Float)

    left margin



6
7
8
# File 'lib/sevgi/graphics/auxiliary/margin.rb', line 6

def left
  @left
end

#rightFloat (readonly)

Returns right margin.

Returns:

  • (Float)

    right margin



6
7
8
# File 'lib/sevgi/graphics/auxiliary/margin.rb', line 6

def right
  @right
end

#topFloat (readonly)

Returns top margin.

Returns:

  • (Float)

    top margin



6
7
8
# File 'lib/sevgi/graphics/auxiliary/margin.rb', line 6

def top
  @top
end

Class Method Details

.margin(array) ⇒ Sevgi::Graphics::Margin

Builds a margin from an array-like shorthand.

Parameters:

  • array (Object)

    value converted with Array()

Returns:



62
63
64
65
66
# File 'lib/sevgi/graphics/auxiliary/margin.rb', line 62

def self.margin(array)
  self[
    *(array = Array(array)[0...(size = Margin.members.size)]).fill(nil, array.size, size - array.size)
  ]
end

.zeroSevgi::Graphics::Margin

Returns a zero margin.



70
# File 'lib/sevgi/graphics/auxiliary/margin.rb', line 70

def self.zero = (@zero ||= self[0.0, 0.0, 0.0, 0.0])

Instance Method Details

#<=>(other) ⇒ Integer?

Compares margins by top, right, bottom, then left.

Parameters:

Returns:

  • (Integer, nil)


31
# File 'lib/sevgi/graphics/auxiliary/margin.rb', line 31

def <=>(other) = deconstruct <=> other.deconstruct

#adjust(h, v) ⇒ Sevgi::Graphics::Margin

Returns a margin inflated horizontally and vertically.

Parameters:

  • h (Numeric)

    horizontal addition

  • v (Numeric)

    vertical addition

Returns:



37
# File 'lib/sevgi/graphics/auxiliary/margin.rb', line 37

def adjust(h, v) = self.class[top + v, right + h, bottom + v, left + h]

#eql?(other) ⇒ Boolean Also known as: ==

Reports strict margin equality.

Parameters:

  • other (Object)

    object to compare

Returns:

  • (Boolean)


42
# File 'lib/sevgi/graphics/auxiliary/margin.rb', line 42

def eql?(other) = self.class == other.class && deconstruct == other.deconstruct

#hashInteger

Returns a hash compatible with strict equality.

Returns:

  • (Integer)


46
# File 'lib/sevgi/graphics/auxiliary/margin.rb', line 46

def hash = [self.class, *deconstruct].hash

#horizontalFloat

Returns total horizontal margin.

Returns:

  • (Float)


50
# File 'lib/sevgi/graphics/auxiliary/margin.rb', line 50

def horizontal = left + right

#verticalFloat

Returns total vertical margin.

Returns:

  • (Float)


54
# File 'lib/sevgi/graphics/auxiliary/margin.rb', line 54

def vertical = top + bottom