Class: Sumi::Graphics::Margin

Inherits:
Data
  • Object
show all
Includes:
Comparable
Defined in:
graphics/lib/sumi/graphics/auxiliary/margin.rb,
graphics/lib/sumi/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: Undefined, right: Undefined, bottom: Undefined, left: Undefined) ⇒ 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, Sumi::Undefined) (defaults to: Undefined) —

    top value or all-sides shorthand

  • right (Numeric, Sumi::Undefined) (defaults to: Undefined) —

    right value or horizontal shorthand

  • bottom (Numeric, Sumi::Undefined) (defaults to: Undefined) —

    bottom value

  • left (Numeric, Sumi::Undefined) (defaults to: Undefined) —

    left value

Raises:



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

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

Instance Attribute Details

#bottom ⇒ Float (readonly)

Returns bottom margin.

Returns:

  • (Float) —

    bottom margin



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

def bottom
  @bottom
end

#left ⇒ Float (readonly)

Returns left margin.

Returns:

  • (Float) —

    left margin



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

def left
  @left
end

#right ⇒ Float (readonly)

Returns right margin.

Returns:

  • (Float) —

    right margin



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

def right
  @right
end

#top ⇒ Float (readonly)

Returns top margin.

Returns:

  • (Float) —

    top margin



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

def top
  @top
end

Class Method Details

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

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

Examples:

Create a vertical and horizontal margin

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

Parameters:

  • top (Numeric, Sumi::Undefined) (defaults to: nil) —

    top value or all-sides shorthand

  • right (Numeric, Sumi::Undefined) (defaults to: nil) —

    right value or horizontal shorthand

  • bottom (Numeric, Sumi::Undefined) (defaults to: nil) —

    bottom value

  • left (Numeric, Sumi::Undefined) (defaults to: nil) —

    left value

Returns:

Raises:



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
111
# File 'graphics/lib/sumi/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, Sumi::Undefined] top value or all-sides shorthand
  # @param right [Numeric, Sumi::Undefined] right value or horizontal shorthand
  # @param bottom [Numeric, Sumi::Undefined] bottom value
  # @param left [Numeric, Sumi::Undefined] left value
  # @return [void]
  # @raise [Sumi::ArgumentError] when a supplied margin is not a finite, non-negative real number
  def initialize(top: Undefined, right: Undefined, bottom: Undefined, left: Undefined)
    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 [Sumi::Graphics::Margin]
  # @raise [Sumi::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

  # Reports whether all four margin sides are zero.
  # @return [Boolean]
  def zero? = top.zero? && right.zero? && bottom.zero? && left.zero?

  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 [Sumi::Graphics::Margin]
  def self.zero = (@zero ||= self[0.0, 0.0, 0.0, 0.0])

  private

  def normalize(top, right, bottom, left)
    Margin
      .members
      .zip(
        values_for(top, right, bottom, left).map do |value|
          Scalar.finite(value, context: "margin", field: :value, nonnegative: true)
        end
      )
      .to_h
  end

  def values_for(top, right, bottom, left)
    values = [top, right, bottom, left]
    indices = {
      [true, false, false, false] => [0, 0, 0, 0],
      [true, true, false, false] => [0, 1, 0, 1],
      [true, true, true, false] => [0, 1, 2, 1]
    }[values.map { !it.equal?(Undefined) }]

    (indices ? values.values_at(*indices) : values).map { it.equal?(Undefined) ? 0 : it }
  end
end

.zero ⇒ Sumi::Graphics::Margin

Returns a zero margin.



86
# File 'graphics/lib/sumi/graphics/auxiliary/margin.rb', line 86

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 'graphics/lib/sumi/graphics/auxiliary/margin.rb', line 46

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

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

Returns a margin inflated horizontally and vertically.

Parameters:

  • h (Numeric) —

    horizontal addition

  • v (Numeric) —

    vertical addition

Returns:

Raises:



55
# File 'graphics/lib/sumi/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 'graphics/lib/sumi/graphics/auxiliary/margin.rb', line 60

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

#hash ⇒ Integer

Returns a hash compatible with strict equality.

Returns:

  • (Integer)


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

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

#horizontal ⇒ Float

Returns total horizontal margin.

Returns:

  • (Float)


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

def horizontal = left + right

#vertical ⇒ Float

Returns total vertical margin.

Returns:

  • (Float)


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

def vertical = top + bottom

#zero? ⇒ Boolean

Reports whether all four margin sides are zero.

Returns:

  • (Boolean)


76
# File 'graphics/lib/sumi/graphics/auxiliary/margin.rb', line 76

def zero? = top.zero? && right.zero? && bottom.zero? && left.zero?