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



70
71
72
# File 'lib/bade/runtime/render_binding.rb', line 70

def __buff
  __buffs_stack.first
end

#__buffs_popArray<String>?

Returns:



81
82
83
84
# File 'lib/bade/runtime/render_binding.rb', line 81

def __buffs_pop
  __location_stack.shift
  __buffs_stack.shift
end

#__buffs_push(location) ⇒ Object

Parameters:



75
76
77
78
# File 'lib/bade/runtime/render_binding.rb', line 75

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



60
61
62
# File 'lib/bade/runtime/render_binding.rb', line 60

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

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



64
65
66
# File 'lib/bade/runtime/render_binding.rb', line 64

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

#__current_locationLocation?

Returns:



142
143
144
# File 'lib/bade/runtime/render_binding.rb', line 142

def __current_location
  __location_stack.first
end

#__get_bindingBinding

Returns:

  • (Binding)


54
55
56
# File 'lib/bade/runtime/render_binding.rb', line 54

def __get_binding
  binding
end

#__html_escaped(text) ⇒ String

Escape input text with html escapes

Parameters:

Returns:



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

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

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

#__load(filename) ⇒ Object

Parameters:



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

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
49
50
# File 'lib/bade/runtime/render_binding.rb', line 44

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

#__tag_render_attribute(name, *values) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/bade/runtime/render_binding.rb', line 127

def __tag_render_attribute(name, *values)
  values = values
           .compact
           .map(&:to_s)
           .map { |value| __html_escaped(value) }
  return if values.empty?

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

#__update_lineno(number) ⇒ Object



137
138
139
# File 'lib/bade/runtime/render_binding.rb', line 137

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

#require_relative(filename) ⇒ Object

Parameters:



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/bade/runtime/render_binding.rb', line 97

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
    context_path = caller_locations(1, 1).first.path

    abs_path = File.expand_path(filename, File.dirname(context_path))

    Kernel.require(abs_path)
  end
end