Module: Thor::Base::SharedConcern

Extended by:
ActiveSupport::Concern
Defined in:
lib/thor/base/shared_concern.rb

Overview

Share stuff… define Argument, Option and whatever else you like once then apply those definitions to many commands.

Class Method Summary collapse

Class Method Details

.def_shared(kind, name:, groups: nil, **options) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/thor/base/shared_concern.rb', line 51

def def_shared kind, name:, groups: nil, **options
  shared_defs << {
    name: name.to_s,
    kind: kind,
    groups: Set[*groups],
    options: options,
  }
end

.find_shared(*names, kinds: nil, groups: nil) ⇒ Hash<Symbol, Thor::SharedOption>

Find shared options given names and groups.

Parameters:

  • names (*<Symbol>)

    Individual shared option names to include.

  • groups: (nil | Symbol | Enumerable<Symbol>) (defaults to: nil)

    Single or list of shared option groups to include.

Returns:

  • (Hash<Symbol, Thor::SharedOption>)

    Hash mapping option names (as Symbol) to instances.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/thor/base/shared_concern.rb', line 80

def find_shared *names, kinds: nil, groups: nil
  groups_set = Set[*groups]
  kinds_set = Set[*kinds]
  names.map! &:to_s

  results = []
  
  shared_defs.each do |name:, kind:, groups:, options:|
    match = {}
    
    if names.include? name
      match[:name] = true
    end
    
    match_groups = groups & groups_set
      
    unless match_groups.empty?
      match[:groups] = match_groups
    end

    if kinds_set.include? kind
      match[:kind] = true
    end
    
    unless match.empty?
      results << {
        name: name,
        kind: kind,
        groups: groups,
        options: options,
      }
    end
  end
end

.include_shared(*names, kinds: nil, groups: nil, **overrides) ⇒ Object



61
62
63
64
65
66
# File 'lib/thor/base/shared_concern.rb', line 61

def include_shared *names, kinds: nil, groups: nil, **overrides
  find_shared( *names, kinds: kinds, groups: groups ).
    each do |name:, kind:, groups:, options:|
      send kind, name, **options.merge( overrides )
    end
end

.shared_defsObject



38
39
40
# File 'lib/thor/base/shared_concern.rb', line 38

def shared_defs
  @shared_defs ||= []
end