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

.copy_shared_defs_to(array, inherited: true) ⇒ Object (protected)



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/thor/base/shared_concern.rb', line 47

def copy_shared_defs_to array, inherited: true
  array.push *this_class_shared_defs_ref
  
  if  inherited &&
      superclass &&
      superclass.respond_to?( :copy_shared_defs_to, true )
    superclass.copy_shared_defs_to array
  end
  
  array
end

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



69
70
71
72
73
74
75
76
# File 'lib/thor/base/shared_concern.rb', line 69

def def_shared kind, name:, groups: nil, **options
  this_class_shared_defs_ref << {
    name: name.to_sym,
    kind: kind.to_sym,
    groups: Set[*groups].freeze,
    options: options.freeze,
  }.freeze
end

.include_shared(selector, **overrides) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/thor/base/shared_concern.rb', line 79

def include_shared selector, **overrides
  defs = shared_defs.select &selector
  
  if defs.empty?
    logger.warn "No shared parameters found",
      selector: selector,
      class: self
  end
  
  defs.each do |name:, kind:, groups:, options:|
    send kind, name, **options.merge( overrides )
  end
end

.shared_defs(inherited: true) ⇒ Object



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

def shared_defs inherited: true
  [].tap do |array|
    copy_shared_defs_to array, inherited: inherited
  end
end

.this_class_shared_defs_refObject (protected)



42
43
44
# File 'lib/thor/base/shared_concern.rb', line 42

def this_class_shared_defs_ref
  @shared_defs ||= []
end