Class: Cutaneous::Context

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, locals = {}, parent_context = nil) ⇒ Context

Returns a new instance of Context.



8
9
10
11
12
# File 'lib/cutaneous/context.rb', line 8

def initialize(target, locals = {}, parent_context = nil)
  super(target)
  @__target, @__locals = target, {}
  __update_context(locals, parent_context)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/cutaneous/context.rb', line 42

def method_missing(name, *args)
  return @__locals[name.to_s]   if @__locals.key?(name.to_s)
  return @__locals[name.to_sym] if @__locals.key?(name.to_sym)
  super
rescue NameError => e
  __handle_error(e)
end

Instance Attribute Details

#__bufObject

Returns the value of attribute __buf.



6
7
8
# File 'lib/cutaneous/context.rb', line 6

def __buf
  @__buf
end

#__loaderObject

Returns the value of attribute __loader.



6
7
8
# File 'lib/cutaneous/context.rb', line 6

def __loader
  @__loader
end

#__localsObject

Returns the value of attribute __locals.



6
7
8
# File 'lib/cutaneous/context.rb', line 6

def __locals
  @__locals
end

#__targetObject

Returns the value of attribute __target.



6
7
8
# File 'lib/cutaneous/context.rb', line 6

def __target
  @__target
end

Instance Method Details

#__decode_params(params) ⇒ Object



14
15
16
# File 'lib/cutaneous/context.rb', line 14

def __decode_params(params)
  params.to_s
end

#__getobj__Object

Required by the Delegator class



93
94
95
# File 'lib/cutaneous/context.rb', line 93

def __getobj__
  @__target
end

#__handle_error(e) ⇒ Object



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

def __handle_error(e)
  # Default behaviour is to silently discard errors
end

#__setobj__(obj) ⇒ Object

Required by the Delegator class



88
89
90
# File 'lib/cutaneous/context.rb', line 88

def __setobj__(obj)
  @__target = obj
end

#__update_context(locals, parent) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/cutaneous/context.rb', line 54

def __update_context(locals, parent)
  unless parent.nil?
    parent.instance_variables.reject { |var| /^@__/o === var.to_s }.each do |variable|
      instance_variable_set(variable, parent.instance_variable_get(variable))
    end
    __update_with_locals(parent.__locals) if parent.respond_to?(:__locals)
  end
  __update_with_locals(locals)
end

#__update_with_locals(locals) ⇒ Object

Sets up the local variables and also creates singleton methods on this instance so that the local values will override any method implementations on the context itself. i.e.:

class MyContext < Cutanteous::Context

def monkey
  "puzzle"
end

end

context = MyContext.new(Object.new, monkey: “magic”)

context.monkey #=> “magic” not “puzzle”



78
79
80
81
82
83
84
85
# File 'lib/cutaneous/context.rb', line 78

def __update_with_locals(locals)
  @__locals.update(locals)
  singleton = singleton_class
  locals.each do |name, value|
    singleton.__send__(:define_method, name) { value }
  end
  self
end

#clone(locals = {}) ⇒ Object



27
28
29
30
# File 'lib/cutaneous/context.rb', line 27

def clone(locals = {})
  context = self.class.new(__target, locals, self)
  context
end

#escape(value) ⇒ Object



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

def escape(value)
  CGI::escapeHTML(value)
end

#include(template_name, locals = {}) ⇒ Object



22
23
24
25
# File 'lib/cutaneous/context.rb', line 22

def include(template_name, locals = {})
  context = self.clone(locals)
  self.__buf  << __loader.template(template_name).render(context)
end

#respond_to?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to?(name, include_private = false)
  return true if @__locals.key?(name.to_s) || @__locals.key?(name.to_sym)
  super
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/cutaneous/context.rb', line 32

def respond_to_missing?(name, include_private = false)
  return true if @__locals.key?(name.to_s) || @__locals.key?(name.to_sym)
  super
end