Class: UCD::Interface::CommandLine

Inherits:
Base
  • Object
show all
Defined in:
lib/ucd/interface/command_line.rb

Defined Under Namespace

Classes: Error, FileError

Instance Method Summary collapse

Methods inherited from Base

run

Methods included from HasAttributes

#update_attributes

Constructor Details

#initialize(attributes = {}) ⇒ CommandLine

Returns a new instance of CommandLine.



15
16
17
18
19
20
21
22
23
# File 'lib/ucd/interface/command_line.rb', line 15

def initialize(attributes={})
  @formatter     = Formatter::Graphviz.new
  @verbose       = false
  @option_parser = OptionParser.new

  setup_parser_options

  super
end

Instance Method Details

#formatter=(value) ⇒ Object

Raises:



33
34
35
36
37
38
39
# File 'lib/ucd/interface/command_line.rb', line 33

def formatter=(value)
  value = value.to_s.strip.downcase.to_sym
  value = Formatter.find_by_name(value)
  raise Error, "Formatter not found: #{value}" if value.nil?

  @formatter = value
end

#output_path=(value) ⇒ Object



25
26
27
# File 'lib/ucd/interface/command_line.rb', line 25

def output_path=(value)
  @output_path = value.is_a?(Pathname) ? value : Pathname.new(value.to_s) unless value.nil?
end

#paths=(values) ⇒ Object



29
30
31
# File 'lib/ucd/interface/command_line.rb', line 29

def paths=(values)
  @paths = values.to_a.map { |path| Pathname.new(path) }
end

#runObject

Raises:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ucd/interface/command_line.rb', line 41

def run
  args = ARGV.dup # TODO: This is hacky
  @option_parser.parse!(args) rescue nil
  setup_parser_formatter_options
  @option_parser.parse!

  self.paths      = ARGV
  @formatter.type = @type

  raise Error, "Output path must be a directory if multiple input files are given" if @output_path && @output_path.file? && @paths.length > 1

  @paths.each do |input_path|
    raise FileError, "File does not exist: #{input_path}" unless input_path.exist?

    data     = input_path.read
    document = Parser.parse(data)
    result   = @formatter.format(document)

    if @output_path
      output_path = @output_path
      output_path = output_path.join(input_path.basename(".*").to_s + ".#{@formatter.type}") if output_path.directory?

      output_path.open("w+") { |file| file.write(result) }
    else
      puts result
    end

  end
end