Class: Archimate::DataModel::Bounds

Inherits:
Object
  • Object
show all
Includes:
Comparison
Defined in:
lib/archimate/data_model/bounds.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Comparison

#==, #[], #dig, #each, #hash, included, #inspect, #pretty_print, #to_h

Constructor Details

#initialize(x: nil, y: nil, width:, height:) ⇒ Bounds

Returns a new instance of Bounds.



34
35
36
37
38
39
40
41
# File 'lib/archimate/data_model/bounds.rb', line 34

def initialize(x: nil, y: nil, width:, height:)
  raise "Width expected" unless width
  raise "Height expected" unless height
  @x = x.nil? ? nil : x.to_f
  @y = y.nil? ? nil : y.to_f
  @width = width.to_f
  @height = height.to_f
end

Instance Attribute Details

#heightFloat (readonly)

Returns:

  • (Float)


19
# File 'lib/archimate/data_model/bounds.rb', line 19

model_attr :height

#widthFloat (readonly)

Returns:

  • (Float)


16
# File 'lib/archimate/data_model/bounds.rb', line 16

model_attr :width

#xFloat, NilClass (readonly)

Returns:

  • (Float, NilClass)


10
# File 'lib/archimate/data_model/bounds.rb', line 10

model_attr :x, default: nil

#yFloat, NilClass (readonly)

Returns:

  • (Float, NilClass)


13
# File 'lib/archimate/data_model/bounds.rb', line 13

model_attr :y, default: nil

Class Method Details

.from_location(location) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/archimate/data_model/bounds.rb', line 25

def self.from_location(location)
  DataModel::Bounds.new(
    x: location.x,
    y: location.y,
    width: 0,
    height: 0
  )
end

.zeroObject



21
22
23
# File 'lib/archimate/data_model/bounds.rb', line 21

def self.zero
  Archimate::DataModel::Bounds.new(x: 0, y: 0, width: 0, height: 0)
end

Instance Method Details

#above?(other) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/archimate/data_model/bounds.rb', line 80

def above?(other)
  bottom < other.top
end

#below?(other) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/archimate/data_model/bounds.rb', line 84

def below?(other)
  top > other.bottom
end

#bottomObject



59
60
61
# File 'lib/archimate/data_model/bounds.rb', line 59

def bottom
  top + height
end

#centerObject



71
72
73
74
75
76
77
78
# File 'lib/archimate/data_model/bounds.rb', line 71

def center
  DataModel::Bounds.new(
    x: left + width / 2.0,
    y: top + height / 2.0,
    width: 0,
    height: 0
  )
end

#inside?(other) ⇒ Boolean

Tests if this bounds is inside the argument Bounds

Returns:

  • (Boolean)


101
102
103
104
105
106
# File 'lib/archimate/data_model/bounds.rb', line 101

def inside?(other)
  left > other.left &&
    right < other.right &&
    top > other.top &&
    bottom < other.bottom
end

#leftObject



67
68
69
# File 'lib/archimate/data_model/bounds.rb', line 67

def left
  x || 0
end

#left_of?(other) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/archimate/data_model/bounds.rb', line 92

def left_of?(other)
  right < other.left
end

#reduced_by(val) ⇒ Object



96
97
98
# File 'lib/archimate/data_model/bounds.rb', line 96

def reduced_by(val)
  Bounds.new(x: left + val, y: top + val, width: width - val * 2, height: height - val * 2)
end

#rightObject



63
64
65
# File 'lib/archimate/data_model/bounds.rb', line 63

def right
  left + width
end

#right_of?(other) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/archimate/data_model/bounds.rb', line 88

def right_of?(other)
  left > other.right
end

#to_sObject



43
44
45
# File 'lib/archimate/data_model/bounds.rb', line 43

def to_s
  "Bounds(x: #{x}, y: #{y}, width: #{width}, height: #{height})"
end

#topObject



55
56
57
# File 'lib/archimate/data_model/bounds.rb', line 55

def top
  y || 0
end

#x_rangeObject



47
48
49
# File 'lib/archimate/data_model/bounds.rb', line 47

def x_range
  Range.new(left, right)
end

#y_rangeObject



51
52
53
# File 'lib/archimate/data_model/bounds.rb', line 51

def y_range
  Range.new(top, bottom)
end