Class: OpsWalrus::LocalImportInvocationContext

Inherits:
ImportInvocationContext show all
Defined in:
lib/opswalrus/invocation.rb

Instance Method Summary collapse

Methods inherited from ImportInvocationContext

#_bang_method?, #_non_bang_method

Constructor Details

#initialize(runtime_env, namespace_or_ops_file) ⇒ LocalImportInvocationContext

Returns a new instance of LocalImportInvocationContext.



162
163
164
165
# File 'lib/opswalrus/invocation.rb', line 162

def initialize(runtime_env, namespace_or_ops_file)
  @runtime_env = runtime_env
  @initial_namespace_or_ops_file = @namespace_or_ops_file = namespace_or_ops_file
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, **kwargs, &block) ⇒ Object



167
168
169
# File 'lib/opswalrus/invocation.rb', line 167

def method_missing(name, *args, **kwargs, &block)
  _resolve_method_and_invoke(name, *args, **kwargs)
end

Instance Method Details

#_invoke(*args, **kwargs) ⇒ Object



196
197
198
199
200
201
202
203
# File 'lib/opswalrus/invocation.rb', line 196

def _invoke(*args, **kwargs)
  case @namespace_or_ops_file
  when Namespace
    self
  when OpsFile
    _invoke_local(*args, **kwargs)
  end
end

#_invoke_if_namespace_has_ops_file_of_same_name(*args, **kwargs) ⇒ Object

if this namespace contains an OpsFile of the same name as the namespace, e.g. pkg/install/install.ops, then this method invokes the OpsFile of that same name and returns the result; otherwise we return this namespace object



185
186
187
188
189
190
191
192
193
194
# File 'lib/opswalrus/invocation.rb', line 185

def _invoke_if_namespace_has_ops_file_of_same_name(*args, **kwargs)
  method_name = @namespace_or_ops_file.dirname.basename
  resolved_symbol = @namespace_or_ops_file.resolve_symbol(method_name)
  if resolved_symbol.is_a? OpsFile
    params_hash = resolved_symbol.build_params_hash(*args, **kwargs)
    resolved_symbol.invoke(@runtime_env, params_hash)
  else
    self
  end
end

#_invoke_local(*args, **kwargs) ⇒ Object



205
206
207
208
# File 'lib/opswalrus/invocation.rb', line 205

def _invoke_local(*args, **kwargs)
  params_hash = @namespace_or_ops_file.build_params_hash(*args, **kwargs)
  @namespace_or_ops_file.invoke(@runtime_env, params_hash)
end

#_resolve_method_and_invoke(name, *args, **kwargs) ⇒ Object



171
172
173
174
175
176
177
178
179
180
# File 'lib/opswalrus/invocation.rb', line 171

def _resolve_method_and_invoke(name, *args, **kwargs)
  if _bang_method?(name)      # foo! is an attempt to invoke the module's default entrypoint
    method_name = _non_bang_method(name)
    @namespace_or_ops_file = @namespace_or_ops_file.resolve_symbol(method_name)
    _invoke_if_namespace_has_ops_file_of_same_name(*args, **kwargs)
  else
    @namespace_or_ops_file = @namespace_or_ops_file.resolve_symbol(name)
    _invoke(*args, **kwargs)
  end
end