Class: Bade::Runtime::RenderBinding

Inherits:
Object
  • Object
show all
Defined in:
lib/bade/runtime/render_binding.rb

Constant Summary collapse

Location =
Bade::Runtime::Location

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vars = {}) ⇒ RenderBinding

Returns a new instance of RenderBinding.

Parameters:

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


28
29
30
31
32
33
34
35
36
37
38
# File 'lib/bade/runtime/render_binding.rb', line 28

def initialize(vars = {})
  __reset

  vars.each do |key, value|
    raise KeyError, "Already defined variable #{key.inspect} in this binding" if respond_to?(key.to_sym)

    define_singleton_method(key) do
      value
    end
  end
end

Instance Attribute Details

#__base_indentString

Holds

Returns:



24
25
26
# File 'lib/bade/runtime/render_binding.rb', line 24

def __base_indent
  @__base_indent
end

#__buffs_stackArray<Array<String>>

Returns:



12
13
14
# File 'lib/bade/runtime/render_binding.rb', line 12

def __buffs_stack
  @__buffs_stack
end

#__location_stackArray<Location>

Returns:



15
16
17
# File 'lib/bade/runtime/render_binding.rb', line 15

def __location_stack
  @__location_stack
end

#__mixinsHash<String, Mixin>

Returns:



19
20
21
# File 'lib/bade/runtime/render_binding.rb', line 19

def __mixins
  @__mixins
end

#__new_lineString

Holds

Returns:



24
25
26
# File 'lib/bade/runtime/render_binding.rb', line 24

def __new_line
  @__new_line
end

Instance Method Details

#__buffObject

— Methods for dealing with pushing and popping buffers in stack



68
69
70
# File 'lib/bade/runtime/render_binding.rb', line 68

def __buff
  __buffs_stack.first
end

#__buffs_popArray<String>?

Returns:



79
80
81
82
# File 'lib/bade/runtime/render_binding.rb', line 79

def __buffs_pop
  __location_stack.shift
  __buffs_stack.shift
end

#__buffs_push(location) ⇒ Object

Parameters:



73
74
75
76
# File 'lib/bade/runtime/render_binding.rb', line 73

def __buffs_push(location)
  __buffs_stack.unshift([])
  __location_stack.unshift(location) unless location.nil?
end

#__create_block(name, location = nil, &block) ⇒ Object

Shortcut for creating blocks



58
59
60
# File 'lib/bade/runtime/render_binding.rb', line 58

def __create_block(name, location = nil, &block)
  Bade::Runtime::Block.new(name, location, self, &block)
end

#__create_mixin(name, location, &block) ⇒ Object



62
63
64
# File 'lib/bade/runtime/render_binding.rb', line 62

def __create_mixin(name, location, &block)
  Bade::Runtime::Mixin.new(name, location, self, &block)
end

#__current_locationLocation?

Returns:



133
134
135
# File 'lib/bade/runtime/render_binding.rb', line 133

def __current_location
  __location_stack.first
end

#__get_bindingBinding

Returns:

  • (Binding)


52
53
54
# File 'lib/bade/runtime/render_binding.rb', line 52

def __get_binding
  binding
end

#__html_escaped(text) ⇒ String

Escape input text with html escapes

Parameters:

Returns:



112
113
114
115
116
117
118
119
# File 'lib/bade/runtime/render_binding.rb', line 112

def __html_escaped(text)
  return nil if text.nil?

  text.gsub('&', '&amp;')
      .gsub('<', '&lt;')
      .gsub('>', '&gt;')
      .gsub('"', '&quot;')
end

#__load(filename) ⇒ Object

Parameters:



87
88
89
90
91
92
# File 'lib/bade/runtime/render_binding.rb', line 87

def __load(filename)
  # Universal way to load Ruby file (production or during tests)
  # rubocop:disable Security/Eval
  eval(File.read(filename), __get_binding, filename)
  # rubocop:enable Security/Eval
end

#__resetnil

Resets this binding to default state, this method should be evoked after running the template lambda

Returns:

  • (nil)


44
45
46
47
48
# File 'lib/bade/runtime/render_binding.rb', line 44

def __reset
  @__buffs_stack = []
  @__location_stack = []
  @__mixins = Hash.new { |_hash, key| raise Bade::Runtime::KeyError.new("Undefined mixin '#{key}'", __location_stack) }
end

#__tag_render_attribute(name, *values) ⇒ Object



121
122
123
124
125
126
# File 'lib/bade/runtime/render_binding.rb', line 121

def __tag_render_attribute(name, *values)
  values = values.compact
  return if values.empty?

  %( #{name}="#{values.join(' ')}")
end

#__update_lineno(number) ⇒ Object



128
129
130
# File 'lib/bade/runtime/render_binding.rb', line 128

def __update_lineno(number)
  __location_stack.first&.lineno = number
end

#require_relative(filename) ⇒ Object

Parameters:



95
96
97
98
99
100
101
102
103
104
# File 'lib/bade/runtime/render_binding.rb', line 95

def require_relative(filename)
  # FakeFS does not fake `require_relative` method
  if Object.const_defined?(:FakeFS) && Object.const_get(:FakeFS).activated?
    # rubocop:disable Security/Eval
    eval(File.read(filename), __get_binding, filename)
    # rubocop:enable Security/Eval
  else
    Kernel.require_relative(filename)
  end
end