Class: Opulent::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/opulent/engine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, settings = {}) ⇒ Engine

Update render settings

Parameters:

  • input (Symbol/String)

    Input code or file

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

    Opulent settings override



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/opulent/engine.rb', line 23

def initialize(input, settings = {})
  # Update default settings with user settings
  @settings = Settings.new
  @settings.update_settings settings unless settings.empty?

  # Read input parameter based on opening mode. If we have a file mode, we
  # get its path and read the code. We need to reset the mode in case the
  # next render call is on code, not on a file.
  @code = read input

  # Pass filename into settings for Parser and Compiler
  @settings[:file] = @file unless @settings[:file]

  # Get the nodes tree
  @nodes, @def = Parser.new(@settings).parse @code

  # Compile our syntax tree using input context
  @src = Compiler.new(@settings).compile @nodes, @def
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



15
16
17
# File 'lib/opulent/engine.rb', line 15

def buffer
  @buffer
end

#codeObject (readonly)

Returns the value of attribute code.



15
16
17
# File 'lib/opulent/engine.rb', line 15

def code
  @code
end

#defObject (readonly)

Returns the value of attribute def.



15
16
17
# File 'lib/opulent/engine.rb', line 15

def def
  @def
end

#fileObject (readonly)

Returns the value of attribute file.



15
16
17
# File 'lib/opulent/engine.rb', line 15

def file
  @file
end

#nodesObject (readonly)

Returns the value of attribute nodes.



15
16
17
# File 'lib/opulent/engine.rb', line 15

def nodes
  @nodes
end

#parserObject (readonly)

Returns the value of attribute parser.



15
16
17
# File 'lib/opulent/engine.rb', line 15

def parser
  @parser
end

#settingsObject (readonly)

Returns the value of attribute settings.



15
16
17
# File 'lib/opulent/engine.rb', line 15

def settings
  @settings
end

#srcObject (readonly)

Returns the value of attribute src.



15
16
17
# File 'lib/opulent/engine.rb', line 15

def src
  @src
end

Instance Method Details

#render(scope = Object.new, locals = {}, &block) ⇒ Object

Avoid code duplication when layouting is set. When we have a layout, look in layouts/application by default.

Parameters:

  • scope (Object) (defaults to: Object.new)

    Template evaluation context

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

    Render call local variables

  • block (Proc)

    Processing environment data



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/opulent/engine.rb', line 50

def render(scope = Object.new, locals = {}, &block)
  # Get opulent buffer value
  if scope.instance_variable_defined?(:@_opulent_buffer)
    initial_buffer = scope.instance_variable_get(:@_opulent_buffer)
  else
    initial_buffer = []
  end

  # If a layout is set, get the specific layout, otherwise, set the default
  # one. If a layout is set to false, the page will be render as it is.
  if scope.is_a? binding.class
    scope_object = eval 'self', scope
    scope = scope_object.instance_eval { binding } if block_given?
  else
    scope_object = scope
    scope = scope_object.instance_eval { binding }
  end

  # Set input local variables in current scope
  if scope.respond_to? :local_variable_set
    locals.each do |key, value|
      scope.local_variable_set key, value
    end
  else
    locals.each do |key, value|
      eval("#{key} = nil; lambda {|v| #{key} = v}", scope).call(value)
    end
  end

  # Evaluate the template in the given scope (context)
  begin
    eval @src, scope
  rescue ::SyntaxError => e
    raise SyntaxError, e.message
  ensure
    # Get rid of the current buffer
    scope_object.instance_variable_set :@_opulent_buffer, initial_buffer
  end
end