sapphire 0.7.26 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/sapphire.rb +51 -40
- data/lib/sapphire/Adapters/Selenium/RubySeleniumWebDriver.rb +24 -68
- data/lib/sapphire/DSL.rb +18 -16
- data/lib/sapphire/DSL/Browser/Create.rb +10 -10
- data/lib/sapphire/DSL/Browser/ExecuteAgainstControl.rb +20 -20
- data/lib/sapphire/DSL/Browser/Fluff/By.rb +10 -10
- data/lib/sapphire/DSL/Browser/Fluff/To.rb +10 -10
- data/lib/sapphire/DSL/Browser/Fluff/With.rb +10 -10
- data/lib/sapphire/DSL/Browser/Nouns/Browser.rb +17 -17
- data/lib/sapphire/DSL/Browser/Nouns/Enter.rb +10 -10
- data/lib/sapphire/DSL/Browser/Tracker.rb +15 -15
- data/lib/sapphire/DSL/Browser/Verbs/Accept.rb +8 -8
- data/lib/sapphire/DSL/Browser/Verbs/Check.rb +10 -10
- data/lib/sapphire/DSL/Browser/Verbs/Clear.rb +8 -8
- data/lib/sapphire/DSL/Browser/Verbs/Click.rb +10 -10
- data/lib/sapphire/DSL/Browser/Verbs/Compare.rb +18 -18
- data/lib/sapphire/DSL/Browser/Verbs/Complete.rb +13 -13
- data/lib/sapphire/DSL/Browser/Verbs/Exists.rb +16 -16
- data/lib/sapphire/DSL/Browser/Verbs/Exit.rb +11 -11
- data/lib/sapphire/DSL/Browser/Verbs/MouseOver.rb +10 -10
- data/lib/sapphire/DSL/Browser/Verbs/Navigate.rb +10 -10
- data/lib/sapphire/DSL/Browser/Verbs/Press.rb +8 -8
- data/lib/sapphire/DSL/Browser/Verbs/Reload.rb +11 -11
- data/lib/sapphire/DSL/Browser/Verbs/Set.rb +10 -10
- data/lib/sapphire/DSL/Browser/Verbs/Uncheck.rb +10 -10
- data/lib/sapphire/DSL/Comparisons/CheckedComparison.rb +17 -17
- data/lib/sapphire/DSL/Comparisons/Comparison.rb +50 -50
- data/lib/sapphire/DSL/Comparisons/ContainsComparison.rb +17 -17
- data/lib/sapphire/DSL/Comparisons/DifferComparison.rb +17 -17
- data/lib/sapphire/DSL/Comparisons/EqualsComparison.rb +16 -16
- data/lib/sapphire/DSL/Comparisons/NotComparison.rb +20 -20
- data/lib/sapphire/DSL/Comparisons/SelectedComparison.rb +17 -17
- data/lib/sapphire/DSL/Comparisons/StartsWithComparison.rb +17 -17
- data/lib/sapphire/DSL/Comparisons/VisibleComparison.rb +17 -17
- data/lib/sapphire/DSL/Configuration/ConfiguredBrowser.rb +12 -12
- data/lib/sapphire/DSL/Configuration/ConfiguredUser.rb +10 -10
- data/lib/sapphire/DSL/Configuration/Use.rb +18 -18
- data/lib/sapphire/DSL/Data/Find.rb +10 -10
- data/lib/sapphire/DSL/Data/GetPageField.rb +13 -13
- data/lib/sapphire/DSL/Data/Verify.rb +10 -10
- data/lib/sapphire/DSL/Evaluations/ControlEvaluation.rb +13 -0
- data/lib/sapphire/DSL/Evaluations/Evaluation.rb +55 -45
- data/lib/sapphire/DSL/Evaluations/FieldNotDefinedEvaluation.rb +20 -20
- data/lib/sapphire/DSL/Evaluations/FieldNotFoundEvaluation.rb +22 -22
- data/lib/sapphire/DSL/Evaluators/Contain.rb +10 -10
- data/lib/sapphire/DSL/Evaluators/Count.rb +8 -8
- data/lib/sapphire/DSL/Evaluators/Differ.rb +10 -10
- data/lib/sapphire/DSL/Evaluators/Exist.rb +9 -9
- data/lib/sapphire/DSL/Evaluators/In.rb +10 -10
- data/lib/sapphire/DSL/Evaluators/Selected.rb +10 -10
- data/lib/sapphire/DSL/Evaluators/Show.rb +10 -10
- data/lib/sapphire/DSL/Evaluators/Transition.rb +10 -10
- data/lib/sapphire/DSL/Evaluators/Validate.rb +8 -8
- data/lib/sapphire/DSL/Events/Data.rb +12 -0
- data/lib/sapphire/DSL/Events/DataSegment.rb +18 -0
- data/lib/sapphire/DSL/Events/Event.rb +11 -3
- data/lib/sapphire/DSL/Events/Expect.rb +3 -11
- data/lib/sapphire/DSL/Events/Expectation.rb +14 -0
- data/lib/sapphire/DSL/Events/For.rb +11 -0
- data/lib/sapphire/DSL/Events/From.rb +9 -0
- data/lib/sapphire/DSL/Events/Where.rb +11 -0
- data/lib/sapphire/DSL/Profiles/Profile.rb +26 -0
- data/lib/sapphire/DSL/Profiles/Table.rb +9 -0
- data/lib/sapphire/DSL/Scenarios/Is.rb +9 -9
- data/lib/sapphire/DSL/Scenarios/Pending.rb +28 -28
- data/lib/sapphire/DSL/Scenarios/and.rb +25 -25
- data/lib/sapphire/DSL/Scenarios/dsl.rb +59 -59
- data/lib/sapphire/DSL/Scenarios/runner.rb +40 -40
- data/lib/sapphire/Data.rb +1 -1
- data/lib/sapphire/DataAbstractions/Database.rb +57 -57
- data/lib/sapphire/DataAbstractions/Query.rb +20 -20
- data/lib/sapphire/Extensions/Class.rb +2 -2
- data/lib/sapphire/Extensions/Hash.rb +112 -112
- data/lib/sapphire/Extensions/Module.rb +11 -0
- data/lib/sapphire/Extensions/Numeric.rb +6 -6
- data/lib/sapphire/Extensions/Object.rb +4 -0
- data/lib/sapphire/Extensions/String.rb +30 -23
- data/lib/sapphire/Extensions/Symbol.rb +1 -1
- data/lib/sapphire/JobAbstractions/Job.rb +32 -32
- data/lib/sapphire/Observable.rb +87 -0
- data/lib/sapphire/Observers.rb +2 -0
- data/lib/sapphire/Observers/FailedControlHighlighter.rb +41 -0
- data/lib/sapphire/Observers/Highlighter.rb +40 -0
- data/lib/sapphire/Observers/Observer.rb +23 -0
- data/lib/sapphire/Observers/ObserverRepository.rb +37 -0
- data/lib/sapphire/Observers/PassedControlHighlighter.rb +42 -0
- data/lib/sapphire/Observers/VerboseObserver.rb +36 -0
- data/lib/sapphire/TeamCity.rb +10 -10
- data/lib/sapphire/Testing.rb +3 -3
- data/lib/sapphire/Testing/RakeTask.rb +108 -108
- data/lib/sapphire/Testing/ResultList.rb +18 -18
- data/lib/sapphire/Testing/ScenarioResult.rb +36 -36
- data/lib/sapphire/Testing/TestResult.rb +21 -21
- data/lib/sapphire/Testing/TestRunnerAdapter.rb +85 -85
- data/lib/sapphire/UI/ProcessBuilder.rb +173 -173
- data/lib/sapphire/Virtualization.rb +7 -7
- data/lib/sapphire/Web.rb +2 -2
- data/lib/sapphire/WebAbstractions/Controls/AlertBox.rb +20 -20
- data/lib/sapphire/WebAbstractions/Controls/Base/Control.rb +43 -26
- data/lib/sapphire/WebAbstractions/Controls/Button.rb +7 -7
- data/lib/sapphire/WebAbstractions/Controls/CheckBox.rb +3 -3
- data/lib/sapphire/WebAbstractions/Controls/Date.rb +11 -11
- data/lib/sapphire/WebAbstractions/Controls/DropDown.rb +6 -8
- data/lib/sapphire/WebAbstractions/Controls/Hyperlink.rb +8 -8
- data/lib/sapphire/WebAbstractions/Controls/Image.rb +19 -19
- data/lib/sapphire/WebAbstractions/Controls/Label.rb +8 -8
- data/lib/sapphire/WebAbstractions/Controls/List.rb +16 -16
- data/lib/sapphire/WebAbstractions/Controls/RadioButton.rb +2 -2
- data/lib/sapphire/WebAbstractions/Controls/Title.rb +12 -12
- data/lib/sapphire/version.rb +1 -1
- metadata +26 -8
@@ -1,21 +1,21 @@
|
|
1
|
-
module Sapphire
|
2
|
-
module DataAbstractions
|
3
|
-
module Query
|
4
|
-
def Execute(block)
|
5
|
-
x = self.Database.new
|
6
|
-
|
7
|
-
x.Open $config[x.Connection]
|
8
|
-
y = ""
|
9
|
-
y = AppConfig.Current.SqlPath if AppConfig.Current
|
10
|
-
path = File.expand_path(y + self.Script, __FILE__)
|
11
|
-
file = File.open(path)
|
12
|
-
contents = file.read
|
13
|
-
self.Tokenize(contents)
|
14
|
-
|
15
|
-
results = x.query(contents)
|
16
|
-
block.call results
|
17
|
-
x.close
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
1
|
+
module Sapphire
|
2
|
+
module DataAbstractions
|
3
|
+
module Query
|
4
|
+
def Execute(block)
|
5
|
+
x = self.Database.new
|
6
|
+
|
7
|
+
x.Open $config[x.Connection]
|
8
|
+
y = ""
|
9
|
+
y = AppConfig.Current.SqlPath if AppConfig.Current
|
10
|
+
path = File.expand_path(y + self.Script, __FILE__)
|
11
|
+
file = File.open(path)
|
12
|
+
contents = file.read
|
13
|
+
self.Tokenize(contents)
|
14
|
+
|
15
|
+
results = x.query(contents)
|
16
|
+
block.call results
|
17
|
+
x.close
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
21
|
end
|
@@ -1,113 +1,113 @@
|
|
1
|
-
class Hash < Object
|
2
|
-
|
3
|
-
def Set(hash)
|
4
|
-
hash.keys.first.Set(hash)
|
5
|
-
end
|
6
|
-
|
7
|
-
def For(item)
|
8
|
-
{item => GetPageField(item).Text}
|
9
|
-
end
|
10
|
-
|
11
|
-
def Examine(item, comparator, &block)
|
12
|
-
|
13
|
-
flip = false
|
14
|
-
key = GetKey(item) do |item| item.keys.first end
|
15
|
-
|
16
|
-
return FieldNotDefinedEvaluation.new(key, $page) if !$page.Contains key and !Parameters.instance.Contains key
|
17
|
-
|
18
|
-
field = $page.Get(key) if $page.Contains key
|
19
|
-
|
20
|
-
if(field.nil?)
|
21
|
-
key1 = GetKey(item) do |item| item[item.keys.first] end
|
22
|
-
if $page.Contains key1
|
23
|
-
field = $page.Get(key1)
|
24
|
-
flip = true
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
return Fix(Evaluation.new(Parameter(key), item.first[key]), comparator) if Parameters.instance.Contains(key) and field.nil?
|
29
|
-
|
30
|
-
return FieldNotFoundEvaluation.new(key, $page, "selenium could not find the field") if field == nil
|
31
|
-
evaluation = field.Evaluate(key, item, comparator, block)
|
32
|
-
|
33
|
-
if(flip)
|
34
|
-
left = evaluation.left
|
35
|
-
right = evaluation.right
|
36
|
-
|
37
|
-
evaluation.right = left
|
38
|
-
evaluation.left = right
|
39
|
-
end
|
40
|
-
|
41
|
-
|
42
|
-
return Fix(evaluation, comparator)
|
43
|
-
end
|
44
|
-
|
45
|
-
def GetKey(item, &block)
|
46
|
-
return block.call(item.first) if item.first.is_a? Hash
|
47
|
-
return item.first.first if item.first.is_a? Array
|
48
|
-
end
|
49
|
-
|
50
|
-
def Show(item, comparator)
|
51
|
-
Examine(item, comparator) do |field, value|
|
52
|
-
field.Equals(value, comparator)
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
def AltText(item, comparator)
|
57
|
-
Examine(item, comparator) do |field, value|
|
58
|
-
field.AltText(value, comparator)
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
def Source(item, comparator)
|
63
|
-
Examine(item, comparator) do |field, value|
|
64
|
-
field.Source(value, comparator)
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
def Contain(item, comparator)
|
69
|
-
Examine(item, comparator) do |field, value|
|
70
|
-
field.Contain(value)
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
def Count(item, comparator)
|
75
|
-
Examine(item, comparator) do |field, value|
|
76
|
-
field.Count(value)
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
def In(item, comparator)
|
81
|
-
Examine(item, comparator) do |field, value|
|
82
|
-
field.In(value, comparator)
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
|
87
|
-
def Validate(hash, comparator)
|
88
|
-
Fix(Evaluation.new(hash.keys.first.to_s, hash[hash.keys.first].to_s), comparator)
|
89
|
-
end
|
90
|
-
|
91
|
-
def Fix(evaluation, comparator)
|
92
|
-
comparator = comparator.Create(evaluation)
|
93
|
-
comparator
|
94
|
-
end
|
95
|
-
|
96
|
-
def Using(items)
|
97
|
-
$matrices ||= {}
|
98
|
-
|
99
|
-
master_array = []
|
100
|
-
master_array = $matrices[$matrix_variable] if $matrices.has_key? $matrix_variable
|
101
|
-
|
102
|
-
array = []
|
103
|
-
|
104
|
-
items.keys.each do |key|
|
105
|
-
array << Proc.new { Parameter key => items[key] }
|
106
|
-
end
|
107
|
-
|
108
|
-
master_array << array
|
109
|
-
|
110
|
-
$matrices.merge! $matrix_variable => master_array
|
111
|
-
end
|
112
|
-
|
1
|
+
class Hash < Object
|
2
|
+
|
3
|
+
def Set(hash)
|
4
|
+
hash.keys.first.Set(hash)
|
5
|
+
end
|
6
|
+
|
7
|
+
def For(item)
|
8
|
+
{item => GetPageField(item).Text}
|
9
|
+
end
|
10
|
+
|
11
|
+
def Examine(item, comparator, &block)
|
12
|
+
|
13
|
+
flip = false
|
14
|
+
key = GetKey(item) do |item| item.keys.first end
|
15
|
+
|
16
|
+
return FieldNotDefinedEvaluation.new(key, $page) if !$page.Contains key and !Parameters.instance.Contains key
|
17
|
+
|
18
|
+
field = $page.Get(key) if $page.Contains key
|
19
|
+
|
20
|
+
if(field.nil?)
|
21
|
+
key1 = GetKey(item) do |item| item[item.keys.first] end
|
22
|
+
if $page.Contains key1
|
23
|
+
field = $page.Get(key1)
|
24
|
+
flip = true
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
return Fix(Evaluation.new(Parameter(key), item.first[key]), comparator) if Parameters.instance.Contains(key) and field.nil?
|
29
|
+
|
30
|
+
return FieldNotFoundEvaluation.new(key, $page, "selenium could not find the field") if field == nil
|
31
|
+
evaluation = field.Evaluate(key, item, comparator, block)
|
32
|
+
|
33
|
+
if(flip)
|
34
|
+
left = evaluation.left
|
35
|
+
right = evaluation.right
|
36
|
+
|
37
|
+
evaluation.right = left
|
38
|
+
evaluation.left = right
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
return Fix(evaluation, comparator)
|
43
|
+
end
|
44
|
+
|
45
|
+
def GetKey(item, &block)
|
46
|
+
return block.call(item.first) if item.first.is_a? Hash
|
47
|
+
return item.first.first if item.first.is_a? Array
|
48
|
+
end
|
49
|
+
|
50
|
+
def Show(item, comparator)
|
51
|
+
Examine(item, comparator) do |field, value|
|
52
|
+
field.Equals(value, comparator)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def AltText(item, comparator)
|
57
|
+
Examine(item, comparator) do |field, value|
|
58
|
+
field.AltText(value, comparator)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def Source(item, comparator)
|
63
|
+
Examine(item, comparator) do |field, value|
|
64
|
+
field.Source(value, comparator)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def Contain(item, comparator)
|
69
|
+
Examine(item, comparator) do |field, value|
|
70
|
+
field.Contain(value)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def Count(item, comparator)
|
75
|
+
Examine(item, comparator) do |field, value|
|
76
|
+
field.Count(value)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def In(item, comparator)
|
81
|
+
Examine(item, comparator) do |field, value|
|
82
|
+
field.In(value, comparator)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
|
87
|
+
def Validate(hash, comparator)
|
88
|
+
Fix(Evaluation.new(hash.keys.first.to_s, hash[hash.keys.first].to_s), comparator)
|
89
|
+
end
|
90
|
+
|
91
|
+
def Fix(evaluation, comparator)
|
92
|
+
comparator = comparator.Create(evaluation)
|
93
|
+
comparator
|
94
|
+
end
|
95
|
+
|
96
|
+
def Using(items)
|
97
|
+
$matrices ||= {}
|
98
|
+
|
99
|
+
master_array = []
|
100
|
+
master_array = $matrices[$matrix_variable] if $matrices.has_key? $matrix_variable
|
101
|
+
|
102
|
+
array = []
|
103
|
+
|
104
|
+
items.keys.each do |key|
|
105
|
+
array << Proc.new { Parameter key => items[key] }
|
106
|
+
end
|
107
|
+
|
108
|
+
master_array << array
|
109
|
+
|
110
|
+
$matrices.merge! $matrix_variable => master_array
|
111
|
+
end
|
112
|
+
|
113
113
|
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class Module
|
2
|
+
def sub_modules
|
3
|
+
[self] + constants.map {|const| const_get(const) }
|
4
|
+
.select {|const| const.is_a? Module }
|
5
|
+
.flat_map {|const| const.sub_modules }
|
6
|
+
end
|
7
|
+
|
8
|
+
def sub_classes
|
9
|
+
self.constants.select {|c| Class === self.const_get(c)}
|
10
|
+
end
|
11
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
|
-
class Numeric < Object
|
2
|
-
|
3
|
-
def Wait
|
4
|
-
sleep(self)
|
5
|
-
end
|
6
|
-
|
1
|
+
class Numeric < Object
|
2
|
+
|
3
|
+
def Wait
|
4
|
+
sleep(self)
|
5
|
+
end
|
6
|
+
|
7
7
|
end
|
@@ -1,24 +1,31 @@
|
|
1
|
-
class String
|
2
|
-
|
3
|
-
alias_method :"plus", :"+"
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
def +(item)
|
14
|
-
return plus(Parameter(item)) if Sapphire::DSL::TestPlans::Parameters.instance.Contains(item)
|
15
|
-
raise "No Parameter defined for: " << item.to_s if item.is_a? Symbol
|
16
|
-
|
17
|
-
plus(item)
|
18
|
-
end
|
19
|
-
|
20
|
-
def For(item)
|
21
|
-
item
|
22
|
-
end
|
23
|
-
|
1
|
+
class String
|
2
|
+
|
3
|
+
alias_method :"plus", :"+"
|
4
|
+
|
5
|
+
def underscore
|
6
|
+
self.gsub(/::/, '/').
|
7
|
+
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
8
|
+
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
9
|
+
tr("-", "_").
|
10
|
+
downcase
|
11
|
+
end
|
12
|
+
|
13
|
+
def +(item)
|
14
|
+
return plus(Parameter(item)) if Sapphire::DSL::TestPlans::Parameters.instance.Contains(item)
|
15
|
+
raise "No Parameter defined for: " << item.to_s if item.is_a? Symbol and !item.nil?
|
16
|
+
|
17
|
+
plus(item)
|
18
|
+
end
|
19
|
+
|
20
|
+
def For(item)
|
21
|
+
item
|
22
|
+
end
|
23
|
+
|
24
|
+
def namespace
|
25
|
+
self.gsub(/::/, '_').
|
26
|
+
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
27
|
+
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
28
|
+
tr("-", "_")
|
29
|
+
end
|
30
|
+
|
24
31
|
end
|
@@ -1,32 +1,32 @@
|
|
1
|
-
module Sapphire
|
2
|
-
module JobAbstractions
|
3
|
-
class Job
|
4
|
-
|
5
|
-
def Server
|
6
|
-
return ""
|
7
|
-
end
|
8
|
-
|
9
|
-
def User
|
10
|
-
return ""
|
11
|
-
end
|
12
|
-
|
13
|
-
def Password
|
14
|
-
return ""
|
15
|
-
end
|
16
|
-
|
17
|
-
def Path
|
18
|
-
return ""
|
19
|
-
end
|
20
|
-
|
21
|
-
def PsExecPath
|
22
|
-
return ""
|
23
|
-
end
|
24
|
-
|
25
|
-
def Arg
|
26
|
-
return nil
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
1
|
+
module Sapphire
|
2
|
+
module JobAbstractions
|
3
|
+
class Job
|
4
|
+
|
5
|
+
def Server
|
6
|
+
return ""
|
7
|
+
end
|
8
|
+
|
9
|
+
def User
|
10
|
+
return ""
|
11
|
+
end
|
12
|
+
|
13
|
+
def Password
|
14
|
+
return ""
|
15
|
+
end
|
16
|
+
|
17
|
+
def Path
|
18
|
+
return ""
|
19
|
+
end
|
20
|
+
|
21
|
+
def PsExecPath
|
22
|
+
return ""
|
23
|
+
end
|
24
|
+
|
25
|
+
def Arg
|
26
|
+
return nil
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
@@ -0,0 +1,87 @@
|
|
1
|
+
module Sapphire
|
2
|
+
module Observable
|
3
|
+
def self.intercept(base, hash, *method_names)
|
4
|
+
do_before = proc { |name, inst, args|
|
5
|
+
begin
|
6
|
+
hash[:before].call(name, inst, args)
|
7
|
+
rescue => observer_exception
|
8
|
+
|
9
|
+
end
|
10
|
+
}
|
11
|
+
|
12
|
+
do_on_success = proc { |name, inst, args|
|
13
|
+
begin
|
14
|
+
hash[:success].call(name, inst, args)
|
15
|
+
rescue => raised_exception
|
16
|
+
raise raised_exception
|
17
|
+
end
|
18
|
+
}
|
19
|
+
|
20
|
+
do_on_failure = proc { |name, inst, raised_exception, args|
|
21
|
+
begin
|
22
|
+
hash[:failure].call(name, inst, raised_exception, args)
|
23
|
+
rescue => observer_exception
|
24
|
+
|
25
|
+
end
|
26
|
+
}
|
27
|
+
|
28
|
+
do_after = proc { |name, inst, args|
|
29
|
+
begin
|
30
|
+
hash[:after].call(name, inst, args)
|
31
|
+
rescue => observer_exception
|
32
|
+
|
33
|
+
end
|
34
|
+
}
|
35
|
+
|
36
|
+
method_names.each do |method_name|
|
37
|
+
method = base.instance_method(method_name)
|
38
|
+
base.send :define_method, method_name.to_sym do |*args, &method_block|
|
39
|
+
do_before.call(method_name, self, args) if hash.has_key? :before
|
40
|
+
begin
|
41
|
+
result = method.bind(self).(*args, &method_block)
|
42
|
+
do_on_success.call(method_name, self, args) if hash.has_key? :after
|
43
|
+
return result
|
44
|
+
rescue => raised_exception
|
45
|
+
do_on_failure.call(method_name, self, raised_exception, args) if hash.has_key? :failure
|
46
|
+
raise raised_exception
|
47
|
+
ensure
|
48
|
+
do_after.call(method_name, self, args) if hash.has_key? :success
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.included(base)
|
55
|
+
before = proc { |name, inst, args|
|
56
|
+
observers = Observers::ObserverRepository.instance.Find(name.to_sym, inst.class)
|
57
|
+
observers.each do |x| x.Before(inst, name, args) if x.respond_to? :Before end
|
58
|
+
}
|
59
|
+
|
60
|
+
after = proc { |name, inst, args|
|
61
|
+
observers = Observers::ObserverRepository.instance.Find(name.to_sym, inst.class)
|
62
|
+
observers.each do |x| x.After(inst, name, args) if x.respond_to? :After end
|
63
|
+
}
|
64
|
+
|
65
|
+
success = proc { |name, inst, args|
|
66
|
+
observers = Observers::ObserverRepository.instance.Find(name.to_sym, inst.class)
|
67
|
+
observers.each do |x| x.OnSuccess(inst, name, args) if x.respond_to? :OnSuccess end
|
68
|
+
}
|
69
|
+
|
70
|
+
|
71
|
+
failure = proc { |name, inst, exception, args|
|
72
|
+
observers = Observers::ObserverRepository.instance.Find(name.to_sym, inst.class)
|
73
|
+
observers.each do |x| x.OnFailure(inst, name, exception, args) if x.respond_to? :OnFailure end
|
74
|
+
}
|
75
|
+
|
76
|
+
|
77
|
+
hash = {}
|
78
|
+
|
79
|
+
hash.merge! :before => before,
|
80
|
+
:after => after,
|
81
|
+
:failure => failure,
|
82
|
+
:success => success
|
83
|
+
|
84
|
+
intercept(base, hash, *base.instance_methods(false))
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|