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.
568 569 570 571 572 573 574 |
# File 'lib/hiiro.rb', line 568 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.
566 567 568 |
# File 'lib/hiiro.rb', line 566 def bin_name @bin_name end |
#hiiro ⇒ Object (readonly)
Returns the value of attribute hiiro.
566 567 568 |
# File 'lib/hiiro.rb', line 566 def hiiro @hiiro end |
#subcmd ⇒ Object (readonly)
Returns the value of attribute subcmd.
566 567 568 |
# File 'lib/hiiro.rb', line 566 def subcmd @subcmd end |
#subcommands ⇒ Object (readonly)
Returns the value of attribute subcommands.
566 567 568 |
# File 'lib/hiiro.rb', line 566 def subcommands @subcommands end |
Instance Method Details
#add_default(handler, **values) ⇒ Object
576 577 578 579 580 581 582 583 |
# File 'lib/hiiro.rb', line 576 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
614 615 616 617 618 619 620 621 |
# File 'lib/hiiro.rb', line 614 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
608 609 610 611 612 |
# File 'lib/hiiro.rb', line 608 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
600 601 602 |
# File 'lib/hiiro.rb', line 600 def all_runners [*all_bins, *subcommands.values] end |
#ambiguous_matches ⇒ Object
644 645 646 647 648 |
# File 'lib/hiiro.rb', line 644 def ambiguous_matches return [] unless subcmd_result matches = matching_runners matches.count > 1 ? matches : [] end |
#exact_runner ⇒ Object
634 635 636 |
# File 'lib/hiiro.rb', line 634 def exact_runner subcmd_result&.exact_match&.item end |
#matching_runners ⇒ Object
650 651 652 653 |
# File 'lib/hiiro.rb', line 650 def matching_runners return [] unless subcmd_result remove_child_runners(subcmd_result.matches.map(&:item)) end |
#paths ⇒ Object
604 605 606 |
# File 'lib/hiiro.rb', line 604 def paths @paths ||= ENV['PATH'].split(?:).uniq end |
#remove_child_runners(list) ⇒ Object
655 656 657 658 659 |
# File 'lib/hiiro.rb', line 655 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
623 624 625 626 627 |
# File 'lib/hiiro.rb', line 623 def run_subcommand(name, *args) cmd = subcommands[name] cmd&.run(*args) end |
#runner ⇒ Object
585 586 587 588 589 590 |
# File 'lib/hiiro.rb', line 585 def runner return exact_runner if exact_runner return unambiguous_runner if unambiguous_runner @default_subcommand end |
#subcmd_result ⇒ Object
629 630 631 632 |
# File 'lib/hiiro.rb', line 629 def subcmd_result return nil unless subcmd @subcmd_result ||= Matcher.by_prefix(all_runners, subcmd.to_s, key: :subcommand_name) end |
#subcommand_names ⇒ Object
596 597 598 |
# File 'lib/hiiro.rb', line 596 def subcommand_names all_runners.map(&:subcommand_name) end |
#unambiguous_runner ⇒ Object
638 639 640 641 642 |
# File 'lib/hiiro.rb', line 638 def unambiguous_runner return nil unless subcmd_result matches = matching_runners matches.first if matches.count == 1 end |
#using_default? ⇒ Boolean
592 593 594 |
# File 'lib/hiiro.rb', line 592 def using_default? !exact_runner && !unambiguous_runner end |