class DTK::Shell::ContextParams

Attributes

current_context[RW]
method_arguments[RW]
pure_cli_mode[RW]

Public Class Methods

new(override_method_arguments = []) click to toggle source
# File lib/shell/domain/context_params.rb, line 25
def initialize(override_method_arguments = [])
  @current_context  = ActiveContext.new
  @method_arguments = override_method_arguments
  @thor_options     = Hash.new
  @pure_cli_mode    = false

  @method_arguments
end

Public Instance Methods

add_context_name_to_params(context_name, entity_name, context_value = nil) click to toggle source
# File lib/shell/domain/context_params.rb, line 38
def add_context_name_to_params(context_name, entity_name, context_value = nil)
  @current_context.push_new_name_context(context_name, stand_name(entity_name), context_value)
end
add_context_to_params(context_name, entity_name, context_value = nil) click to toggle source
# File lib/shell/domain/context_params.rb, line 34
def add_context_to_params(context_name, entity_name, context_value = nil)
  @current_context.push_new_context(context_name, stand_name(entity_name), context_value)
end
current_command?() click to toggle source
# File lib/shell/domain/context_params.rb, line 167
def current_command?
  return @current_context.current_command?
end
forward_options(options) click to toggle source
# File lib/shell/domain/context_params.rb, line 42
def forward_options(options)
  @thor_options = options
end
get_forwarded_options() click to toggle source
# File lib/shell/domain/context_params.rb, line 46
def get_forwarded_options()
  @thor_options
end
get_forwarded_thor_option(option_key) click to toggle source
# File lib/shell/domain/context_params.rb, line 50
def get_forwarded_thor_option(option_key)
  return @thor_options ? @thor_options[option_key] : nil
end
is_last_command_eql_to?(command_name) click to toggle source
# File lib/shell/domain/context_params.rb, line 156
def is_last_command_eql_to?(command_name)
  return @current_context.last_command_name() == command_name.to_s
end
is_there_command?(entity_name) click to toggle source
# File lib/shell/domain/context_params.rb, line 164
def is_there_command?(entity_name)
  return @current_context.find_command(entity_name) != nil
end
is_there_identifier?(entity_name) click to toggle source
# File lib/shell/domain/context_params.rb, line 160
def is_there_identifier?(entity_name)
  return @current_context.find_identifier(entity_name) != nil
end
last_entity_name() click to toggle source
# File lib/shell/domain/context_params.rb, line 173
def last_entity_name
  @current_context.last_context_entity_name
end
override_method_argument!(key, value) click to toggle source
# File lib/shell/domain/context_params.rb, line 54
def override_method_argument!(key, value)
  id = match_argument_id(key)
  raise DTK::Client::DtkImplementationError, "Wrong identifier used '#{key}', ID not matched!" unless id
  @method_arguments[id] = value
