Class: SimpleHdGraph::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/simple-hd-graph/command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser: Parser.new, reader: Reader.new, renderer: :plantuml) ⇒ Command

Returns a new instance of Command.

Parameters:

  • parser (Parser) (defaults to: Parser.new)
  • reader (Reader) (defaults to: Reader.new)
  • renderer (Symbol) (defaults to: :plantuml)


13
14
15
16
17
# File 'lib/simple-hd-graph/command.rb', line 13

def initialize(parser: Parser.new, reader: Reader.new, renderer: :plantuml)
  @parser = parser
  @reader = reader
  @renderer = SimpleHdGraph::Renderer.method(renderer.to_s)
end

Instance Attribute Details

#parserObject (readonly)

Returns the value of attribute parser.



18
19
20
# File 'lib/simple-hd-graph/command.rb', line 18

def parser
  @parser
end

#readerObject (readonly)

Returns the value of attribute reader.



18
19
20
# File 'lib/simple-hd-graph/command.rb', line 18

def reader
  @reader
end

#rendererObject (readonly)

Returns the value of attribute renderer.



18
19
20
# File 'lib/simple-hd-graph/command.rb', line 18

def renderer
  @renderer
end

Instance Method Details

#optsOptionParser

:reek:NestedIterators, :reek:DuplicateMethodCall

Returns:

  • (OptionParser)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/simple-hd-graph/command.rb', line 58

def opts
  OptionParser.new do |opt|
    opt.on("-d DIR", "--dir", "dirname") { |value|
      if File.exist?(value) && File.directory?(value)
        @dir = value
      else
        raise DirectoryNotExist, value
      end
    }
    opt.on("-f FILE", "--file", "filename") { |value|
      if File.exist?(value) && File.file?(value)
        @file = value
      else
        raise FileNotExist, value
      end
    }
    opt.on("-r RENDERER", "--renderer", "renderer") { |value|
      begin
        @renderer = SimpleHdGraph::Renderer.method(value)
      rescue NameError
        warn "[Warining] renderer `#{value}` not found. falling back to :plantuml"
      end
    }
  end
end

#parse(argv) ⇒ Object

Parameters:

  • argv (Array)


33
34
35
# File 'lib/simple-hd-graph/command.rb', line 33

def parse(argv)
  opts.parse(argv)
end

#run(argv) ⇒ Object

Parameters:

  • argv (Array)


23
24
25
26
27
28
# File 'lib/simple-hd-graph/command.rb', line 23

def run(argv)
  parse(argv)
  if @file || @dir
    start
  end
end

#startObject



48
49
50
51
52
# File 'lib/simple-hd-graph/command.rb', line 48

def start
  nodes = parser.parse(stream)

  @renderer.call(nodes)
end

#streamString

Returns:

  • (String)


40
41
42
43
44
45
46
# File 'lib/simple-hd-graph/command.rb', line 40

def stream
  if @dir
    reader.read_dir(@dir)
  else
    reader.read_file(@file)
  end
end