Class: CSVStepImporter::Node

Inherits:
Base
  • Object
show all
Defined in:
lib/csv_step_importer/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#assign_attributes, #inspect, #persisted?, #save, #save!, #to_s, #update

Constructor Details

#initialize(parent: nil, children: [], env: nil) ⇒ Node

Returns a new instance of Node.



15
16
17
18
19
20
21
# File 'lib/csv_step_importer/node.rb', line 15

def initialize(parent: nil, children: [], env: nil)
  super()

  self.env = env
  self.parent = parent
  self.children = children
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



5
6
7
# File 'lib/csv_step_importer/node.rb', line 5

def children
  @children
end

#envObject

Returns the value of attribute env.



8
9
10
# File 'lib/csv_step_importer/node.rb', line 8

def env
  @env
end

#parentObject

Returns the value of attribute parent.



6
7
8
# File 'lib/csv_step_importer/node.rb', line 6

def parent
  @parent
end

Instance Method Details

#add_children(children, prepend: false) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/csv_step_importer/node.rb', line 33

def add_children(children, prepend: false)
  children = Array(children) unless children.is_a?(Enumerable)

  children = children.collect do |child, sub_children|
    next child unless child.is_a?(Class)
    child.new parent: self, children: sub_children
  end.compact

  @children = prepend ? children + @children : @children + children
end

#create_or_updateObject



44
45
46
# File 'lib/csv_step_importer/node.rb', line 44

def create_or_update
  children.empty? || children.all?(&:save)
end

#validate_childrenObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/csv_step_importer/node.rb', line 48

def validate_children
  return unless errors.empty?

  children.each do |child|
    next if child.valid?
    child.errors.each do |key, message|
      errors[key] << message
    end
  end
end