Module: ERBh

Defined in:
lib/erbh.rb

Overview

rubocop:disable Style/Documentation

Class Method Summary collapse

Class Method Details

.default_optionsObject



17
18
19
# File 'lib/erbh.rb', line 17

def self.default_options
  @default_options
end

.define_method(name, &block) ⇒ Object



13
14
15
# File 'lib/erbh.rb', line 13

def self.define_method(name, &block)
  @methods[name] = block
end

.erbh(str, variables = {}, options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/erbh.rb', line 25

def erbh(str, variables = {}, options = {})
  options = ERBh.default_options.merge(options)
  context = Object.new

  variables.each do |name, value|
    context.instance_variable_set("@#{name}", value)
  end

  class << context
    ERBh.erbh_methods.each do |name, block|
      define_method(name, block)
    end
  end

  context.instance_eval do
    erb = ERB.new("<% @#{options[:eoutvar]} = #{options[:eoutvar]} %>\n" + str, **options)
    erb.result(binding).sub(/\A\n/, '')
  end
end

.erbh_methodsObject



21
22
23
# File 'lib/erbh.rb', line 21

def self.erbh_methods
  @methods
end