Class: Marta::Dialogs::MethodSpeaker

Inherits:
Object
  • Object
show all
Includes:
ElementInformation, Injector, Lightning, PageArithmetic, PublicMethods, SimpleElementFinder, XPath
Defined in:
lib/marta/dialogs.rb

Overview

Note:

It is believed that no user will use it

Dialog operator class

Instance Method Summary collapse

Methods included from PublicMethods

#default_method_missing, #engine, #method_edit, #method_missing, #open_page

Constructor Details

#initialize(method_name, requestor) ⇒ MethodSpeaker



29
30
31
32
33
34
35
36
37
38
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Marta::PublicMethods

Instance Method Details

#answer_to_hash(answer) ⇒ Object

Creating new fashioned hash out of data



101
102
103
104
105
106
107
# 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) ⇒ Object

Standart question



41
42
43
# 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_confirmationObject

Asking: “Are you sure?”



138
139
140
# File 'lib/marta/dialogs.rb', line 138

def ask_confirmation
  ask 'element-confirm', @title, @mass.length.to_s
end

#ask_for_elementsObject

Asking: “What are you looking for?”



95
96
97
98
# 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_xpathObject

Asking: “Provide your xpath”



143
144
145
# File 'lib/marta/dialogs.rb', line 143

def ask_xpath
  ask 'custom-xpath', @title
end

#attrs_exists?Boolean

Was something stated by user?



46
47
48
49
50
51
52
# File 'lib/marta/dialogs.rb', line 46

def attrs_exists?
  if !@attrs.nil?
    @attrs != Hash.new
  else
    false
  end
end

#attrs_plus_resultObject

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.



84
85
86
87
88
89
90
91
92
# 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

#dialogObject

Main method. All the dialog logic is here



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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?Boolean

Is dialog finished?

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



152
153
154
155
156
157
158
# File 'lib/marta/dialogs.rb', line 152

def finished?
  if @result == '1' or @result == '4'
    true
  else
    false
  end
end

#get_elements_by_attrsObject

Finding out what was selected



126
127
128
129
130
131
132
133
134
135
# 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_mergeObject

Creating data to save when it is a basically defined element



110
111
112
113
114
115
# 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_hashObject

Forming of an empty hash for storing element info



174
175
176
177
178
179
# 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_mergeObject

Creating data to save when user suggests a custom xpath



118
119
120
121
122
123
# 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_wayObject

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



161
162
163
164
165
166
167
168
169
170
171
# 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