Class: DTK::Shell::ContextParams

Inherits:
Object
  • Object
show all
Defined in:
lib/shell/domain/context_params.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(override_method_arguments = []) ⇒ ContextParams

Returns a new instance of ContextParams.



25
26
27
28
29
30
31
32
# 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

Instance Attribute Details

#current_contextObject

Returns the value of attribute current_context.



21
22
23
# File 'lib/shell/domain/context_params.rb', line 21

def current_context
  @current_context
end

#method_argumentsObject

Returns the value of attribute method_arguments.



22
23
24
# File 'lib/shell/domain/context_params.rb', line 22

def method_arguments
  @method_arguments
end

#pure_cli_modeObject

Returns the value of attribute pure_cli_mode.



23
24
25
# File 'lib/shell/domain/context_params.rb', line 23

def pure_cli_mode
  @pure_cli_mode
end

Instance Method Details

#add_context_name_to_params(context_name, entity_name, context_value = nil) ⇒ Object



38
39
40
# 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) ⇒ Object



34
35
36
# 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?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/shell/domain/context_params.rb', line 167

def current_command?
  return @current_context.current_command?
end

#forward_options(options) ⇒ Object



42
43
44
# File 'lib/shell/domain/context_params.rb', line 42

def forward_options(options)
  @thor_options = options
end

#get_forwarded_optionsObject



46
47
48
# File 'lib/shell/domain/context_params.rb', line 46

def get_forwarded_options()
  @thor_options
end

#get_forwarded_thor_option(option_key) ⇒ Object



50
51
52
# 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) ⇒ Boolean

Returns:

  • (Boolean)


156
157
158
# 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) ⇒ Boolean

Returns:

  • (Boolean)


164
165
166
# 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) ⇒ Boolean

Returns:

  • (Boolean)


160
161
162
# 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_nameObject



173
174
175
# 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) ⇒ Object



54
55
56
57
58
# 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 = []) ⇒ Object



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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# 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) ⇒ Object

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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_nameObject



170
171
172
# File 'lib/shell/domain/context_params.rb', line 170

def root_command_name
  @current_context.first_command_name
end

#shadow_entity_nameObject



176
177
178
# File 'lib/shell/domain/context_params.rb', line 176

def shadow_entity_name()
  @current_context.shadow_entity()
end