Class: Sevgi::Graphics::Margin

Inherits:
Data
  • Object
show all
Includes:
Comparable
Defined in:
lib/sevgi/graphics/auxiliary/margin.rb,
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



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

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



19
20
21
# File 'lib/sevgi/graphics/auxiliary/margin.rb', line 19

def bottom
  @bottom
end

#leftFloat (readonly)

Returns left margin.

Returns:

  • (Float)

    left margin



19
20
21
# File 'lib/sevgi/graphics/auxiliary/margin.rb', line 19

def left
  @left
end

#rightFloat (readonly)

Returns right margin.

Returns:

  • (Float)

    right margin



19
20
21
# File 'lib/sevgi/graphics/auxiliary/margin.rb', line 19

def right
  @right
end

#topFloat (readonly)

Returns top margin.

Returns:

  • (Float)

    top margin



19
20
21
# File 'lib/sevgi/graphics/auxiliary/margin.rb', line 19

def top
  @top
end

Class Method Details

.[](top = nil, right = nil, bottom = nil, left = nil) ⇒ Sevgi::Graphics::Margin

Creates a margin from one to four CSS-like shorthand values.

Examples:

Create a vertical and horizontal margin

Sevgi::Graphics::Margin[5, 10]

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

Returns:

Raises:

  • (Sevgi::ArgumentError)

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



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/sevgi/graphics/auxiliary/margin.rb', line 19

Margin = Data.define(:top, :right, :bottom, :left) do
  include Comparable

  # @!attribute [r] top
  #   @return [Float] top margin
  # @!attribute [r] right
  #   @return [Float] right margin
  # @!attribute [r] bottom
  #   @return [Float] bottom margin
  # @!attribute [r] left
  #   @return [Float] left margin

  # Creates a margin from one to four shorthand values. Values must be finite real numbers greater than or equal to
  # zero.
  # @param top [Numeric, nil] top value or all-sides shorthand
  # @param right [Numeric, nil] right value or horizontal shorthand
  # @param bottom [Numeric, nil] bottom value
  # @param left [Numeric, nil] left value
  # @return [void]
  # @raise [Sevgi::ArgumentError] when a supplied margin is not a finite, non-negative real number
  def initialize(top: nil, right: nil, bottom: nil, left: nil)
    super(**normalize(top, right, bottom, left))
  end

  # Compares margins by top, right, bottom, then left.
  # @param other [Object] margin to compare
  # @return [Integer, nil] comparison result, or nil for a non-Margin operand
  def <=>(other)
    deconstruct <=> other.deconstruct if other.is_a?(self.class)
  end

  # Returns a margin inflated horizontally and vertically.
  # @param h [Numeric] horizontal addition
  # @param v [Numeric] vertical addition
  # @return [Sevgi::Graphics::Margin]
  # @raise [Sevgi::ArgumentError] when the adjusted margin is negative or not finite
  def adjust(h, v) = self.class[top + v, right + h, bottom + v, left + h]

  # Reports strict margin equality.
  # @param other [Object] object to compare
  # @return [Boolean]
  def eql?(other) = self.class == other.class && deconstruct == other.deconstruct

  # Returns a hash compatible with strict equality.
  # @return [Integer]
  def hash = [self.class, *deconstruct].hash

  # Returns total horizontal margin.
  # @return [Float]
  def horizontal = left + right

  # Returns total vertical margin.
  # @return [Float]
  def vertical = top + bottom

  alias_method :==, :eql?

  # Returns margin values in top, right, bottom, left order.
  # @return [Array<Float>]
  alias_method :to_a, :deconstruct

  # Returns a zero margin.
  # @return [Sevgi::Graphics::Margin]
  def self.zero = (@zero ||= self[0.0, 0.0, 0.0, 0.0])

  private

  def normalize(top, right, bottom, left)
    values = [top, right, bottom, left]

    Margin
      .members
      .zip(
        values_for(values.compact.size, values).map do |value|
          Scalar.finite(value, context: "margin", field: :value, nonnegative: true)
        end
      )
      .to_h
  end

  def values_for(size, values)
    top, right, bottom = values

    [
      [0, 0, 0, 0],
      [top, top, top, top],
      [top, right, top, right],
      [top, right, bottom, right]
    ][size] ||
      values
  end
end

.zeroSevgi::Graphics::Margin

Returns a zero margin.



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

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:

  • other (Object)

    margin to compare

Returns:

  • (Integer, nil)

    comparison result, or nil for a non-Margin operand



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

def <=>(other)
  deconstruct <=> other.deconstruct if other.is_a?(self.class)
end

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

Returns a margin inflated horizontally and vertically.

Parameters:

  • h (Numeric)

    horizontal addition

  • v (Numeric)

    vertical addition

Returns:

Raises:

  • (Sevgi::ArgumentError)

    when the adjusted margin is negative or not finite



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

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)


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

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

#hashInteger

Returns a hash compatible with strict equality.

Returns:

  • (Integer)


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

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

#horizontalFloat

Returns total horizontal margin.

Returns:

  • (Float)


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

def horizontal = left + right

#verticalFloat

Returns total vertical margin.

Returns:

  • (Float)


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

def vertical = top + bottom