Class: Sevgi::Graphics::Margin
- Inherits:
-
Data
- Object
- Data
- Sevgi::Graphics::Margin
- Includes:
- Comparable
- Defined in:
- 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
-
.margin(array) ⇒ Sevgi::Graphics::Margin
Builds a margin from an array-like shorthand.
-
.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.
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
#bottom ⇒ Float (readonly)
Returns bottom margin.
6 7 8 |
# File 'lib/sevgi/graphics/auxiliary/margin.rb', line 6 def bottom @bottom end |
#left ⇒ Float (readonly)
Returns left margin.
6 7 8 |
# File 'lib/sevgi/graphics/auxiliary/margin.rb', line 6 def left @left end |
#right ⇒ Float (readonly)
Returns right margin.
6 7 8 |
# File 'lib/sevgi/graphics/auxiliary/margin.rb', line 6 def right @right end |
#top ⇒ Float (readonly)
Returns 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.
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 |
.zero ⇒ Sevgi::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.
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.
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.
42 |
# File 'lib/sevgi/graphics/auxiliary/margin.rb', line 42 def eql?(other) = self.class == other.class && deconstruct == other.deconstruct |
#hash ⇒ Integer
Returns a hash compatible with strict equality.
46 |
# File 'lib/sevgi/graphics/auxiliary/margin.rb', line 46 def hash = [self.class, *deconstruct].hash |
#horizontal ⇒ Float
Returns total horizontal margin.
50 |
# File 'lib/sevgi/graphics/auxiliary/margin.rb', line 50 def horizontal = left + right |
#vertical ⇒ Float
Returns total vertical margin.
54 |
# File 'lib/sevgi/graphics/auxiliary/margin.rb', line 54 def vertical = top + bottom |