Class: Hiiro::Runners
- Inherits:
-
Object
- Object
- Hiiro::Runners
- Defined in:
- lib/hiiro.rb
Defined Under Namespace
Classes: Bin, Subcommand
Instance Attribute Summary collapse
-
#bin_name ⇒ Object
readonly
Returns the value of attribute bin_name.
-
#hiiro ⇒ Object
readonly
Returns the value of attribute hiiro.
-
#subcmd ⇒ Object
readonly
Returns the value of attribute subcmd.
-
#subcommands ⇒ Object
readonly
Returns the value of attribute subcommands.
Instance Method Summary collapse
- #add_default(handler, **values) ⇒ Object
- #add_subcommand(name, handler, opts: nil, subcmd_args: [], subcmd_opts: nil, **values) ⇒ Object
- #all_bins ⇒ Object
- #all_runners ⇒ Object
- #ambiguous_matches ⇒ Object
- #exact_runner ⇒ Object
-
#initialize(hiiro) ⇒ Runners
constructor
A new instance of Runners.
- #matching_runners ⇒ Object
- #paths ⇒ Object
- #remove_child_runners(list) ⇒ Object
- #run_subcommand(name, *args) ⇒ Object
- #runner ⇒ Object
- #subcmd_result ⇒ Object
- #subcommand_names ⇒ Object
- #unambiguous_runner ⇒ Object
- #using_default? ⇒ Boolean
Constructor Details
#initialize(hiiro) ⇒ Runners
Returns a new instance of Runners.
566 567 568 569 570 571 572 |
# File 'lib/hiiro.rb', line 566 def initialize(hiiro) @hiiro = hiiro @bin_name = hiiro.bin_name @subcmd = hiiro.subcmd @subcommands = {} @default_subcommand = hiiro.default_subcommand end |
Instance Attribute Details
#bin_name ⇒ Object (readonly)
Returns the value of attribute bin_name.
564 565 566 |
# File 'lib/hiiro.rb', line 564 def bin_name @bin_name end |
#hiiro ⇒ Object (readonly)
Returns the value of attribute hiiro.
564 565 566 |
# File 'lib/hiiro.rb', line 564 def hiiro @hiiro end |
#subcmd ⇒ Object (readonly)
Returns the value of attribute subcmd.
564 565 566 |
# File 'lib/hiiro.rb', line 564 def subcmd @subcmd end |
#subcommands ⇒ Object (readonly)
Returns the value of attribute subcommands.
564 565 566 |
# File 'lib/hiiro.rb', line 564 def subcommands @subcommands end |
Instance Method Details
#add_default(handler, **values) ⇒ Object
574 575 576 577 578 579 580 581 |
# File 'lib/hiiro.rb', line 574 def add_default(handler, **values) @default_subcommand = Subcommand.new( bin_name, :DEFAULT, handler, values ) end |
#add_subcommand(name, handler, opts: nil, subcmd_args: [], subcmd_opts: nil, **values) ⇒ Object
612 613 614 615 616 617 618 619 |
# File 'lib/hiiro.rb', line 612 def add_subcommand(name, handler, opts: nil, subcmd_args: [], subcmd_opts: nil, **values) @subcommands[name] = Subcommand.new( bin_name, name, handler, values, opts: opts, subcmd_args: subcmd_args, subcmd_opts: subcmd_opts ) end |
#all_bins ⇒ Object
606 607 608 609 610 |
# File 'lib/hiiro.rb', line 606 def all_bins pattern = format('{%s}/%s-*', paths.join(?,), bin_name) Dir.glob(pattern).map { |path| Bin.new(bin_name, path) } end |
#all_runners ⇒ Object
598 599 600 |
# File 'lib/hiiro.rb', line 598 def all_runners [*all_bins, *subcommands.values] end |
#ambiguous_matches ⇒ Object
642 643 644 645 646 |
# File 'lib/hiiro.rb', line 642 def ambiguous_matches return [] unless subcmd_result matches = matching_runners matches.count > 1 ? matches : [] end |
#exact_runner ⇒ Object
632 633 634 |
# File 'lib/hiiro.rb', line 632 def exact_runner subcmd_result&.exact_match&.item end |
#matching_runners ⇒ Object
648 649 650 651 |
# File 'lib/hiiro.rb', line 648 def matching_runners return [] unless subcmd_result remove_child_runners(subcmd_result.matches.map(&:item)) end |
#paths ⇒ Object
602 603 604 |
# File 'lib/hiiro.rb', line 602 def paths @paths ||= ENV['PATH'].split(?:).uniq end |
#remove_child_runners(list) ⇒ Object
653 654 655 656 657 |
# File 'lib/hiiro.rb', line 653 def remove_child_runners(list) list.reject do |r| list.any? { |other| r != other && r.full_name.start_with?(other.full_name + ?-) } end end |
#run_subcommand(name, *args) ⇒ Object
621 622 623 624 625 |
# File 'lib/hiiro.rb', line 621 def run_subcommand(name, *args) cmd = subcommands[name] cmd&.run(*args) end |
#runner ⇒ Object
583 584 585 586 587 588 |
# File 'lib/hiiro.rb', line 583 def runner return exact_runner if exact_runner return unambiguous_runner if unambiguous_runner @default_subcommand end |
#subcmd_result ⇒ Object
627 628 629 630 |
# File 'lib/hiiro.rb', line 627 def subcmd_result return nil unless subcmd @subcmd_result ||= Matcher.by_prefix(all_runners, subcmd.to_s, key: :subcommand_name) end |
#subcommand_names ⇒ Object
594 595 596 |
# File 'lib/hiiro.rb', line 594 def subcommand_names all_runners.map(&:subcommand_name) end |
#unambiguous_runner ⇒ Object
636 637 638 639 640 |
# File 'lib/hiiro.rb', line 636 def unambiguous_runner return nil unless subcmd_result matches = matching_runners matches.first if matches.count == 1 end |
#using_default? ⇒ Boolean
590 591 592 |
# File 'lib/hiiro.rb', line 590 def using_default? !exact_runner && !unambiguous_runner end |