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. Values must be finite real numbers greater than or equal to zero.

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

Raises:

  • (Sevgi::ArgumentError)

    when a supplied margin is not a finite, non-negative real number



26
27
28
# File 'lib/sevgi/graphics/auxiliary/margin.rb', line 26

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:



67
68
69
70
71
# File 'lib/sevgi/graphics/auxiliary/margin.rb', line 67

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.



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

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)


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

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:



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

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)


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

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

#hashInteger

Returns a hash compatible with strict equality.

Returns:

  • (Integer)


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

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

#horizontalFloat

Returns total horizontal margin.

Returns:

  • (Float)


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

def horizontal = left + right

#verticalFloat

Returns total vertical margin.

Returns:

  • (Float)


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

def vertical = top + bottom