Class: Ruta::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/ruta/context.rb

Constant Summary collapse

DOM =
::Kernel.method(:DOM)

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ref, block) ⇒ Context

Returns a new instance of Context.

See Also:

  • Ruta::Context#Context#Context#handle_render


22
23
24
25
26
27
28
29
# File 'lib/ruta/context.rb', line 22

def initialize ref,block
    @ref = ref
    @elements = {}
    @handlers = {}
    @routes = {}
    @sub_contexts = []
    instance_exec(&block) if block
end

Class Attribute Details

.collection{ref => Context} (readonly)

Returns Hash of all Contexts created.

Returns:

  • ({ref => Context})

    Hash of all Contexts created



# File 'lib/ruta/context.rb', line 61

.current_contextSymbol

Returns The reference to the current context being rendered.

Returns:

  • (Symbol)

    The reference to the current context being rendered



# File 'lib/ruta/context.rb', line 67

.rendererProc (readonly)

Returns the renderer used to render and or mount components on to the DOM.

Returns:

  • (Proc)

    the renderer used to render and or mount components on to the DOM



# File 'lib/ruta/context.rb', line 64

Instance Attribute Details

#elements{ref => Route} (readonly)

Returns list of all routes attached to this context.

Returns:

  • ({ref => Route})

    list of all routes attached to this context



# File 'lib/ruta/context.rb', line 5

#handlers{ref => Proc}

Returns list of route handlers attached to this context.

Returns:

  • ({ref => Proc})

    list of route handlers attached to this context



# File 'lib/ruta/context.rb', line 11

#ref{ref => Route} (readonly)

Returns list of all routes attached to this context.

Returns:

  • ({ref => Route})

    list of all routes attached to this context



# File 'lib/ruta/context.rb', line 8

#routes{ref => Route}

Returns list of all routes attached to this context.

Returns:

  • ({ref => Route})

    list of all routes attached to this context



17
# File 'lib/ruta/context.rb', line 17

attr_reader :elements, :ref

#sub_contextsObject

Returns the value of attribute sub_contexts.



18
19
20
# File 'lib/ruta/context.rb', line 18

def sub_contexts
  @sub_contexts
end

Class Method Details

.define(ref) { ... } ⇒ Object

used to define a context’s page composition

Examples:

Define a composition for a context called :main

Ruta::Context.define :main do
  element :header do
    #some code that returns a component
  end

  sub_context :info, :info_view

  element :footer do
    #some code that returns a component
  end
end

Parameters:

  • ref (Symbol)

    reference to the context being defined

Yields:

  • a block that is used to define the composition of a context



103
104
105
# File 'lib/ruta/context.rb', line 103

def define ref, &block
  @collection[ref] = new(ref,block)
end

.handle_render {|object, element_id| ... } ⇒ Object

Used to tell the router how to render components to the DOM

Examples:

render a react.rb component to the dom

Ruta::Context.handle_render do |object,element_id|
  React.render React.create_element(object), `document.getElementById(#{element_id})`
end

Yields:

  • (object, element_id)

    A block of code that gets executed to render a given object to a given element id

Yield Parameters:

  • object (Object)

    to be rendered

  • ID (String)

    of page element object will be mounted to



83
84
85
# File 'lib/ruta/context.rb', line 83

def handle_render &block
  @renderer = block
end

.render(context, id = nil) ⇒ Object

used to render a composition to the screen

Parameters:

  • context (Symbol)

    the context to render

  • id (String) (defaults to: nil)

    of element context is to be rendered to, if no id is provided will default to body tagt



122
123
124
125
126
127
# File 'lib/ruta/context.rb', line 122

def render context, id=nil
  this = id ? $document[id]: $document.body
  context_to_render = @collection[context]
  render_context_elements context_to_render,context, this
  render_element_contents context_to_render,context
end

.wipe(id = nil) ⇒ Object

used to wipe clear an element’s content

Parameters:

  • id (String) (defaults to: nil)

    of element to be cleared, if no id is provided will clear body tag of content



110
111
112
113
114
115
116
# File 'lib/ruta/context.rb', line 110

def wipe id=nil
  if id
    $document[id].clear
  else
    $document.body.clear
  end
end

Instance Method Details

#component(id, attribs = {}) { ... } ⇒ Object

define a component of the composition

Parameters:

  • id (Symbol)

    of element to mount element contents to

  • list ({Symbol => String,Number,Boolean})

    of attributes to attach to tag

Yields:

  • block containing component to be rendered to page

Yield Returns:

  • (Object)

    a component that will be passed to the renderer to be rendered to the page



37
38
39
40
41
42
43
# File 'lib/ruta/context.rb', line 37

def component id,attribs={}, &block
    self.elements[id] = {
      attributes: attribs,
      type: :element,
      content: block
    }
end

#sub_context(id, ref, attribs = {}) ⇒ Object

mount a context as a sub context here

Parameters:

  • id (Symbol)

    of component to mount context to

  • ref (Symbol)

    of context to be mounted

  • list ({Symbol => String,Number,Boolean})

    of attributes to attach to tag



50
51
52
53
54
55
56
57
# File 'lib/ruta/context.rb', line 50

def sub_context id,ref,attribs={}
  @sub_contexts << ref
  self.elements[id] = {
    attributes: attribs,
    type: :sub_context,
    content: ref,
  }
end