Class: Arbre::Context

Inherits:
Container show all
Defined in:
lib/arbre/context.rb

Overview

Root of any Arbre construct. Each elements in any Arbre construct will have a reference to this context, which provides information like what the current arbre element is.

Instance Method Summary collapse

Methods inherited from Container

#to_s

Methods inherited from Element

#+, #<<, #ancestors, #arbre_context, #build!, #children, #content, #content=, #descendants, #empty?, #has_children?, #inspect, #orphan?, #parent, #remove!, #respond_to?, #to_html, #to_s

Methods included from Html::Querying

#child_tags, #descendant_tags, #find, #find_by_classes, #find_by_id, #find_by_tag, #find_by_tag_and_classes, #find_first

Methods included from Element::Building

#append_within, #append_within?, #build, included, #insert, #insert_child, #prepend_within, #prepend_within?, #temporary

Constructor Details

#initialize(assigns = nil, helpers = nil, &block) ⇒ Context

Initialize a new Arbre::Context. Any passed block will be instance-exec’d.

Parameters:

  • assigns (Hash) (defaults to: nil)

    A hash of objects which will be made available as instance variables in the context, and in some Arbre elements.

  • helpers (Object) (defaults to: nil)

    An object that has methods on it which will become instance methods within the context. In a Rails set up, this is typically the ActionView::Base instance used to render the view.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/arbre/context.rb', line 23

def initialize(assigns = nil, helpers = nil, &block)
  @_assigns = (assigns || {}).symbolize_keys
  @_helpers = helpers
  @_element_stack = [ self ]
  @_flow_stack = [ :append ]

  # Pass ourselves as the arbre context to the element.
  super self

  instance_exec &block if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Arbre::Element

Instance Method Details

#assignsObject

Attributes



38
39
40
# File 'lib/arbre/context.rb', line 38

def assigns
  @_assigns
end

#current_elementObject

Element & flow stack



57
58
59
# File 'lib/arbre/context.rb', line 57

def current_element
  @_element_stack.last
end

#current_flowObject



61
62
63
# File 'lib/arbre/context.rb', line 61

def current_flow
  @_flow_stack.last
end

#helpersObject



42
43
44
# File 'lib/arbre/context.rb', line 42

def helpers
  @_helpers
end

#indent_levelObject



50
51
52
# File 'lib/arbre/context.rb', line 50

def indent_level
  -1
end

#parent=Object

Raises:

  • (NotImplementedError)


46
47
48
# File 'lib/arbre/context.rb', line 46

def parent=(*)
  raise NotImplementedError, "Arbre::Context cannot have a parent"
end

#replace_current_flow(flow) ⇒ Object

Replaces the current flow. For internal usie!



77
78
79
80
# File 'lib/arbre/context.rb', line 77

def replace_current_flow(flow)
  @_flow_stack.pop
  @_flow_stack.push flow
end

#with_current(element: nil, flow: :append) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/arbre/context.rb', line 65

def with_current(element: nil, flow: :append)
  raise ArgumentError, "can't be in the context of nil" unless element

  @_element_stack.push element
  @_flow_stack.push flow
  yield if block_given?
ensure
  @_flow_stack.pop
  @_element_stack.pop
end