Module: Scribble::Support::Context

Included in:
Block, Partial::Context
Defined in:
lib/scribble/support/context.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



13
14
15
# File 'lib/scribble/support/context.rb', line 13

def self.included base
  base.extend ClassMethods
end

Instance Method Details

#converterObject



67
68
69
# File 'lib/scribble/support/context.rb', line 67

def converter
  @converter ||= template.find_converter(format, render_format)
end

#evaluate(call, args, context) ⇒ Object

Evaluation



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/scribble/support/context.rb', line 87

def evaluate call, args, context
  resolve_variable(call) || Scribble::Registry.evaluate(call.name, self, args, call, context)
rescue Support::Unmatched => local
  raise local if @context.nil? || call.split?

  begin
    @context.evaluate call, args, context
  rescue Support::Unmatched => global
    raise global.merge(local)
  end
end

#formatObject

Format conversion



55
56
57
# File 'lib/scribble/support/context.rb', line 55

def format
  @context.format
end

#nodesObject

Rendering

Raises:

  • (NotImplementedError)


19
20
21
# File 'lib/scribble/support/context.rb', line 19

def nodes
  raise NotImplementedError, 'Class that includes context must implement nodes method'
end

#registersObject



49
50
51
# File 'lib/scribble/support/context.rb', line 49

def registers
  @context.registers
end

#render(nodes: nil, context: self) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/scribble/support/context.rb', line 23

def render nodes: nil, context: self
  nodes ||= self.nodes
  
  if !require_conversion?
    render_without_conversion nodes, context

  elsif converter
    converter.convert render_without_conversion(nodes, context)

  elsif format.nil?
    raise "Cannot convert to #{render_format} without format"
  else
    raise "No suitable converter converting #{format} to #{render_format}"
  end
end

#render_formatObject



59
60
61
# File 'lib/scribble/support/context.rb', line 59

def render_format
  @context.format
end

#render_without_conversion(nodes, context) ⇒ Object



39
40
41
# File 'lib/scribble/support/context.rb', line 39

def render_without_conversion nodes, context
  nodes.map { |node| Scribble::Registry.to_string node.evaluate(context) }.join
end

#require_conversion?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/scribble/support/context.rb', line 63

def require_conversion?
  render_format && format != render_format
end

#resolve_variable(call) ⇒ Object



81
82
83
# File 'lib/scribble/support/context.rb', line 81

def resolve_variable call
  variables[call.name] if call.allow_variable
end

#set_variable(name, value) ⇒ Object



77
78
79
# File 'lib/scribble/support/context.rb', line 77

def set_variable name, value
  variables[name] = value
end

#templateObject

Template and registers



45
46
47
# File 'lib/scribble/support/context.rb', line 45

def template
  @context.template
end

#variablesObject

Variables



73
74
75
# File 'lib/scribble/support/context.rb', line 73

def variables
  @variables ||= {}
end