Module: Plumbum::Consumers::ClassMethods

Included in:
Plumbum::Consumer::ClassMethods, ScopedConsumer::ClassMethods
Defined in:
lib/plumbum/consumers/class_methods.rb

Overview

Class methods for defining the Consumer interface.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.define_delegated_method(receiver, key:, method_name:, path:, **options) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/plumbum/consumers/class_methods.rb', line 24

def define_delegated_method( # rubocop:disable Metrics/MethodLength
  receiver,
  key:,
  method_name:,
  path:,
  **options
)
  validate_delegated_method_options(path:, **options)

  *path, inner_name = path
  method_name       = method_name[1..] if method_name.start_with?('#')
  inner_name        = inner_name[1..]

  dependency_methods_for(receiver)
    .define_method(method_name) do |*args, **keywords, &block|
      inner = get_scoped_plumbum_dependency(key, path:)

      inner.public_send(inner_name, *args, **keywords, &block)
    end # rubocop:disable Style/MultilineBlockChain
    .tap do |method_name|
      receiver.send(:private, method_name) if options[:private]
    end
end

.define_memoized_reader(receiver, default:, key:, method_name:, optional:, path:, **options) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/plumbum/consumers/class_methods.rb', line 49

def define_memoized_reader( # rubocop:disable Metrics/MethodLength, Metrics/ParameterLists
  receiver,
  default:,
  key:,
  method_name:,
  optional:,
  path:,
  **options
)
  dependency_methods_for(receiver)
    .define_method(method_name) do
      if (@plumbum_dependencies ||= {}).key?(key)
        return @plumbum_dependencies[key]
      end

      get_scoped_plumbum_dependency(key, default:, optional:, path:)
        .tap do |value|
          @plumbum_dependencies[key] = value unless value.nil?
        end
    end # rubocop:disable Style/MultilineBlockChain
    .tap do |method_name|
      receiver.send(:private, method_name) if options[:private]
    end
end

.define_methods(receiver, key:, method_name:, memoize:, predicate:) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/plumbum/consumers/class_methods.rb', line 75

def define_methods(receiver, key:, method_name:, memoize:, predicate:, **) # rubocop:disable Metrics/ParameterLists
  define_predicate(receiver, key:, method_name:, **) if predicate

  if memoize
    define_memoized_reader(receiver, key:, method_name:, **)
  else
    define_reader(receiver, key:, method_name:, **)
  end

  method_name.to_sym
end

.define_predicate(receiver, key:, method_name:, **options) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/plumbum/consumers/class_methods.rb', line 88

def define_predicate(receiver, key:, method_name:, **options)
  method_name = :"#{method_name}?"

  dependency_methods_for(receiver)
    .define_method(method_name) do
      has_plumbum_dependency?(key)
    end # rubocop:disable Style/MultilineBlockChain
    .tap do |method_name|
      receiver.send(:private, method_name) if options[:private]
    end
end

.define_reader(receiver, default:, key:, method_name:, optional:, path:, **options) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/plumbum/consumers/class_methods.rb', line 101

def define_reader( # rubocop:disable Metrics/ParameterLists
  receiver,
  default:,
  key:,
  method_name:,
  optional:,
  path:,
  **options
)
  dependency_methods_for(receiver)
    .define_method(method_name) do
      get_scoped_plumbum_dependency(key, default:, optional:, path:)
    end # rubocop:disable Style/MultilineBlockChain
    .tap do |method_name|
      receiver.send(:private, method_name) if options[:private]
    end
end

.dependency_methods_for(receiver) ⇒ Object



120
121
122
123
124
125
126
127
128
129
# File 'lib/plumbum/consumers/class_methods.rb', line 120

def dependency_methods_for(receiver)
  if receiver.const_defined?(:PlumbumDependencyMethods, false)
    return receiver.const_get(:PlumbumDependencyMethods)
  end

  Module
    .new
    .tap { |mod| receiver.include mod }
    .then { |mod| receiver.const_set(:PlumbumDependencyMethods, mod) }
end

.split_key(key, as:, scope:) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/plumbum/consumers/class_methods.rb', line 132

def split_key(key, as:, scope:)
  ClassMethods.validate_name(key, as: :key)
  ClassMethods.validate_name(scope, as: :scope) if scope

  key = "#{scope}.#{key}" if scope

  segments = key.to_s.split('.')

  return [key, as || key, nil] if segments.size == 1

  [segments.first, as || segments.last, segments[1..]]
end

.validate_name(value, as: nil) ⇒ Object



