Class: Haml::Exec::Generic
Overview
An abstract class that encapsulates the executable code for all three executables.
Instance Method Summary collapse
-
#get_line(exception) ⇒ String
protected
Finds the line of the source template on which an exception was raised.
-
#initialize(args) ⇒ Generic
constructor
A new instance of Generic.
-
#parse! ⇒ Object
Parses the command-line arguments and runs the executable.
-
#process_result ⇒ Object
protected
Processes the options set by the command-line arguments.
-
#set_opts(opts) ⇒ Object
protected
Tells optparse how to parse the arguments available for all executables.
-
#to_s ⇒ String
A description of the executable.
Constructor Details
#initialize(args) ⇒ Generic
Returns a new instance of Generic.
10 11 12 13 |
# File 'lib/haml/exec.rb', line 10
def initialize(args)
@args = args
@options = {}
end
|
Instance Method Details
#get_line(exception) ⇒ String (protected)
Finds the line of the source template on which an exception was raised.
46 47 48 49 50 51 52 |
# File 'lib/haml/exec.rb', line 46
def get_line(exception)
# SyntaxErrors have weird line reporting
# when there's trailing whitespace,
# which there is for Haml documents.
return exception.message.scan(/:(\d+)/).first.first if exception.is_a?(::SyntaxError)
exception.backtrace[0].scan(/:(\d+)/).first.first
end
|
#parse! ⇒ Object
Parses the command-line arguments and runs the executable.
Calls Kernel#exit
at the end, so it never returns.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/haml/exec.rb', line 17
def parse!
begin
@opts = OptionParser.new(&method(:set_opts))
@opts.parse!(@args)
process_result
@options
rescue Exception => e
raise e if @options[:trace] || e.is_a?(SystemExit)
$stderr.puts e.message
exit 1
end
exit 0
end
|
#process_result ⇒ Object (protected)
Processes the options set by the command-line arguments.
In particular, sets @options[:input]
and @options[:output]
to appropriate IO streams.
This is meant to be overridden by subclasses so they can run their respective programs.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/haml/exec.rb', line 87
def process_result
input, output = @options[:input], @options[:output]
input_file, output_file = if input
[nil, open_file(@args[0], 'w')]
else
@options[:filename] = @args[0]
[open_file(@args[0]), open_file(@args[1], 'w')]
end
input ||= input_file
output ||= output_file
input ||= $stdin
output ||= $stdout
@options[:input], @options[:output] = input, output
end
|
#set_opts(opts) ⇒ Object (protected)
Tells optparse how to parse the arguments available for all executables.
This is meant to be overridden by subclasses so they can add their own options.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/haml/exec.rb', line 61
def set_opts(opts)
opts.on('-s', '--stdin', :NONE, 'Read input from standard input instead of an input file') do
@options[:input] = $stdin
end
opts.on('--trace', :NONE, 'Show a full traceback on error') do
@options[:trace] = true
end
opts.on_tail("-?", "-h", "--help", "Show this message") do
puts opts
exit
end
opts.on_tail("-v", "--version", "Print version") do
puts("Haml/Sass #{::Haml.version[:string]}")
exit
end
end
|
#to_s ⇒ String
Returns A description of the executable.
35 36 37 |
# File 'lib/haml/exec.rb', line 35
def to_s
@opts.to_s
end
|