Class: HomeworkReport::CommandLineTool

Inherits:
Object
  • Object
show all
Defined in:
lib/homework_report/command_line_tool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CommandLineTool

Returns a new instance of CommandLineTool.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/homework_report/command_line_tool.rb', line 8

def initialize(*args)
  args = args.flatten
  
  self.options = { output: :stdout }
  
  OptionParser.new do |opts|
    opts.banner = "Usage: #{opts.program_name} [options] TEMPLATE_FILE"
    opts.separator "Outcome: a HTML file/text is generated"
    opts.separator "Options:"
    
    command_options opts
  end.parse! args
  
  unless self.template_filepath = args.shift
    raise RuntimeError, "TEMPLATE_FILE is required"
  end
  
  self.scope = Scope.new
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/homework_report/command_line_tool.rb', line 6

def options
  @options
end

#scopeObject

Returns the value of attribute scope.



6
7
8
# File 'lib/homework_report/command_line_tool.rb', line 6

def scope
  @scope
end

#template_filepathObject

Returns the value of attribute template_filepath.



6
7
8
# File 'lib/homework_report/command_line_tool.rb', line 6

def template_filepath
  @template_filepath
end

Instance Method Details

#runObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/homework_report/command_line_tool.rb', line 28

def run
  lambda {|path, &y|
    case path
    when :stdout then y.call $stdout
    when String then File.open path, "w", &y
    else raise RuntimeError
    end
  }.call options.delete(:output) do |output|
    output << scope.render(template_filepath)
    output << "\n"
  end
end