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
68 69 70 |
# File 'lib/bade/runtime/render_binding.rb', line 68 def __buff __buffs_stack.first end |
#__buffs_pop ⇒ Array<String>?
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
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_location ⇒ Location?
133 134 135 |
# File 'lib/bade/runtime/render_binding.rb', line 133 def __current_location __location_stack.first end |
#__get_binding ⇒ 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
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('&', '&') .gsub('<', '<') .gsub('>', '>') .gsub('"', '"') end |
#__load(filename) ⇒ Object
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 |
#__reset ⇒ nil
Resets this binding to default state, this method should be evoked after running the template lambda
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
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 |