Class: Arbre::Context

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

Overview

The Arbre::Context class is the frontend for using Arbre.

The simplest example possible:

html = Arbre::Context.new do
  h1 "Hello World"
end

html.to_s #=> "<h1>Hello World</h1>"

The contents of the block are instance eval'd within the Context object. This means that you lose context to the outside world from within the block. To pass local variables into the Context, use the assigns param.

html = Arbre::Context.new({one: 1}) do
  h1 "Your number #{one}"
end

html.to_s #=> "Your number 1"

Instance Attribute Summary

Attributes inherited from Element

#children, #parent

Instance Method Summary collapse

Methods inherited from Element

#+, #<<, #add_child, #ancestors, #build, #children?, #content, #content=, #each, #find_first_ancestor, #get_elements_by_class_name, #get_elements_by_tag_name, #html_safe, #inspect, #parent?, #remove_child, #tag_name, #to_ary, #to_s, #to_str

Methods included from Element::BuilderMethods

#build_tag, included, #insert_tag

Constructor Details

#initialize(assigns = {}, helpers = nil) { ... } ⇒ Context

Initialize a new Arbre::Context

Parameters:

  • assigns (Hash) (defaults to: {})

    A hash of objecs that you would like to be availble as local variables within the Context

  • helpers (Object) (defaults to: nil)

    An object that has methods on it which will become instance methods within the context.

Yields:

  • The block that will get instance eval'd in the context


37
38
39
40
41
42
43
44
45
46
# File 'lib/arbre/context.rb', line 37

def initialize(assigns = {}, helpers = nil, &block)
  assigns = assigns || {}
  @_assigns = assigns.symbolize_keys

  @_helpers = helpers
  @_current_arbre_element_buffer = [self]

  super(self)
  instance_eval &block if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Webservers treat Arbre::Context as a string. We override method_missing to delegate to the string representation of the html.


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

def method_missing(method, *args, &block)
  if cached_html.respond_to? method
    cached_html.send method, *args, &block
  else
    super
  end
end

Instance Method Details

#arbre_contextObject


48
49
50
# File 'lib/arbre/context.rb', line 48

def arbre_context
  self
end

#assignsObject


52
53
54
# File 'lib/arbre/context.rb', line 52

def assigns
  @_assigns
end

#bytesizeObject Also known as: length


65
66
67
# File 'lib/arbre/context.rb', line 65

def bytesize
  cached_html.bytesize
end

#current_arbre_elementObject


85
86
87
# File 'lib/arbre/context.rb', line 85

def current_arbre_element
  @_current_arbre_element_buffer.last
end

#helpersObject


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

def helpers
  @_helpers
end

#indent_levelObject


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

def indent_level
  # A context does not increment the indent_level
  super - 1
end

#respond_to_missing?(method, include_all) ⇒ Boolean

Returns:

  • (Boolean)

70
71
72
# File 'lib/arbre/context.rb', line 70

def respond_to_missing?(method, include_all)
  super || cached_html.respond_to?(method, include_all)
end

#with_current_arbre_element(tag) ⇒ Object Also known as: within

Raises:

  • (ArgumentError)

89
90
91
92
93
94
# File 'lib/arbre/context.rb', line 89

def with_current_arbre_element(tag)
  raise ArgumentError, "Can't be in the context of nil. #{@_current_arbre_element_buffer.inspect}" unless tag
  @_current_arbre_element_buffer.push tag
  yield
  @_current_arbre_element_buffer.pop
end