Class: SimpleHdGraph::Command
- Inherits:
-
Object
- Object
- SimpleHdGraph::Command
- Defined in:
- lib/simple-hd-graph/command.rb
Instance Attribute Summary collapse
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
-
#reader ⇒ Object
readonly
Returns the value of attribute reader.
-
#renderer ⇒ Object
readonly
Returns the value of attribute renderer.
Instance Method Summary collapse
-
#initialize(parser: Parser.new, reader: Reader.new, renderer: :plantuml) ⇒ Command
constructor
A new instance of Command.
-
#opts ⇒ OptionParser
:reek:NestedIterators, :reek:DuplicateMethodCall.
- #parse(argv) ⇒ Object
- #run(argv) ⇒ Object
- #start ⇒ Object
- #stream ⇒ String
Constructor Details
#initialize(parser: Parser.new, reader: Reader.new, renderer: :plantuml) ⇒ Command
Returns a new instance of Command.
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
#parser ⇒ Object (readonly)
Returns the value of attribute parser.
18 19 20 |
# File 'lib/simple-hd-graph/command.rb', line 18 def parser @parser end |
#reader ⇒ Object (readonly)
Returns the value of attribute reader.
18 19 20 |
# File 'lib/simple-hd-graph/command.rb', line 18 def reader @reader end |
#renderer ⇒ Object (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
#opts ⇒ OptionParser
:reek:NestedIterators, :reek:DuplicateMethodCall
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
33 34 35 |
# File 'lib/simple-hd-graph/command.rb', line 33 def parse(argv) opts.parse(argv) end |
#run(argv) ⇒ Object
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 |
#start ⇒ Object
48 49 50 51 52 |
# File 'lib/simple-hd-graph/command.rb', line 48 def start nodes = parser.parse(stream) @renderer.call(nodes) end |
#stream ⇒ 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 |