Class: TotalIn::Contexts

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

Instance Method Summary collapse

Constructor Details

#initialize(containers = nil) ⇒ Contexts

Returns a new instance of Contexts.



3
4
5
6
7
# File 'lib/total_in/contexts.rb', line 3

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

Instance Method Details

#add(container) ⇒ Object



17
18
19
# File 'lib/total_in/contexts.rb', line 17

def add container
  contexts.push container
end

#currentObject



13
14
15
# File 'lib/total_in/contexts.rb', line 13

def current
  contexts.last
end

#move_to(container_class) ⇒ Object



25
26
27
28
29
# File 'lib/total_in/contexts.rb', line 25

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

#move_to_or_add_to_parent(container_class, parent_container_class) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/total_in/contexts.rb', line 31

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



21
22
23
# File 'lib/total_in/contexts.rb', line 21

def move_up
  contexts.pop
end

#resultObject



9
10
11
# File 'lib/total_in/contexts.rb', line 9

def result
  contexts.first
end