Class: RFormat::Application
- Inherits:
-
Object
- Object
- RFormat::Application
- Defined in:
- lib/rformat/application.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#environment ⇒ Object
readonly
Returns the value of attribute environment.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #help ⇒ Object
-
#initialize(output = $stdout, env = RFormat::Environment.new) ⇒ Application
constructor
A new instance of Application.
- #list ⇒ Object
- #parse_command ⇒ Object
- #parse_options ⇒ Object
- #run ⇒ Object
- #version ⇒ Object
- #write(formats) ⇒ Object
Constructor Details
#initialize(output = $stdout, env = RFormat::Environment.new) ⇒ Application
Returns a new instance of Application.
22 23 24 25 26 |
# File 'lib/rformat/application.rb', line 22 def initialize(output = $stdout, env = RFormat::Environment.new) @output = output @environment = env @args = Array.try_convert(ARGV) end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
17 18 19 |
# File 'lib/rformat/application.rb', line 17 def args @args end |
#command ⇒ Object (readonly)
Returns the value of attribute command.
17 18 19 |
# File 'lib/rformat/application.rb', line 17 def command @command end |
#environment ⇒ Object (readonly)
Returns the value of attribute environment.
17 18 19 |
# File 'lib/rformat/application.rb', line 17 def environment @environment end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
17 18 19 |
# File 'lib/rformat/application.rb', line 17 def end |
Instance Method Details
#help ⇒ Object
40 41 42 |
# File 'lib/rformat/application.rb', line 40 def help @output.puts File.read File.join(RFormat::root_dir, 'HELP.txt') end |
#list ⇒ Object
44 45 46 47 |
# File 'lib/rformat/application.rb', line 44 def list @output.puts "Formatters:" @output.puts @environment.formatters.keys.map { |f| " #{f}\n" } end |
#parse_command ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/rformat/application.rb', line 72 def parse_command command = :help if @args.empty? if @args.first && @args.first == 'list' command = :list @args.shift end command = :write if command.nil? @command = command end |
#parse_options ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/rformat/application.rb', line 82 def = {} OptionParser.new do |opts| opts.on("-v", "--version", "Version info") do [:version] = true version end opts.on('-h', '--help', 'Display help') do [:help] = true help end end.parse! end |
#run ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/rformat/application.rb', line 28 def run parse_command if @command && @command !~ /^-/ @args.empty? ? self.send(@command) : self.send(@command, @args) else help end end |
#version ⇒ Object
49 50 51 52 53 |
# File 'lib/rformat/application.rb', line 49 def version version = RFormat::Version copyright = RFormat::Copyright @output.puts "#{version}\n#{copyright}" end |
#write(formats) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/rformat/application.rb', line 55 def write(formats) config = { color: true, formats: {} } formats.each do |format| config[:formats][format] = @environment.formatters[format] if @environment.formatters[format] end @environment.config = config if config[:formats].empty? @output.puts "No formatters matching #{formats.join(', ')}" else @environment.write_config @output.puts "rspec now using format(s): #{config[:formats].values.join(', ')}" end end |