class Marta::Dialogs::MethodSpeaker

Dialog operator class

@note It is believed that no user will use it

Public Class Methods

new(method_name, requestor) click to toggle source
# File lib/marta/dialogs.rb, line 29
def initialize(method_name, requestor)
  @class_name = requestor.class_name
  @method_name = method_name
  @data = requestor.data
  @title = @class_name+  '.' + method_name.to_s
  @requestor = requestor
  @found = 0
  @attrs = @data['meths'][@method_name]
  @mass = Array.new
end

Public Instance Methods

answer_to_hash(answer) click to toggle source

Creating new fashioned hash out of data

# File lib/marta/dialogs.rb, line 101
def answer_to_hash(answer)
  result = method_structure
  result['options']['collection'] =  answer['collection']
  what = answer['exclude'] ? 'negative' : 'positive'
  result[what] = get_attributes(answer['element'])
  result
end
ask(what, title = 'Some title', data = Hash.new, vars = Array.new) click to toggle source

Standart question

# File lib/marta/dialogs.rb, line 41
def ask(what, title = 'Some title', data = Hash.new, vars = Array.new)
  inject(what, title, data, vars)
end
ask_confirmation() click to toggle source

Asking: “Are you sure?”

# File lib/marta/dialogs.rb, line 138
def ask_confirmation
  ask 'element-confirm', @title, @mass.length.to_s
end
ask_for_elements() click to toggle source

Asking: “What are you looking for?”

# File lib/marta/dialogs.rb, line 95
def ask_for_elements
  answer = ask 'element', "Found #{@found} elements for #{@title}", @attrs
  return answer.class == Hash ? answer_to_hash(answer) : answer
end
ask_xpath() click to toggle source

Asking: “Provide your xpath”

# File lib/marta/dialogs.rb, line 143
def ask_xpath
  ask 'custom-xpath', @title
end
attrs_exists?() click to toggle source

Was something stated by user?

# File lib/marta/dialogs.rb, line 46
def attrs_exists?
  if !@attrs.nil?
    @attrs != Hash.new
  else
    false
  end
end
attrs_plus_result() click to toggle source

This method is responsible for collection in two clicks feature

If we have two elements of collection this methods returns hash of element without diffs (only the same attributes). As well this method is responsible for adding excluding attributes to collection. Rare case with single element that not has some attribute is not implemented so far. All that party is for collections now.

# File lib/marta/dialogs.rb, line 84
def attrs_plus_result
  if !attrs_exists?
    @attrs = @result
  elsif !@result['options']['collection']
    @attrs = @result
  else
    @attrs = make_collection(@attrs, @result)
  end
end
dialog() click to toggle source

Main method. All the dialog logic is here

# File lib/marta/dialogs.rb, line 55
def dialog
  while !finished? do
    if attrs_exists?
      @mass = get_elements_by_attrs
      mass_highlight_turn @mass
    end
    @result = ask_for_elements
    mass_highlight_turn(@mass, false)
    if @result.class == Hash
      attrs_plus_result
    elsif @result != '1'
      xpath_way
    end
  end
  if @result == '1'
    standart_meth_merge
  else
    xpath_meth_merge
  end
end
finished?() click to toggle source

Is dialog finished?

JS returning '1' when it's done. That is not good and should be rewrited as soon as possible

# File lib/marta/dialogs.rb, line 152
def finished?
  if @result == '1' or @result == '4'
    true
  else
    false
  end
end
get_elements_by_attrs() click to toggle source

Finding out what was selected

# File lib/marta/dialogs.rb, line 126
def get_elements_by_attrs
  if @attrs['options']['xpath'].nil?
    xpath = XPathFactory.new(@attrs, @requestor).generate_xpath
  else
    xpath = @attrs['options']['xpath']
  end
  result = engine.elements(xpath: xpath)
  @found = result.length
  result
end
standart_meth_merge() click to toggle source

Creating data to save when it is a basically defined element

# File lib/marta/dialogs.rb, line 110
def standart_meth_merge
  temp = temp_hash
  temp['meths'][@method_name] = @attrs
  @data['meths'].merge!(temp['meths'])
  @data
end
temp_hash() click to toggle source

Forming of an empty hash for storing element info

# File lib/marta/dialogs.rb, line 174
def temp_hash
  temp, temp['meths'], temp['meths'][@method_name],
  temp['meths'][@method_name]['options'] = Hash.new, Hash.new, Hash.new,
  Hash.new
  temp
end
xpath_meth_merge() click to toggle source

Creating data to save when user suggests a custom xpath

# File lib/marta/dialogs.rb, line 118
def xpath_meth_merge
  temp = temp_hash
  temp['meths'][@method_name]['options'] = @attrs['options']
  @data['meths'].merge!(temp['meths'])
  @data
end
xpath_way() click to toggle source

When user selects xpath way. Marta is doing some work before finish

# File lib/marta/dialogs.rb, line 161
def xpath_way
  @result = ask_xpath
  if @result != '2'
    @attrs = Hash.new
    @attrs['options'] = @result
    @mass = get_elements_by_attrs
    mass_highlight_turn @mass
    @result = ask_confirmation
    mass_highlight_turn(@mass, false)
  end
end