Class: Docs::Example

Inherits:
Object
  • Object
show all
Defined in:
lib/docs/example.rb

Constant Summary collapse

EXTENSIONS_TO_COMMANDS =
{
  '.sh' => 'bash',
  '.rb' => 'ruby'
}

Instance Method Summary collapse

Constructor Details

#initialize(example_name, erb_renderer = nil) ⇒ Example


13
14
15
16
17
18
19
20
21
22
# File 'lib/docs/example.rb', line 13

def initialize(example_name, erb_renderer=nil)
  @config = Config.new
  @erb = erb_renderer || new_erb_renderer 

  @source = nil
  @output = nil

  @file_name = example_name
  @rendered_file_name = nil
end

Instance Method Details

#is_erb?Boolean


39
40
41
# File 'lib/docs/example.rb', line 39

def is_erb?
  @is_erb ||= File.fnmatch?('*.erb.*', @file_name)
end

#outputObject


24
25
26
27
28
29
30
31
32
33
# File 'lib/docs/example.rb', line 24

def output
  return @output if @output

  source if (is_erb? && !@rendered_file_name)

  file_name = @rendered_file_name || @file_name
  i = IO.popen("#{EXTENSIONS_TO_COMMANDS[File.extname(file_name)]} #{file_name}")

  @output = i.read
end

#sourceObject


35
36
37
# File 'lib/docs/example.rb', line 35

def source
  @source ||= (is_erb?) ? render_erb_template : File.read(@file_name)
end