Class: Slideck::Margin Private

Inherits:
Object
  • Object
show all
Defined in:
lib/slideck/margin.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Responsible for accessing margin configuration

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(top, right, bottom, left) ⇒ Margin

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a Margin instance

Parameters:

  • top (Integer)

    the top margin

  • right (Integer)

    the right margin

  • bottom (Integer)

    the bottom margin

  • left (Integer)

    the left margin



253
254
255
256
257
258
259
260
# File 'lib/slideck/margin.rb', line 253

def initialize(top, right, bottom, left)
  @top = validate_margin_side(:top, top)
  @right = validate_margin_side(:right, right)
  @bottom = validate_margin_side(:bottom, bottom)
  @left = validate_margin_side(:left, left)

  freeze
end

Instance Attribute Details

#bottomInteger (readonly)

The bottom margin

Examples:

margin.bottom

Returns:

  • (Integer)


209
210
211
# File 'lib/slideck/margin.rb', line 209

def bottom
  @bottom
end

#leftInteger (readonly)

The left margin

Examples:

margin.left

Returns:

  • (Integer)


219
220
221
# File 'lib/slideck/margin.rb', line 219

def left
  @left
end

#rightInteger (readonly)

The right margin

Examples:

margin.right

Returns:

  • (Integer)


229
230
231
# File 'lib/slideck/margin.rb', line 229

def right
  @right
end

#topInteger (readonly)

The top margin

Examples:

margin.top

Returns:

  • (Integer)


239
240
241
# File 'lib/slideck/margin.rb', line 239

def top
  @top
end

Class Method Details

.[](*values) ⇒ Slideck::Margin

Create a Margin instance with an array-like initialiser

Examples:

Slideck::Margin[1, 2]
Slideck::Margin[1, 2, 3, 4]

Parameters:

  • values (Array<Integer>)

    the values to convert to a margin

Returns:



78
79
80
# File 'lib/slideck/margin.rb', line 78

def self.[](*values)
  from_array(values)
end

.from(value) ⇒ Slideck::Margin

Create a Margin instance from a value

Examples:

Slideck::Margin.from(1)
Slideck::Margin.from([1, 2])
Slideck::Margin.from({top: 1, left: 2})
Slideck::Margin.from("1, 2")

Parameters:

  • value (Object)

    the value to create a margin from

Returns:

Raises:



54
55
56
57
58
59
60
61
62
# File 'lib/slideck/margin.rb', line 54

def self.from(value)
  if value.is_a?(Hash)
    from_hash(value)
  elsif (converted = convert_to_array(value))
    from_array(converted)
  else
    raise_invalid_margin_error(value)
  end
end

.from_array(value) ⇒ Slideck::Margin

Create a Margin instance from an array

Examples:

Slideck::Margin.from_array([1, 2])

Parameters:

  • value (Array)

    the value to convert to a margin

Returns:

Raises:



95
96
97
98
99
100
101
102
103
# File 'lib/slideck/margin.rb', line 95

def self.from_array(value)
  case value.size
  when 1 then new(*(value * 4))
  when 2 then new(*(value * 2))
  when 3 then new(*value, value[1])
  when 4 then new(*value)
  else raise_invalid_margin_as_array_error(value)
  end
end

.from_hash(value) ⇒ Slideck::Margin

Create a Margin instance from a hash

Examples:

Slideck::Margin.from_hash{top: 1, left: 2})

Parameters:

  • value (Hash)

    the hash value to convert to a margin

Returns:

Raises:



118
119
120
121
122
123
124
# File 'lib/slideck/margin.rb', line 118

def self.from_hash(value)
  unless (invalid = (value.keys - SIDE_NAMES)).empty?
    raise_invalid_margin_as_hash_error(invalid)
  end

  new(*value.values_at(*SIDE_NAMES).map { |val| val.nil? ? 0 : val })
end

Instance Method Details

#==(other) ⇒ Boolean

Determine equivalence with another object

Examples:

margin == other

Parameters:

  • other (Object)

    the other object to determine equivalence with

Returns:

  • (Boolean)

    true if this object is equivalent to the other, false otherwise



275
276
277
278
279
# File 'lib/slideck/margin.rb', line 275

def ==(other)
  other.is_a?(self.class) &&
    top == other.top && right == other.right &&
    bottom == other.bottom && left == other.left
end

#eql?(other) ⇒ Boolean

Determine for equality with another object

Examples:

margin.eql?(other)

Parameters:

  • other (Object)

    the other object to determine equality with

Returns:

  • (Boolean)

    true if this object is equal to the other, false otherwise



293
294
295
296
297
# File 'lib/slideck/margin.rb', line 293

def eql?(other)
  instance_of?(other.class) &&
    top.eql?(other.top) && right.eql?(other.right) &&
    bottom.eql?(other.bottom) && left.eql?(other.left)
end

#hashInteger

Generate hash value of this margin

Examples:

margin.hash

Returns:

  • (Integer)


307
308
309
# File 'lib/slideck/margin.rb', line 307

def hash
  [self.class, top, right, bottom, left].hash
end

#to_aArray<Integer, Integer, Integer, Integer>

An array representation of all margin sides

Examples:

margin = Slideck::Margin[1, 2, 3, 4]
margin.to_a # => [1, 2, 3, 4]

Returns:

  • (Array<Integer, Integer, Integer, Integer>)


320
321
322
# File 'lib/slideck/margin.rb', line 320

def to_a
  [top, right, bottom, left]
end