Class: GeoLabels::Importer::ImportLabel

Inherits:
Object
  • Object
show all
Defined in:
lib/geo_labels/importer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ ImportLabel

Returns a new instance of ImportLabel.



85
86
87
# File 'lib/geo_labels/importer.rb', line 85

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



83
84
85
# File 'lib/geo_labels/importer.rb', line 83

def name
  @name
end

#parentObject

Returns the value of attribute parent.



83
84
85
# File 'lib/geo_labels/importer.rb', line 83

def parent
  @parent
end

Instance Method Details

#add_child(label) ⇒ Object



103
104
105
106
107
# File 'lib/geo_labels/importer.rb', line 103

def add_child(label)
  children.push label
  label.parent = self
  label
end

#add_sibling(label) ⇒ Object



97
98
99
100
101
# File 'lib/geo_labels/importer.rb', line 97

def add_sibling(label)
  label.parent = parent
  parent.children.push label
  label
end

#childrenObject



93
94
95
# File 'lib/geo_labels/importer.rb', line 93

def children
  @children ||= LabelList.new
end

#inspectObject



109
110
111
112
113
114
115
# File 'lib/geo_labels/importer.rb', line 109

def inspect
  # "#{name} => #{children.inspect}"
  # children.any? ? "<#{name}> => #{children.inpsect}" : "#{name}"
  base = name.to_s
  base = "#{base} => #{children.inspect}" if children.any?
  base
end

#is_root?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/geo_labels/importer.rb', line 89

def is_root?
  name == 'root'
end

#to_hObject



117
118
119
120
121
122
123
124
125
# File 'lib/geo_labels/importer.rb', line 117

def to_h
  if is_root?
    {labels: children.map(&:to_h)}
  else
    r = {name: name}
    r[:children] = children.map(&:to_h) if children.any?
    r
  end
end