Class: Norma43::Utils::Contexts

Inherits:
Object
  • Object
show all
Defined in:
lib/norma43/utils/contexts.rb

Instance Method Summary collapse

Constructor Details

#initialize(containers = nil) ⇒ Contexts

Returns a new instance of Contexts.



6
7
8
9
10
# File 'lib/norma43/utils/contexts.rb', line 6

def initialize(containers = nil)
  Array(containers).compact.each do |container|
    add container
  end
end

Instance Method Details

#add(container) ⇒ Object



20
21
22
# File 'lib/norma43/utils/contexts.rb', line 20

def add(container)
  contexts.push container
end

#currentObject



16
17
18
# File 'lib/norma43/utils/contexts.rb', line 16

def current
  contexts.last
end

#move_to(container_class) ⇒ Object



28
29
30
31
32
# File 'lib/norma43/utils/contexts.rb', line 28

def move_to(container_class)
  until current.is_a?(container_class) || current.nil?
    move_up
  end if contexts.any?
end

#move_to_or_add_to_parent(container_class, parent_container_class) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/norma43/utils/contexts.rb', line 34

def move_to_or_add_to_parent(container_class, parent_container_class)
  return self if current.is_a?(container_class)

  until current.kind_of?(parent_container_class)
    move_up
  end

  entity = container_class.new

  setter_name = StringHelpers.underscore container_class.name.split("::").last
  current.public_send "#{setter_name}=", entity

  add entity

  self
end

#move_upObject



24
25
26
# File 'lib/norma43/utils/contexts.rb', line 24

def move_up
  contexts.pop
end

#resultObject



12
13
14
# File 'lib/norma43/utils/contexts.rb', line 12

def result
  contexts.first
end