Class: RFormat::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/rformat/application.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#argsObject (readonly)

Returns the value of attribute args.



17
18
19
# File 'lib/rformat/application.rb', line 17

def args
  @args
end

#commandObject (readonly)

Returns the value of attribute command.



17
18
19
# File 'lib/rformat/application.rb', line 17

def command
  @command
end

#environmentObject (readonly)

Returns the value of attribute environment.



17
18
19
# File 'lib/rformat/application.rb', line 17

def environment
  @environment
end

#optionsObject (readonly)

Returns the value of attribute options.



17
18
19
# File 'lib/rformat/application.rb', line 17

def options
  @options
end

Instance Method Details

#helpObject



40
41
42
# File 'lib/rformat/application.rb', line 40

def help
  @output.puts File.read File.join(RFormat::root_dir, 'HELP.txt')
end

#listObject



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_commandObject



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_optionsObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rformat/application.rb', line 82

def parse_options
  @options = {}
  OptionParser.new do |opts|
    opts.on("-v", "--version", "Version info") do
      @options[:version] = true
      version
    end

    opts.on('-h', '--help', 'Display help') do
      @options[:help] = true
      help
    end
  end.parse!
end

#runObject



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

def run
  parse_command
  parse_options

  if @command && @command !~ /^-/
      @args.empty? ? self.send(@command) : self.send(@command, @args)
  else
    help
  end

end

#versionObject



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