end
retrieve_arguments(mapping, method_info = []) click to toggle source
# File lib/shell/domain/context_params.rb, line 83
def retrieve_arguments(mapping, method_info = [])
  results = []
  errors  = []

  # using context_name when have array as key_mapping [:assembly_id, :workspace_id]
  # to determine which context is used
  context_name = method_info.first.split('-').first unless method_info.empty?

  mapping.each do |key_mapping|

    is_array = key_mapping.is_a?(Array)

    selected_key = is_array ? key_mapping.first : key_mapping

    required = selected_key.to_s.match(/.+!$/)

    element = nil
    matched = selected_key.to_s.match(/option_([0-9]+)/)
    if matched
      id = matched[1].to_i - 1
      element = @method_arguments[id].dup if @method_arguments[id]

      # used if last parameter has more than one word
      # e.g. set-attribute attr_name "some value" (thor separates 'some value' as two parameters but we need it as one)
      if(mapping.last.to_s.eql?(key_mapping.to_s))
        new_id = id+1
        while @method_arguments[new_id] do
          element << " #{@method_arguments[new_id]}"
          new_id += 1;
        end
      end

      unless method_info.empty?
        unless element
          errors << method_info[id] if required
        end
      end

    else
      # More complex split regex for extracting entitiy name from mapping due to complex context names
      # i.e. assembly-template will have assembly_template_id mapping
      element = check_context_for_element(selected_key)

      # if we are dealing with array we need to check rest of the keys since it is OR
      # approach if first element not found take second
      if element.nil? && is_array
        key_mapping[1..-1].each do |alternative_key|
          element = check_context_for_element(alternative_key)
          break if element
          if context_name
            if alternative_key.to_s.include?(context_name.downcase)
              required = alternative_key.to_s.match(/.+!$/)
              selected_key = alternative_key
            end
          end
        end
      end

      unless element
        errors << "#{entity_name(selected_key).upcase} ID/NAME" if required
      end
    end

    results << element
  end

  unless errors.empty?
    raise DTK::Client::DtkValidationError.new("Missing required argument#{errors.size > 1 ? 's' : ''}: #{errors.join(', ')}", true)
  end

  return ((results.size == 1) ? results.first : results)
end
retrieve_thor_options(mapping, options) click to toggle source

can be class methods but no need, since we have this instance available in each method

# File lib/shell/domain/context_params.rb, line 61
def retrieve_thor_options(mapping, options)
  results = []
  errors  = []

  mapping.each do |key|
    required = key.to_s.match(/.+!$/)
    thor_key = key.to_s.gsub('!','')

    results << element = options[thor_key]

    if required && element.nil?
      errors << thor_key
    end
  end

  unless errors.empty?
    raise DTK::Client::DtkValidationError.new("Missing required option#{errors.size > 1 ? 's' : ''}: #{errors.join(', ')}", true)
  end

  return ((results.size == 1) ? results.first : results)
end
root_command_name() click to toggle source
# File lib/shell/domain/context_params.rb, line 170
def root_command_name
  @current_context.first_command_name
end
shadow_entity_name() click to toggle source
# File lib/shell/domain/context_params.rb, line 176
def shadow_entity_name()
  @current_context.shadow_entity()
end

Private Instance Methods

check_context_for_element(key_mapping) click to toggle source

based on map key binding e.g. assembly_id, assembly_name we will extrace value from our ActiveContext

# File lib/shell/domain/context_params.rb, line 192
def check_context_for_element(key_mapping)
  split_info  =  split_info(key_mapping)
  entity_name =  entity_name(key_mapping,split_info)
  id_type     = split_info[1].gsub(/!/,'')   # for required elements we remove '!' required marker
  context_identifier = @current_context.find_identifier(entity_name)
  if context_identifier
    return context_identifier.get_identifier(id_type)
  else
    return nil
  end
end
entity_name(key_mapping,split_info=nil) click to toggle source
# File lib/shell/domain/context_params.rb, line 204
def entity_name(key_mapping,split_info=nil)
  split_info ||= split_info(key_mapping)
  split_info[0].gsub(/_/,'-')  # makes sure we are using entity names with '_'
end
match_argument_id(identifier) click to toggle source

matches argument id (integer) from used identifier (symbol)

Returns: Integer as ID , or nil if not found

# File lib/shell/domain/context_params.rb, line 185
def match_argument_id(identifier)
  matched = identifier.to_s.match(/option_([0-9]+)/)
  (matched ? matched[1].to_i - 1 : nil)
end
split_info(key_mapping) click to toggle source
# File lib/shell/domain/context_params.rb, line 218
def split_info(key_mapping)
  key_mapping.to_s.split(/_([a-z]+!?$)/)
end
stand_name(name) click to toggle source

Standardize context name since we are in domain treating :component_module as :'component-module' and need to be careful about these changes

# File lib/shell/domain/context_params.rb, line 214
def stand_name(name)
  name.to_s.gsub('_','-').to_sym
end