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