Class: DNote::Format::ErbScope

Inherits:
Object
  • Object
show all
Defined in:
lib/dnote/format.rb

Overview

Evaluation scope for ERB templates

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ ErbScope



120
121
122
# File 'lib/dnote/format.rb', line 120

def initialize(data = {})
  @data = data
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *_args) ⇒ Object



134
135
136
137
138
139
# File 'lib/dnote/format.rb', line 134

def method_missing(method, *_args)
  sym = method.to_sym
  return @data.fetch(sym) if @data.key? sym

  super
end

Instance Method Details

#h(string) ⇒ Object



130
131
132
# File 'lib/dnote/format.rb', line 130

def h(string)
  REXML::Text.normalize(string)
end

#render(file) ⇒ Object



124
125
126
127
128
# File 'lib/dnote/format.rb', line 124

def render(file)
  contents = File.read(file)
  erb = ERB.new(contents, trim_mode: "<>")
  erb.result(binding)
end

#respond_to_missing?(method) ⇒ Boolean



141
142
143
# File 'lib/dnote/format.rb', line 141

def respond_to_missing?(method)
  @data.key?(method.to_sym) || super
end