class Norma43::Utils::Contexts

Public Class Methods

new(containers = nil) click to toggle source
# File lib/norma43/utils/contexts.rb, line 6
def initialize(containers = nil)
  Array(containers).compact.each do |container|
    add container
  end
end

Public Instance Methods

add(container) click to toggle source
# File lib/norma43/utils/contexts.rb, line 20
def add(container)
  contexts.push container
end
current() click to toggle source
# File lib/norma43/utils/contexts.rb, line 16
def current
  contexts.last
end
move_to(container_class) click to toggle source
# 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) click to toggle source
# 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_up() click to toggle source
# File lib/norma43/utils/contexts.rb, line 24
def move_up
  contexts.pop
end
result() click to toggle source
# File lib/norma43/utils/contexts.rb, line 12
def result
  contexts.first
end

Private Instance Methods

contexts() click to toggle source
# File lib/norma43/utils/contexts.rb, line 52
def contexts
  @contexts ||= []
end