automation_object 0.0.4 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/automation_object.rb +14 -72
- data/lib/automation_object/blue_print.rb +37 -100
- data/lib/automation_object/blue_print/composite/automatic_onload_modal.rb +28 -0
- data/lib/automation_object/blue_print/composite/base.rb +18 -0
- data/lib/automation_object/blue_print/composite/custom_method.rb +23 -0
- data/lib/automation_object/blue_print/composite/element.rb +20 -0
- data/lib/automation_object/blue_print/composite/element_array.rb +21 -0
- data/lib/automation_object/blue_print/composite/element_hash.rb +26 -0
- data/lib/automation_object/blue_print/composite/helpers/element_helper.rb +33 -0
- data/lib/automation_object/blue_print/composite/helpers/multiple_elements_helper.rb +18 -0
- data/lib/automation_object/blue_print/composite/hook.rb +31 -0
- data/lib/automation_object/blue_print/composite/hook_action.rb +84 -0
- data/lib/automation_object/blue_print/composite/hook_element_requirements.rb +39 -0
- data/lib/automation_object/blue_print/composite/modal.rb +56 -0
- data/lib/automation_object/blue_print/composite/screen.rb +61 -0
- data/lib/automation_object/blue_print/composite/top.rb +51 -0
- data/lib/automation_object/blue_print/composite/view.rb +48 -0
- data/lib/automation_object/blue_print/hash_adapter.rb +17 -0
- data/lib/automation_object/blue_print/hash_adapter/automatic_onload_modal.rb +35 -0
- data/lib/automation_object/blue_print/hash_adapter/composite.rb +93 -0
- data/lib/automation_object/blue_print/hash_adapter/custom_method.rb +30 -0
- data/lib/automation_object/blue_print/hash_adapter/element.rb +34 -0
- data/lib/automation_object/blue_print/hash_adapter/element_array.rb +36 -0
- data/lib/automation_object/blue_print/hash_adapter/element_hash.rb +43 -0
- data/lib/automation_object/blue_print/hash_adapter/helpers/element_helper.rb +59 -0
- data/lib/automation_object/blue_print/hash_adapter/helpers/multiple_elements_helper.rb +35 -0
- data/lib/automation_object/blue_print/hash_adapter/helpers/validation_error.rb +26 -0
- data/lib/automation_object/blue_print/hash_adapter/helpers/validation_helper.rb +99 -0
- data/lib/automation_object/blue_print/hash_adapter/helpers/validators/validate.rb +23 -0
- data/lib/automation_object/blue_print/hash_adapter/helpers/validators/validate_allowed_keys.rb +31 -0
- data/lib/automation_object/blue_print/hash_adapter/helpers/validators/validate_element_presence_of.rb +55 -0
- data/lib/automation_object/blue_print/hash_adapter/helpers/validators/validate_instance_of.rb +39 -0
- data/lib/automation_object/blue_print/hash_adapter/helpers/validators/validate_modal_presence_of.rb +54 -0
- data/lib/automation_object/blue_print/hash_adapter/helpers/validators/validate_presence_of.rb +36 -0
- data/lib/automation_object/blue_print/hash_adapter/helpers/validators/validate_screen_presence_of.rb +55 -0
- data/lib/automation_object/blue_print/hash_adapter/helpers/validators/validate_view_presence_of.rb +52 -0
- data/lib/automation_object/blue_print/hash_adapter/hook.rb +35 -0
- data/lib/automation_object/blue_print/hash_adapter/hook_action.rb +134 -0
- data/lib/automation_object/blue_print/hash_adapter/hook_element_requirements.rb +64 -0
- data/lib/automation_object/blue_print/hash_adapter/modal.rb +52 -0
- data/lib/automation_object/blue_print/hash_adapter/screen.rb +91 -0
- data/lib/automation_object/blue_print/hash_adapter/top.rb +63 -0
- data/lib/automation_object/blue_print/hash_adapter/view.rb +29 -0
- data/lib/automation_object/blue_print/helpers/file_helper.rb +38 -0
- data/lib/automation_object/blue_print/yaml_adapter.rb +50 -0
- data/lib/automation_object/driver.rb +44 -0
- data/lib/automation_object/driver/appium_adapter/driver.rb +150 -0
- data/lib/automation_object/driver/appium_adapter/element.rb +118 -0
- data/lib/automation_object/driver/base.rb +11 -0
- data/lib/automation_object/driver/driver.rb +69 -245
- data/lib/automation_object/driver/element.rb +51 -210
- data/lib/automation_object/driver/helpers/selenium_driver_helper.rb +41 -0
- data/lib/automation_object/driver/helpers/selenium_element_helper.rb +138 -0
- data/lib/automation_object/driver/nokogiri_adapter/backup/driver.rb +83 -0
- data/lib/automation_object/driver/nokogiri_adapter/backup/element.rb +66 -0
- data/lib/automation_object/driver/nokogiri_adapter/backup/form.rb +29 -0
- data/lib/automation_object/driver/nokogiri_adapter/backup/helpers/driver_element_helper.rb +52 -0
- data/lib/automation_object/driver/nokogiri_adapter/backup/session.rb +115 -0
- data/lib/automation_object/driver/nokogiri_adapter/driver.rb +104 -0
- data/lib/automation_object/driver/nokogiri_adapter/element.rb +85 -0
- data/lib/automation_object/driver/nokogiri_adapter/error.rb +17 -0
- data/lib/automation_object/driver/selenium_adapter/driver.rb +143 -0
- data/lib/automation_object/driver/selenium_adapter/element.rb +116 -0
- data/lib/automation_object/dsl.rb +33 -0
- data/lib/automation_object/dsl/blue_print_adapter.rb +16 -0
- data/lib/automation_object/dsl/blue_print_adapter/composite.rb +54 -0
- data/lib/automation_object/dsl/blue_print_adapter/element.rb +14 -0
- data/lib/automation_object/dsl/blue_print_adapter/element_array.rb +14 -0
- data/lib/automation_object/dsl/blue_print_adapter/element_hash.rb +14 -0
- data/lib/automation_object/dsl/blue_print_adapter/modal.rb +23 -0
- data/lib/automation_object/dsl/blue_print_adapter/screen.rb +26 -0
- data/lib/automation_object/dsl/blue_print_adapter/top.rb +19 -0
- data/lib/automation_object/dsl/models.rb +18 -0
- data/lib/automation_object/dsl/proxies/element.rb +16 -0
- data/lib/automation_object/dsl/proxies/element_array.rb +16 -0
- data/lib/automation_object/dsl/proxies/element_hash.rb +16 -0
- data/lib/automation_object/dsl/proxies/helpers/composite_helper.rb +74 -0
- data/lib/automation_object/dsl/proxies/modal.rb +17 -0
- data/lib/automation_object/dsl/proxies/screen.rb +17 -0
- data/lib/automation_object/dsl/proxies/top.rb +17 -0
- data/lib/automation_object/framework.rb +79 -386
- data/lib/automation_object/helpers/composite.rb +95 -0
- data/lib/automation_object/helpers/composite_hook.rb +46 -0
- data/lib/automation_object/helpers/hash.rb +15 -0
- data/lib/automation_object/helpers/reflection_helper.rb +28 -0
- data/lib/automation_object/helpers/string.rb +27 -0
- data/lib/automation_object/proxies/mutex_proxy.rb +63 -0
- data/lib/automation_object/proxies/proxy.rb +20 -0
- data/lib/automation_object/proxies/throttle_proxy.rb +57 -0
- data/lib/automation_object/state.rb +37 -0
- data/lib/automation_object/state/blue_print_adapter.rb +16 -0
- data/lib/automation_object/state/blue_print_adapter/composite.rb +65 -0
- data/lib/automation_object/state/blue_print_adapter/element.rb +20 -0
- data/lib/automation_object/state/blue_print_adapter/element_array.rb +25 -0
- data/lib/automation_object/state/blue_print_adapter/element_hash.rb +31 -0
- data/lib/automation_object/state/blue_print_adapter/element_proxy.rb +28 -0
- data/lib/automation_object/state/blue_print_adapter/helpers/element_helper.rb +31 -0
- data/lib/automation_object/state/blue_print_adapter/helpers/screen_manager.rb +137 -0
- data/lib/automation_object/state/blue_print_adapter/helpers/window.rb +39 -0
- data/lib/automation_object/state/blue_print_adapter/hook.rb +56 -0
- data/lib/automation_object/state/blue_print_adapter/hook_action.rb +33 -0
- data/lib/automation_object/state/blue_print_adapter/hook_actions/action_loop.rb +35 -0
- data/lib/automation_object/state/blue_print_adapter/hook_actions/change_screen.rb +26 -0
- data/lib/automation_object/state/blue_print_adapter/hook_actions/change_to_previous_screen.rb +21 -0
- data/lib/automation_object/state/blue_print_adapter/hook_actions/close_modal.rb +31 -0
- data/lib/automation_object/state/blue_print_adapter/hook_actions/close_screen.rb +19 -0
- data/lib/automation_object/state/blue_print_adapter/hook_actions/element_requirement.rb +34 -0
- data/lib/automation_object/state/blue_print_adapter/hook_actions/new_screen.rb +37 -0
- data/lib/automation_object/state/blue_print_adapter/hook_actions/possible_screen_changes.rb +21 -0
- data/lib/automation_object/state/blue_print_adapter/hook_actions/reset_screen.rb +18 -0
- data/lib/automation_object/state/blue_print_adapter/hook_actions/show_modal.rb +26 -0
- data/lib/automation_object/state/blue_print_adapter/hook_actions/sleep.rb +18 -0
- data/lib/automation_object/state/blue_print_adapter/hook_actions/wait_for_elements.rb +20 -0
- data/lib/automation_object/state/blue_print_adapter/modal.rb +26 -0
- data/lib/automation_object/state/blue_print_adapter/screen.rb +32 -0
- data/lib/automation_object/state/blue_print_adapter/top.rb +45 -0
- data/lib/automation_object/state/error.rb +24 -0
- data/lib/automation_object/state/session.rb +38 -0
- data/lib/automation_object/step_definitions.rb +14 -0
- data/lib/automation_object/step_definitions/action.rb +21 -0
- data/lib/automation_object/step_definitions/cache.rb +23 -0
- data/lib/automation_object/step_definitions/element/actions.rb +87 -0
- data/lib/automation_object/step_definitions/element/regex.rb +24 -0
- data/lib/automation_object/step_definitions/element/steps.rb +67 -0
- data/lib/automation_object/step_definitions/parse.rb +39 -0
- data/lib/automation_object/version.rb +1 -1
- metadata +194 -110
- data/lib/automation_object/blue_print_validation/base_validation.rb +0 -44
- data/lib/automation_object/blue_print_validation/common_methods.rb +0 -106
- data/lib/automation_object/blue_print_validation/element_validation.rb +0 -198
- data/lib/automation_object/blue_print_validation/formatted_errors.rb +0 -41
- data/lib/automation_object/blue_print_validation/hook_validation.rb +0 -393
- data/lib/automation_object/blue_print_validation/key_value_constants.rb +0 -75
- data/lib/automation_object/blue_print_validation/modal_validation.rb +0 -37
- data/lib/automation_object/blue_print_validation/screen_modal_common_methods.rb +0 -119
- data/lib/automation_object/blue_print_validation/screen_validation.rb +0 -21
- data/lib/automation_object/blue_print_validation/validation_object.rb +0 -32
- data/lib/automation_object/driver/anonymous.rb +0 -27
- data/lib/automation_object/element/element.rb +0 -145
- data/lib/automation_object/element/element_array.rb +0 -12
- data/lib/automation_object/element/element_cell.rb +0 -126
- data/lib/automation_object/element/element_group.rb +0 -33
- data/lib/automation_object/element/element_hash.rb +0 -25
- data/lib/automation_object/element/element_helpers.rb +0 -29
- data/lib/automation_object/element/element_methods.rb +0 -134
- data/lib/automation_object/element/elements_helpers.rb +0 -204
- data/lib/automation_object/framework/events.rb +0 -8
- data/lib/automation_object/framework/helpers.rb +0 -101
- data/lib/automation_object/framework/print_objects.rb +0 -67
- data/lib/automation_object/framework/screen_monitor.rb +0 -55
- data/lib/automation_object/framework/screen_routing.rb +0 -310
- data/lib/automation_object/framework/window_helpers.rb +0 -181
- data/lib/automation_object/hash.rb +0 -6
- data/lib/automation_object/hook_helpers.rb +0 -27
- data/lib/automation_object/logger.rb +0 -179
- data/lib/automation_object/object.rb +0 -22
- data/lib/automation_object/screen/modal.rb +0 -8
- data/lib/automation_object/screen/screen.rb +0 -209
- data/lib/automation_object/screen/screen_hook_helpers.rb +0 -319
- data/lib/automation_object/screen/screen_modal_helpers.rb +0 -101
- data/lib/automation_object/screen/screen_prompt_helpers.rb +0 -21
- data/lib/automation_object/screen/screen_window_helpers.rb +0 -149
@@ -0,0 +1,30 @@
|
|
1
|
+
#Require parent class
|
2
|
+
require_relative 'composite'
|
3
|
+
|
4
|
+
module AutomationObject
|
5
|
+
module BluePrint
|
6
|
+
module HashAdapter
|
7
|
+
#Custom method composite
|
8
|
+
class CustomMethod < Composite
|
9
|
+
#Validations
|
10
|
+
validates :element_method, instance_of: [String, Symbol], presence_of: true
|
11
|
+
validates :evaluate, instance_of: [String], presence_of: true
|
12
|
+
|
13
|
+
def element_method
|
14
|
+
element_method = self.hash[:element_method]
|
15
|
+
|
16
|
+
case element_method
|
17
|
+
when Symbol, String
|
18
|
+
return element_method.to_sym
|
19
|
+
else
|
20
|
+
return nil
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def evaluate
|
25
|
+
return self.hash[:evaluate] ||= nil
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
#Require parent class
|
2
|
+
require_relative 'composite'
|
3
|
+
|
4
|
+
#Require child classes
|
5
|
+
require_relative 'hook'
|
6
|
+
require_relative 'custom_method'
|
7
|
+
|
8
|
+
#Require helpers
|
9
|
+
require_relative 'helpers/element_helper'
|
10
|
+
|
11
|
+
module AutomationObject
|
12
|
+
module BluePrint
|
13
|
+
module HashAdapter
|
14
|
+
#Element composite
|
15
|
+
class Element < Composite
|
16
|
+
include ElementHelper
|
17
|
+
|
18
|
+
before_create :method_hooks
|
19
|
+
|
20
|
+
#Relationships
|
21
|
+
has_one :load, interface: Hook
|
22
|
+
has_many :custom_methods, interface: CustomMethod
|
23
|
+
|
24
|
+
#Validations
|
25
|
+
validates :load, instance_of: Hash
|
26
|
+
validates :custom_methods, instance_of: Hash
|
27
|
+
|
28
|
+
validates :in_iframe, instance_of: String, element_presence_of: true
|
29
|
+
validates :css, instance_of: String, presence_of: {:unless_presence_of => :xpath}
|
30
|
+
validates :xpath, instance_of: String, presence_of: {:unless_presence_of => :css}
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
#Require parent class
|
2
|
+
require_relative 'composite'
|
3
|
+
|
4
|
+
#Require child classes
|
5
|
+
require_relative 'hook'
|
6
|
+
require_relative 'custom_method'
|
7
|
+
|
8
|
+
#Require helpers
|
9
|
+
require_relative 'helpers/element_helper'
|
10
|
+
require_relative 'helpers/multiple_elements_helper'
|
11
|
+
|
12
|
+
module AutomationObject
|
13
|
+
module BluePrint
|
14
|
+
module HashAdapter
|
15
|
+
#Element array composite
|
16
|
+
class ElementArray < Composite
|
17
|
+
include ElementHelper
|
18
|
+
include MultipleElementsHelper
|
19
|
+
|
20
|
+
before_create :method_hooks
|
21
|
+
|
22
|
+
#Relationships
|
23
|
+
has_one :load, interface: Hook
|
24
|
+
has_many :custom_methods, interface: CustomMethod
|
25
|
+
|
26
|
+
#Validations
|
27
|
+
validates :load, instance_of: Hash
|
28
|
+
validates :custom_range, instance_of: [Hash, String]
|
29
|
+
validates :in_iframe, instance_of: String, element_presence_of: true
|
30
|
+
validates :custom_methods, instance_of: Hash
|
31
|
+
validates :css, instance_of: String, presence_of: {:unless_presence_of => :xpath}
|
32
|
+
validates :xpath, instance_of: String, presence_of: {:unless_presence_of => :css}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
#Require parent class
|
2
|
+
require_relative 'composite'
|
3
|
+
|
4
|
+
#Require child classes
|
5
|
+
require_relative 'hook'
|
6
|
+
require_relative 'custom_method'
|
7
|
+
|
8
|
+
#Require helpers
|
9
|
+
require_relative 'helpers/element_helper'
|
10
|
+
require_relative 'helpers/multiple_elements_helper'
|
11
|
+
|
12
|
+
module AutomationObject
|
13
|
+
module BluePrint
|
14
|
+
module HashAdapter
|
15
|
+
#Element hash composite
|
16
|
+
class ElementHash < Composite
|
17
|
+
include ElementHelper
|
18
|
+
include MultipleElementsHelper
|
19
|
+
|
20
|
+
before_create :method_hooks
|
21
|
+
|
22
|
+
#Relationships
|
23
|
+
has_one :load, interface: Hook
|
24
|
+
has_many :custom_methods, interface: CustomMethod
|
25
|
+
|
26
|
+
#Validations
|
27
|
+
validates :load, instance_of: Hash
|
28
|
+
|
29
|
+
validates :define_elements_by, instance_of: [String, Symbol], presence_of: true
|
30
|
+
validates :custom_range, instance_of: [Hash, String]
|
31
|
+
validates :in_iframe, instance_of: String, element_presence_of: true
|
32
|
+
validates :custom_methods, instance_of: Hash
|
33
|
+
validates :css, instance_of: String, presence_of: {:unless_presence_of => :xpath}
|
34
|
+
validates :xpath, instance_of: String, presence_of: {:unless_presence_of => :css}
|
35
|
+
|
36
|
+
# @return [Symbol] element method to define element keys by
|
37
|
+
def define_elements_by
|
38
|
+
return self.hash[:define_elements_by].to_sym
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module AutomationObject
|
2
|
+
module BluePrint
|
3
|
+
module HashAdapter
|
4
|
+
#Helper module for Element composite classes
|
5
|
+
module ElementHelper
|
6
|
+
# @return [Array<Symbol, String>, nil] params as an array for driver find_element args
|
7
|
+
def selector_params
|
8
|
+
case
|
9
|
+
when self.hash[:xpath].is_a?(String)
|
10
|
+
[:xpath, self.hash[:xpath]]
|
11
|
+
when self.hash[:css].is_a?(String)
|
12
|
+
[:css, self.hash[:css]]
|
13
|
+
else
|
14
|
+
nil
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# @return [Symbol, nil] element name of iframe element is in or nil if not
|
19
|
+
def in_iframe
|
20
|
+
case self.hash[:in_iframe]
|
21
|
+
when Symbol, String
|
22
|
+
self.hash[:in_iframe].to_sym
|
23
|
+
else
|
24
|
+
nil
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# @return [Boolean] whether or not element is in iframe
|
29
|
+
def in_iframe?
|
30
|
+
(self.in_iframe) ? true : false
|
31
|
+
end
|
32
|
+
|
33
|
+
def method_hook?(name)
|
34
|
+
return self.method_hooks.has_key?(name)
|
35
|
+
end
|
36
|
+
|
37
|
+
# @return [Hash<Hook>] array of Hook that are defined under the element
|
38
|
+
def method_hooks
|
39
|
+
return @method_hooks if @method_hooks
|
40
|
+
|
41
|
+
children = {}
|
42
|
+
self.hash.each { |key, value|
|
43
|
+
#Skip possible keys that elements can have
|
44
|
+
#Otherwise should be a method hook
|
45
|
+
next if [:load, :custom_methods, :in_iframe, :css, :xpath, :define_elements_by, :custom_range].include?(key)
|
46
|
+
children[key] = value
|
47
|
+
}
|
48
|
+
|
49
|
+
@method_hooks = self.create_hash_children(children,
|
50
|
+
{interface: Hook,
|
51
|
+
location: self.location + '[hook]'})
|
52
|
+
|
53
|
+
return @method_hooks
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module AutomationObject
|
2
|
+
module BluePrint
|
3
|
+
module HashAdapter
|
4
|
+
#Helper module for multiple element composite classes
|
5
|
+
module MultipleElementsHelper
|
6
|
+
# @return [Range, nil] gives range for limiting multiple elements or nil
|
7
|
+
def custom_range
|
8
|
+
custom_range = self.hash[:custom_range]
|
9
|
+
|
10
|
+
case custom_range
|
11
|
+
when String
|
12
|
+
integer_array = custom_range.split('..').map { |number| Integer(number) }
|
13
|
+
return integer_array[0]..integer_array[1]
|
14
|
+
when Hash
|
15
|
+
return custom_range[:start].to_i..custom_range[:end].to_i
|
16
|
+
end
|
17
|
+
|
18
|
+
return nil
|
19
|
+
end
|
20
|
+
|
21
|
+
# @return [Symbol, nil] gives element method to remove duplicates on or nil
|
22
|
+
def remove_duplicates
|
23
|
+
remove_duplicates = self.hash[:remove_duplicates]
|
24
|
+
|
25
|
+
case remove_duplicates
|
26
|
+
when Symbol, String
|
27
|
+
return remove_duplicates.to_sym
|
28
|
+
else
|
29
|
+
return nil
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module AutomationObject
|
2
|
+
module BluePrint
|
3
|
+
module HashAdapter
|
4
|
+
#ValidatorError exception class
|
5
|
+
#Allows for multiple errors at one time to allow for better debugging
|
6
|
+
class ValidationError < Exception
|
7
|
+
attr_accessor :errors
|
8
|
+
|
9
|
+
# @param errors [Array, String] pass validation error(s) in, class can handle singular or multiple
|
10
|
+
def initialize(errors)
|
11
|
+
@errors = (errors.is_a?(Array)) ? errors : [errors]
|
12
|
+
end
|
13
|
+
|
14
|
+
# @return [String] returns full error message
|
15
|
+
def message
|
16
|
+
message = "\nBluePrint::HashAdapter composite validation failed for the following errors:\n"
|
17
|
+
@errors.each { |error|
|
18
|
+
message << " - #{error}\n"
|
19
|
+
}
|
20
|
+
|
21
|
+
return message + "\n"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
#Require all the validators
|
2
|
+
require_relative 'validators/validate_instance_of'
|
3
|
+
require_relative 'validators/validate_allowed_keys'
|
4
|
+
require_relative 'validators/validate_screen_presence_of'
|
5
|
+
require_relative 'validators/validate_modal_presence_of'
|
6
|
+
require_relative 'validators/validate_view_presence_of'
|
7
|
+
require_relative 'validators/validate_element_presence_of'
|
8
|
+
require_relative 'validators/validate_presence_of'
|
9
|
+
|
10
|
+
require_relative '../../../helpers/string'
|
11
|
+
|
12
|
+
module AutomationObject
|
13
|
+
module BluePrint
|
14
|
+
module HashAdapter
|
15
|
+
#Validation helper for the composite structure, will allow Rails style validations on the composite
|
16
|
+
module ValidationHelper
|
17
|
+
attr_accessor :errors
|
18
|
+
|
19
|
+
# Give errors a default empty Array
|
20
|
+
# @return [Array<String>] errors messages
|
21
|
+
def errors
|
22
|
+
@errors ||= Array.new
|
23
|
+
end
|
24
|
+
|
25
|
+
# Give errors a default empty Array
|
26
|
+
# @params [Array<String>] error messages
|
27
|
+
# @return [Array<String>] error messages
|
28
|
+
def add_errors(errors)
|
29
|
+
self.errors += errors
|
30
|
+
end
|
31
|
+
|
32
|
+
# @return [Boolean] composite valid?
|
33
|
+
def valid?
|
34
|
+
return true if self.class.skip_validations
|
35
|
+
|
36
|
+
# ap self.class
|
37
|
+
#ap self.class.name
|
38
|
+
self.class.validations.collect do |validation|
|
39
|
+
# ap validation.class.name
|
40
|
+
end
|
41
|
+
|
42
|
+
self.class.validations.collect do |validation|
|
43
|
+
validation.validate(self)
|
44
|
+
unless validation.valid?
|
45
|
+
self.add_errors(validation.error_messages)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
return self.errors.empty?
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.included(base)
|
53
|
+
base.extend(ClassMethods)
|
54
|
+
end
|
55
|
+
|
56
|
+
#Class level methods
|
57
|
+
module ClassMethods
|
58
|
+
def skip_validations=(boolean)
|
59
|
+
@skip_validations = boolean
|
60
|
+
end
|
61
|
+
|
62
|
+
def skip_validations
|
63
|
+
@skip_validations ||= false
|
64
|
+
end
|
65
|
+
|
66
|
+
# @param key [Symbol] hash key to test
|
67
|
+
# @param args [Hash] arguments for validation
|
68
|
+
# @return [nil]
|
69
|
+
def validates(key, args = {})
|
70
|
+
# ap self
|
71
|
+
# ap self.class.name
|
72
|
+
# ap key
|
73
|
+
# ap args
|
74
|
+
|
75
|
+
|
76
|
+
args.each { |validator_name, validation_args|
|
77
|
+
validator_name = 'Validate' + validator_name.to_s.pascalize
|
78
|
+
validator = Validators.const_get(validator_name)
|
79
|
+
|
80
|
+
self.validations.push(validator.new({:key => key, :args => validation_args}))
|
81
|
+
}
|
82
|
+
end
|
83
|
+
|
84
|
+
# @param args [Hash] arguments for validation
|
85
|
+
# @return [nil]
|
86
|
+
def validates_keys(args = {})
|
87
|
+
self.validations.push(Validators::ValidateAllowedKeys.new(args))
|
88
|
+
end
|
89
|
+
|
90
|
+
# @return [Array] list of Validators, default to empty list
|
91
|
+
def validations
|
92
|
+
@validations = [] unless @validations
|
93
|
+
return @validations
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module AutomationObject
|
2
|
+
module BluePrint
|
3
|
+
module HashAdapter
|
4
|
+
module Validators
|
5
|
+
#Super-class for validators, contains base functionality
|
6
|
+
#Sub-classes must implement validate for the composite
|
7
|
+
class Validate
|
8
|
+
attr_accessor :error_messages
|
9
|
+
|
10
|
+
# @return [Array<String>] array of error messages, default empty array
|
11
|
+
def error_messages
|
12
|
+
@error_messages ||= Array.new
|
13
|
+
end
|
14
|
+
|
15
|
+
# @return [Boolean] whether or not validation passed or failed
|
16
|
+
def valid?
|
17
|
+
return (self.error_messages.length == 0) ? true : false
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/automation_object/blue_print/hash_adapter/helpers/validators/validate_allowed_keys.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require_relative 'validate'
|
2
|
+
|
3
|
+
module AutomationObject
|
4
|
+
module BluePrint
|
5
|
+
module HashAdapter
|
6
|
+
module Validators
|
7
|
+
#Validator that tests the composite hash for bad keys
|
8
|
+
class ValidateAllowedKeys < Validate
|
9
|
+
# @param args [Hash] arguments for the validation class
|
10
|
+
def initialize(args)
|
11
|
+
@allowed_keys = args.fetch :allowed_keys
|
12
|
+
end
|
13
|
+
|
14
|
+
# Validates the composite object and throws errors on failure
|
15
|
+
# @param composite_object [Object] Composite object to be tested.
|
16
|
+
# @return [nil] no return on exceptions on failure
|
17
|
+
def validate(composite_object)
|
18
|
+
#Get the hash from the composite object
|
19
|
+
target_hash = composite_object.hash
|
20
|
+
|
21
|
+
target_hash.each_key { |key|
|
22
|
+
unless @allowed_keys.include?(key)
|
23
|
+
self.error_messages.push("Invalid Key: #{key}, at: #{composite_object.location}. Allowed keys: #{@allowed_keys}.")
|
24
|
+
end
|
25
|
+
}
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require_relative 'validate'
|
2
|
+
|
3
|
+
module AutomationObject
|
4
|
+
module BluePrint
|
5
|
+
module HashAdapter
|
6
|
+
module Validators
|
7
|
+
#Validator that tests that a element is defined when it is called elsewhere through a hook
|
8
|
+
class ValidateElementPresenceOf < Validate
|
9
|
+
# @param args [Hash] arguments for the validation class
|
10
|
+
def initialize(args)
|
11
|
+
@key = args.fetch :key
|
12
|
+
end
|
13
|
+
|
14
|
+
# Validates the composite object and throws errors on failure
|
15
|
+
# @param composite_object [Object] Composite object to be tested.
|
16
|
+
# @return [nil] no return on exceptions on failure
|
17
|
+
def validate(composite_object)
|
18
|
+
#Get the hash value from the composite object
|
19
|
+
target_value = composite_object.hash[@key]
|
20
|
+
|
21
|
+
return unless target_value
|
22
|
+
|
23
|
+
target_values = (target_value.is_a?(Array)) ? target_value : [target_value]
|
24
|
+
|
25
|
+
valid_elements = self.find_elements(composite_object)
|
26
|
+
|
27
|
+
target_values.each { |element|
|
28
|
+
element = element.to_sym
|
29
|
+
next if valid_elements.include?(element)
|
30
|
+
|
31
|
+
error_message = "Invalid Element: #{element}, at: #{composite_object.location}[#{@key}]."
|
32
|
+
error_message << " Valid Elements(s): #{valid_elements}"
|
33
|
+
self.error_messages.push(error_message)
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
#Traverses up a composite tree to find :elements in a hash
|
38
|
+
# @param composite_object [Object] composite object to traverse up
|
39
|
+
# @return [Array<Symbol>] array of element names
|
40
|
+
def find_elements(composite_object)
|
41
|
+
#Using the hash instead of the method because lower nodes will get validated
|
42
|
+
#before composite is finished building
|
43
|
+
if composite_object.hash[:elements].is_a?(Hash)
|
44
|
+
return composite_object.hash[:elements].keys #Should be Hash with element names as the keys
|
45
|
+
elsif composite_object.parent
|
46
|
+
return self.find_elements(composite_object.parent)
|
47
|
+
end
|
48
|
+
|
49
|
+
return []
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|