Class: Bade::Runtime::RenderBinding
- Defined in:
- lib/bade/runtime/render_binding.rb
Constant Summary collapse
Instance Attribute Summary collapse
-
#__base_indent ⇒ String
Holds.
- #__buffs_stack ⇒ Array<Array<String>>
- #__location_stack ⇒ Array<Location>
- #__mixins ⇒ Hash<String, Mixin>
-
#__new_line ⇒ String
Holds.
Instance Method Summary collapse
-
#__buff ⇒ Object
— Methods for dealing with pushing and popping buffers in stack.
- #__buffs_pop ⇒ Array<String>?
- #__buffs_push(location) ⇒ Object
-
#__create_block(name, location = nil, &block) ⇒ Object
Shortcut for creating blocks.
- #__create_mixin(name, location, &block) ⇒ Object
- #__current_location ⇒ Location?
- #__get_binding ⇒ Binding
-
#__html_escaped(text) ⇒ String
Escape input text with html escapes.
- #__load(filename) ⇒ Object
-
#__reset ⇒ nil
Resets this binding to default state, this method should be evoked after running the template lambda.
- #__tag_render_attribute(name, *values) ⇒ Object
- #__update_lineno(number) ⇒ Object
-
#initialize(vars = {}) ⇒ RenderBinding
constructor
A new instance of RenderBinding.
- #require_relative(filename) ⇒ Object
Constructor Details
#initialize(vars = {}) ⇒ RenderBinding
Returns a new instance of RenderBinding.
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_indent ⇒ String
Holds
24 25 26 |
# File 'lib/bade/runtime/render_binding.rb', line 24 def __base_indent @__base_indent end |
#__buffs_stack ⇒ Array<Array<String>>
12 13 14 |
# File 'lib/bade/runtime/render_binding.rb', line 12 def __buffs_stack @__buffs_stack end |
#__location_stack ⇒ Array<Location>
15 16 17 |
# File 'lib/bade/runtime/render_binding.rb', line 15 def __location_stack @__location_stack end |
#__mixins ⇒ Hash<String, Mixin>
19 20 21 |
# File 'lib/bade/runtime/render_binding.rb', line 19 def __mixins @__mixins end |
#__new_line ⇒ String
Holds
24 25 26 |
# File 'lib/bade/runtime/render_binding.rb', line 24 def __new_line @__new_line end |
Instance Method Details
#__buff ⇒ Object
— 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_pop ⇒ Array<String>?
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
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_location ⇒ Location?
142 143 144 |
# File 'lib/bade/runtime/render_binding.rb', line 142 def __current_location __location_stack.first end |
#__get_binding ⇒ 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
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('&', '&') .gsub('<', '<') .gsub('>', '>') .gsub('"', '"') end |
#__load(filename) ⇒ Object
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 |
#__reset ⇒ nil
Resets this binding to default state, this method should be evoked after running the template lambda
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
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.(filename, File.dirname(context_path)) Kernel.require(abs_path) end end |