Class: RubyBytes::Compiler
- Inherits:
-
Object
- Object
- RubyBytes::Compiler
- Defined in:
- lib/ruby_bytes/compiler.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#template ⇒ Object
readonly
Returns the value of attribute template.
Instance Method Summary collapse
- #code(path) ⇒ Object
- #include(path, indent: 0) ⇒ Object
-
#initialize(path, root: nil) ⇒ Compiler
constructor
A new instance of Compiler.
- #render(contents = template) ⇒ Object
Constructor Details
#initialize(path, root: nil) ⇒ Compiler
Returns a new instance of Compiler.
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.(path)) end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
7 8 9 |
# File 'lib/ruby_bytes/compiler.rb', line 7 def path @path end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
7 8 9 |
# File 'lib/ruby_bytes/compiler.rb', line 7 def root @root end |
#template ⇒ Object (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 |