Class: Sevgi::Graphics::Margin
- Inherits:
-
Data
- Object
- Data
- Sevgi::Graphics::Margin
- 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
-
#bottom ⇒ Float
readonly
Bottom margin.
-
#left ⇒ Float
readonly
Left margin.
-
#right ⇒ Float
readonly
Right margin.
-
#top ⇒ Float
readonly
Top margin.
Class Method Summary collapse
-
.[](top = nil, right = nil, bottom = nil, left = nil) ⇒ Sevgi::Graphics::Margin
Creates a margin from one to four CSS-like shorthand values.
-
.zero ⇒ Sevgi::Graphics::Margin
Returns a zero margin.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer?
Compares margins by top, right, bottom, then left.
-
#adjust(h, v) ⇒ Sevgi::Graphics::Margin
Returns a margin inflated horizontally and vertically.
-
#eql?(other) ⇒ Boolean
(also: #==)
Reports strict margin equality.
-
#hash ⇒ Integer
Returns a hash compatible with strict equality.
-
#horizontal ⇒ Float
Returns total horizontal margin.
-
#initialize(top: nil, right: nil, bottom: nil, left: nil) ⇒ void
constructor
Creates a margin from one to four shorthand values.
-
#vertical ⇒ Float
Returns total vertical margin.
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.
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
#bottom ⇒ Float (readonly)
Returns bottom margin.
19 20 21 |
# File 'lib/sevgi/graphics/auxiliary/margin.rb', line 19 def bottom @bottom end |
#left ⇒ Float (readonly)
Returns left margin.
19 20 21 |
# File 'lib/sevgi/graphics/auxiliary/margin.rb', line 19 def left @left end |
#right ⇒ Float (readonly)
Returns right margin.
19 20 21 |
# File 'lib/sevgi/graphics/auxiliary/margin.rb', line 19 def right @right end |
#top ⇒ Float (readonly)
Returns 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.
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 |
.zero ⇒ Sevgi::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.
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.
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.
60 |
# File 'lib/sevgi/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.
64 |
# File 'lib/sevgi/graphics/auxiliary/margin.rb', line 64 def hash = [self.class, *deconstruct].hash |
#horizontal ⇒ Float
Returns total horizontal margin.
68 |
# File 'lib/sevgi/graphics/auxiliary/margin.rb', line 68 def horizontal = left + right |
#vertical ⇒ Float
Returns total vertical margin.
72 |
# File 'lib/sevgi/graphics/auxiliary/margin.rb', line 72 def vertical = top + bottom |