Class: Epuber::RubyTemplater

Inherits:
Object
  • Object
show all
Defined in:
lib/epuber/vendor/ruby_templater.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#file_pathString

Returns:

  • (String)


13
14
15
# File 'lib/epuber/vendor/ruby_templater.rb', line 13

def file_path
  @file_path
end

#localsHash

Returns:

  • (Hash)


17
18
19
# File 'lib/epuber/vendor/ruby_templater.rb', line 17

def locals
  @locals
end

#source_textString

Returns:

  • (String)


9
10
11
# File 'lib/epuber/vendor/ruby_templater.rb', line 9

def source_text
  @source_text
end

Class Method Details

.from_file(file) ⇒ self

Parameters:

  • file (String, File)

Returns:

  • (self)


34
35
36
37
38
39
40
41
42
# File 'lib/epuber/vendor/ruby_templater.rb', line 34

def self.from_file(file)
  file_obj = if file.is_a?(String)
               File.new(file, 'r')
             else
               file
             end

  from_source(file_obj.read, file_obj.path)
end

.from_source(source, file_path = nil) ⇒ self

Parameters:

  • source (String)

Returns:

  • (self)


23
24
25
26
27
28
# File 'lib/epuber/vendor/ruby_templater.rb', line 23

def self.from_source(source, file_path = nil)
  inst = new
  inst.source_text = source
  inst.file_path = file_path
  inst
end

Instance Method Details

#renderString

Returns:

  • (String)


60
61
62
63
64
# File 'lib/epuber/vendor/ruby_templater.rb', line 60

def render
  hash_binding = HashBinding.new(locals)
  eval_string = %(%(#{source_text}))
  eval(eval_string, hash_binding.get_binding) # rubocop:disable Security/Eval
end

#with_locals(locals = {}) ⇒ self

Parameters:

  • locals (Hash) (defaults to: {})

Returns:

  • (self)


53
54
55
56
# File 'lib/epuber/vendor/ruby_templater.rb', line 53

def with_locals(locals = {})
  self.locals = locals
  self
end