Class: Habdsl::Model::Location

Inherits:
Object
  • Object
show all
Defined in:
lib/habdsl/model/location.rb

Overview

Represents a location in the structure.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, label:, icon:, parent: nil) ⇒ Location

Returns a new instance of Location.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/habdsl/model/location.rb', line 9

def initialize(name:, label:, icon:, parent: nil)
  validate!(name, "name")
  validate!(icon, "icon")

  @name = name
  @label = label
  @icon = icon
  @parent = parent
  @sub_locations = []
  @equipments = []
end

Instance Attribute Details

#equipmentsObject (readonly)

Returns the value of attribute equipments.



7
8
9
# File 'lib/habdsl/model/location.rb', line 7

def equipments
  @equipments
end

#iconObject (readonly)

Returns the value of attribute icon.



7
8
9
# File 'lib/habdsl/model/location.rb', line 7

def icon
  @icon
end

#labelObject (readonly)

Returns the value of attribute label.



7
8
9
# File 'lib/habdsl/model/location.rb', line 7

def label
  @label
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/habdsl/model/location.rb', line 7

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



7
8
9
# File 'lib/habdsl/model/location.rb', line 7

def parent
  @parent
end

#sub_locationsObject (readonly)

Returns the value of attribute sub_locations.



7
8
9
# File 'lib/habdsl/model/location.rb', line 7

def sub_locations
  @sub_locations
end

Instance Method Details

#equipment(name:, label:, icon:, parent: nil) ⇒ Object



27
28
29
30
31
# File 'lib/habdsl/model/location.rb', line 27

def equipment(name:, label:, icon:, parent: nil, &)
  eq = Equipment.new(name: name, label: label, icon: icon, parent: parent)
  eq.instance_eval(&) if block_given?
  @equipments << eq
end

#location(name:, label:, icon:, parent: nil) ⇒ Object



21
22
23
24
25
# File 'lib/habdsl/model/location.rb', line 21

def location(name:, label:, icon:, parent: nil, &)
  loc = Location.new(name: name, label: label, icon: icon, parent: parent)
  loc.instance_eval(&) if block_given?
  @sub_locations << loc
end

#pointObject



33
34
35
# File 'lib/habdsl/model/location.rb', line 33

def point(*)
  raise "Error: point cannot be directly under location. Use equipment to wrap it."
end

#to_s(parent_name = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/habdsl/model/location.rb', line 37

def to_s(parent_name = nil)
  result = "Group #{@name} \"#{@label}\" <#{@icon}>"
  result += format_parent(parent_name, @parent)
  result += " [\"Location\"]\n"

  @sub_locations.each {|sub_loc| result += sub_loc.to_s(@name) }
  @equipments.each {|eq| result += eq.to_s(@name) }

  result
end