146
147
148
149
150
151
# File 'lib/plumbum/consumers/class_methods.rb', line 146

def validate_name(value, as: nil)
  SleepingKingStudios::Tools::Toolbelt
    .instance
    .assertions
    .validate_name(value, as:)
end

Instance Method Details

#plumbum_dependency(*keys, as: nil, memoize: true, optional: false, predicate: false, scope: nil) ⇒ Symbol+

Defines injected dependencies for instances of the class.

Parameters:

  • keys (Array<String, Symbol>)

    the keys for the dependency. A new dependency will be defined for each key using the same options.

  • as (String, Symbol) (defaults to: nil)

    the method name used to define dependency methods. Defaults to the key. Cannot be used with multiple keys.

  • default (Object, Proc)

    if given, the default value will be returned when the consumer does not have a provider for the dependency. If the default value is a Proc, the default will be lazily evaluated in the context of the consumer. A default value *will not* be returned if a matching provider is defined but does not support the given scope.

  • memoize (true, false) (defaults to: true)

    if true, memoizes the value of the dependency the first time it is successfully called. Defaults to true.

  • optional (true, false) (defaults to: false)

    if true, calling the dependency returns nil if the dependency is not defined. Defaults to false.

  • predicate (true, false) (defaults to: false)

    if true, also defines a predicate method that returns true if the dependency has a defined value. Defaults to false.

  • private (true, false)

    if true, the generated methods will be generated with private visibility. Defaults to false.

  • scope (String, Symbol) (defaults to: nil)

    if given, combined with the key or keys to determine the dependency name and the path from the dependency to the returned value.

Returns:

  • (Symbol, Array<Symbol>)

    the name of the generated method, or the method names if given more than one key.

Raises:

  • (ArgumentError)

    if any key is not a String or Symbol, or is empty.



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/plumbum/consumers/class_methods.rb', line 205

def plumbum_dependency( # rubocop:disable Metrics/MethodLength, Metrics/ParameterLists
  *keys,
  as:        nil,
  default:   UNDEFINED,
  memoize:   true,
  optional:  false,
  predicate: false,
  private:   false,
  scope:     nil
)
  if keys.size > 1 && as
    raise ArgumentError, 'invalid option :as when providing multiple keys'
  end

  scoped_keys = keys.map do |key|
    define_plumbum_dependency(
      key,
      as:,
      default:,
      memoize:,
      optional:,
      predicate:,
      private:,
      scope:
    )
  end

  scoped_keys.size == 1 ? scoped_keys.first : scoped_keys
end

#plumbum_dependency_keys(cache: true) ⇒ Set<String>

Returns the keys of the dependencies declared by the class and its ancestors.

Parameters:

  • cache (true, false) (defaults to: true)

    if false,.clears the memoized value and recalculates the keys.

Returns:

  • (Set<String>)

    the keys of the dependencies declared by the class and its ancestors.



240
241
242
243
244
245
246
247
248
249
250
# File 'lib/plumbum/consumers/class_methods.rb', line 240

def plumbum_dependency_keys(cache: true)
  @plumbum_dependency_keys = nil if cache == false

  return @plumbum_dependency_keys if @plumbum_dependency_keys

  @plumbum_dependency_keys = ancestors.reduce(Set.new) do |set, ancestor|
    next set unless ancestor.respond_to?(:own_plumbum_dependency_keys, true)

    set.union(ancestor.own_plumbum_dependency_keys)
  end
end

#plumbum_provider(provider) ⇒ Object

Registers a provider for the class.

Parameters:

  • provider (#get, #has?)

    the provider to register.

Returns:

  • void



257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/plumbum/consumers/class_methods.rb', line 257

def plumbum_provider(provider)
  PROVIDER_METHODS.each do |method_name|
    next if provider.respond_to?(method_name)

    # @todo [0.2] use tools error message for assertions.respond_to
    message = "provider does not respond to ##{method_name}"

    raise ArgumentError, message
  end

  own_plumbum_providers.prepend(provider)

  nil
end

#plumbum_providers(cache: true) ⇒ Array<Plumbum::Provider>

Returns the providers defined for the class.

Parameters:

  • cache (true, false) (defaults to: true)

    if false,.clears the memoized value and reaggregates the providers.

Returns:



276
277
278
279
280
# File 'lib/plumbum/consumers/class_methods.rb', line 276

def plumbum_providers(cache: true)
  @plumbum_providers = nil if cache == false

  @plumbum_providers ||= each_plumbum_provider.to_a
end