Class: RubyBytes::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_bytes/compiler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, root: nil) ⇒ Compiler

Returns a new instance of Compiler.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
# File 'lib/ruby_bytes/compiler.rb', line 9

def initialize(path, root: nil)
  @path = path
  raise ArgumentError, "There is no file at the path: #{path}" unless File.file?(path)

  @template = File.read(path)
  @root = root || File.dirname(File.expand_path(path))
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/ruby_bytes/compiler.rb', line 7

def path
  @path
end

#rootObject (readonly)

Returns the value of attribute root.



7
8
9
# File 'lib/ruby_bytes/compiler.rb', line 7

def root
  @root
end

#templateObject (readonly)

Returns the value of attribute template.



7
8
9
# File 'lib/ruby_bytes/compiler.rb', line 7

def template
  @template
end

Instance Method Details

#code(path) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/ruby_bytes/compiler.rb', line 21

def code(path)
  contents = File.read(resolve_path(path))
  %(ERB.new(
*[
  <<~'TCODE'
#{contents}
  TCODE
  ], trim_mode: "<>").result(binding))
end

#include(path, indent: 0) ⇒ Object



31
32
33
# File 'lib/ruby_bytes/compiler.rb', line 31

def include(path, indent: 0)
  indented(render(File.read(resolve_path(path))), indent)
end

#render(contents = template) ⇒ Object



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

def render(contents = template)
  ERB.new(contents, trim_mode: "<>").result(binding)
end