parfait 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,226 @@
1
+ module Parfait
2
+
3
+ class Page
4
+
5
+ attr_reader :name,
6
+ :aliases
7
+
8
+ # Create a Parfait page
9
+ #
10
+ # *Options*
11
+ #
12
+ # +name+:: specifies the name of the page
13
+ # +aliases+:: specifies an array of aliases for the page
14
+ #
15
+ # *Example*
16
+ #
17
+ # Parfait::Page.new(
18
+ # :name => "Prescription New",
19
+ # :aliases => ["New Prescription", "New Rx", "Rx New"]
20
+ # )
21
+ #
22
+ def initialize(opts = {})
23
+ o = {
24
+ :name => nil,
25
+ :aliases => []
26
+ }.merge(opts)
27
+
28
+ @name = o[:name]
29
+ raise "Parfait::Page requires a name to be defined" unless @name
30
+ raise "Parfait::Page requires name to be a string" unless @name.is_a?(String)
31
+
32
+ @aliases = o[:aliases]
33
+ if @aliases
34
+ raise "Parfait::Page requires aliases to be an array" unless @aliases.is_a?(Array)
35
+ @aliases.each do |my_alias|
36
+ raise "Parfait::Page requires each alias in the array to be a string" unless my_alias.is_a?(String)
37
+ end
38
+ end
39
+
40
+ @controls = Hash.new
41
+ @regions = Hash.new
42
+ @page_test = nil
43
+ end
44
+
45
+
46
+ # Add a Region to the current Page
47
+ #
48
+ # *Options*
49
+ #
50
+ # +region+:: specifies the Region object to be added
51
+ #
52
+ # *Example*
53
+ #
54
+ # myregion = Parfait::Region.new(
55
+ # :name => "User List"
56
+ # )
57
+ # page.add_region(myregion)
58
+ #
59
+ def add_region(region)
60
+
61
+ if region
62
+ if region.is_a?(Parfait::Region)
63
+ @regions[region.name] = region
64
+ if region.aliases
65
+ region.aliases.each do |my_alias|
66
+ @regions[my_alias] = region
67
+ end
68
+ end
69
+ else
70
+ raise "Region must be a Parfait::Region when being added to a Page"
71
+ end
72
+ else
73
+ raise "Region must be specified when adding a Region to a Page"
74
+ end
75
+ self
76
+ end
77
+
78
+
79
+ # Retrieve a Region object by name or alias.
80
+ #
81
+ # Under the covers, if there is an existence directive defined for this
82
+ # region, it will be run on the current browser to confirm that it is
83
+ # indeed present.
84
+ #
85
+ # *Options*
86
+ #
87
+ # +name+:: specifies the name or alias of the region
88
+ #
89
+ # *Example*
90
+ #
91
+ # myregion.region("User List" => username)
92
+ #
93
+ def region(opts = {})
94
+ region = @regions[opts.first[0]]
95
+ if region
96
+ # Apply the filter method
97
+ region.filter(opts.first[1])
98
+
99
+ return region
100
+ else
101
+ raise "Invalid region name requested: \"#{requested_name}\""
102
+ end
103
+ end
104
+
105
+
106
+ # Add a Control to the current Page
107
+ #
108
+ # *Options*
109
+ #
110
+ # +control+:: specifies the Control object to be added
111
+ #
112
+ # *Example*
113
+ #
114
+ # page = Parfait::Page.new(
115
+ # :name => "Prescription New",
116
+ # :aliases => ["New Prescription", "New Rx", "Rx New"]
117
+ # )
118
+ # control = Parfait::Control.new(
119
+ # :name => "Prescriber Name",
120
+ # :text => "prescriber full name"
121
+ # )
122
+ # page.add_control(control)
123
+ #
124
+ def add_control(control)
125
+
126
+ if control
127
+ if control.is_a?(Parfait::Control)
128
+ @controls[control.name] = control
129
+ if control.aliases
130
+ control.aliases.each do |my_alias|
131
+ @controls[my_alias] = control
132
+ end
133
+ end
134
+ else
135
+ raise "Control must be a Parfait::Control when being adding to a Page"
136
+ end
137
+ else
138
+ raise "Control must be specified when adding a Control to an Page"
139
+ end
140
+ self
141
+ end
142
+
143
+
144
+ # Method description
145
+ #
146
+ # *Options*
147
+ #
148
+ # +option+:: specifies something
149
+ #
150
+ # *Example*
151
+ #
152
+ # $$$ Need an example $$$
153
+ def add_page_test(&block)
154
+ @page_test = block
155
+ end
156
+
157
+
158
+ # Method description
159
+ #
160
+ # *Options*
161
+ #
162
+ # +option+:: specifies something
163
+ #
164
+ # *Example*
165
+ #
166
+ # $$$ Need an example $$$
167
+ def page_test(&block)
168
+ @page_test.call()
169
+ end
170
+
171
+
172
+ # Method description
173
+ #
174
+ # *Options*
175
+ #
176
+ # +option+:: specifies something
177
+ #
178
+ # *Example*
179
+ #
180
+ # $$$ Need an example $$$
181
+ def add_navigation(&block)
182
+ @navigate_method = block
183
+ end
184
+
185
+
186
+ # Method description
187
+ #
188
+ # *Options*
189
+ #
190
+ # +option+:: specifies something
191
+ #
192
+ # *Example*
193
+ #
194
+ # $$$ Need an example $$$
195
+ def navigate(opts = {})
196
+ @navigate_method.call(opts)
197
+ end
198
+
199
+
200
+ # Retrieve a Control object by name or alias.
201
+ #
202
+ # Under the covers, if there is an existence directive defined for this
203
+ # control, it will be run on the current browser to confirm that it is
204
+ # indeed present.
205
+ #
206
+ # *Options*
207
+ #
208
+ # +name+:: specifies the name or alias of the control
209
+ #
210
+ # *Example*
211
+ #
212
+ # mypage.control("User ID")
213
+ def control(requested_name)
214
+ control = @controls[requested_name]
215
+ if control
216
+ # Confirm that the requested control is present
217
+
218
+ return control
219
+ else
220
+ raise "Invalid control name requested: \"#{requested_name}\""
221
+ end
222
+ end
223
+
224
+ end
225
+
226
+ end
@@ -0,0 +1,267 @@
1
+ module Parfait
2
+
3
+ class Region
4
+
5
+ attr_reader :name,
6
+ :aliases
7
+
8
+ # Create a new region object
9
+ #
10
+ # Regions use filters to reduce the scope of HTML that their child
11
+ # elements (Controls or other Regions) need to consider. This allows
12
+ # for easier management of repeated controls on a page.
13
+ #
14
+ # For instance, assume the Region's parent is a Page and that
15
+ # page contains a list of users, each with an "Edit" link next to them.
16
+ # Defining a Region would allow any child controls to look
17
+ # specifically at a single user, by userid, perhaps. Then a child
18
+ # control can be created to select "Edit" for the chosen userid.
19
+ #
20
+ # *Options*
21
+ #
22
+ # +name+:: the name used to identify this region
23
+ # +aliases+:: specifies an array of aliases for the region
24
+ #
25
+ # *Example*
26
+ #
27
+ # # Define the application
28
+ # mybrowser = Watir::Browser.new()
29
+ # myapp = Parfait::Application.new(
30
+ # :name => "Blog",
31
+ # :browser => mybrowser
32
+ # )
33
+ #
34
+ # # Define the page
35
+ # mypage = Parfait::Page.new(
36
+ # :name => "User List"
37
+ # )
38
+ # mybrowser.add_page(mypage)
39
+ #
40
+ # # Define the region
41
+ # myregion = Parfait::Region.new(
42
+ # :name => "User"
43
+ # )
44
+ #
45
+ def initialize(opts = {})
46
+ o = {
47
+ :name => nil,
48
+ :aliases => []
49
+ }.merge(opts)
50
+ @name = o[:name]
51
+ @aliases = o[:aliases]
52
+ @controls = Hash.new
53
+ @regions = Hash.new
54
+ @pages = Hash.new
55
+
56
+ if @name
57
+ unless @name.is_a?(String)
58
+ raise "Name must be a String when adding a region"
59
+ end
60
+ else
61
+ raise "Name must be specified when adding a region"
62
+ end
63
+ if @aliases
64
+ unless @aliases.is_a?(Array)
65
+ raise "Parfait::Region requires aliases to be an array"
66
+ end
67
+ @aliases.each do |my_alias|
68
+ raise "Parfait::Region requires each alias in the array to be a string" unless my_alias.is_a?(String)
69
+ end
70
+ end
71
+ end
72
+
73
+
74
+ # Set the filter for this Region to use.
75
+ #
76
+ # When a child of this Region is invoked, the Region will use this
77
+ # filter to zoom in on a subset of the parent element's HTML in order
78
+ # to reduce the scope of HTML that the child needs to consider.
79
+ #
80
+ # The defined filter should alter the +Thread.current[:parfait_region]+
81
+ # value.
82
+ #
83
+ # *Options*
84
+ #
85
+ # +block+:: specifies the block of code that will reduce the scope of HTML by updating +Thread.current[:parfait_region]+. The block should take a single parameter indicating data unique to the filtered region. For example, in a list of users, the block parameter could be a user ID.
86
+ #
87
+ # *Example*
88
+ #
89
+ # # Define the application
90
+ # mybrowser = Watir::Browser.new()
91
+ # myapp = Parfait::Application.new(
92
+ # :name => "Blog",
93
+ # :browser => mybrowser
94
+ # )
95
+ #
96
+ # # Define the page
97
+ # mypage = Parfait::Page.new(
98
+ # :name => "User List"
99
+ # )
100
+ # mybrowser.add_page(mypage)
101
+ #
102
+ # # Define the region
103
+ # myregion = Parfait::Region.new(
104
+ # :name => "User"
105
+ # )
106
+ #
107
+ # # Set the filter for this region to look specifically at user ID
108
+ # myregion.add_filter { |userid|
109
+ # Thread.current[:parfait_region] = Thread.current[:parfait_region].div(:id => "userlist").div(:text => /#{userid}/)
110
+ # }
111
+ # mypage.add_region(myregion)
112
+ #
113
+ # # Use the Region to focus on the entry for User "jrotter"
114
+ # myapp.page("User List").region("User" => "jrotter")
115
+ #
116
+ def add_filter(&block)
117
+ @filter_method = block
118
+ end
119
+
120
+
121
+ # Call the filter for this region
122
+ #
123
+ # *Options*
124
+ #
125
+ # +param+:: specifies the parameter to use when applying the filter for this region
126
+ #
127
+ # *Example*
128
+ #
129
+ # region.filter("patches.ohoulihan")
130
+ #
131
+ def filter(param)
132
+ @filter_method.call(param)
133
+ end
134
+
135
+
136
+ # Add a Region to the current Region
137
+ #
138
+ # *Options*
139
+ #
140
+ # +region+:: specifies the Region object to be added
141
+ #
142
+ # *Example*
143
+ #
144
+ # region1 = Parfait::Region.new(
145
+ # :name => "User List"
146
+ # )
147
+ # page.add_region(region1)
148
+ # region2 = Parfait::Region.new(
149
+ # :name => "User Roles"
150
+ # )
151
+ # region1.add_region(region2)
152
+ #
153
+ def add_region(region)
154
+
155
+ if region
156
+ if region.is_a?(Parfait::Region)
157
+ @regions[region.name] = region
158
+ if region.aliases
159
+ region.aliases.each do |my_alias|
160
+ @regions[my_alias] = region
161
+ end
162
+ end
163
+ else
164
+ raise "Region must be a Parfait::Region when being added to another Region"
165
+ end
166
+ else
167
+ raise "Region must be specified when adding a Region to another Region"
168
+ end
169
+ self
170
+ end
171
+
172
+
173
+ # Retrieve a Region object by name or alias.
174
+ #
175
+ # Under the covers, if there is an existence directive defined for this
176
+ # region, it will be run on the current browser to confirm that it is
177
+ # indeed present.
178
+ #
179
+ # *Options*
180
+ #
181
+ # +name+:: specifies the name or alias of the region
182
+ #
183
+ # *Example*
184
+ #
185
+ # myregion.region("User List" => username)
186
+ #
187
+ def region(opts = {})
188
+ region = @regions[opts.first[0]]
189
+ if region
190
+ # Apply the filter method
191
+ region.filter(opts.first[1])
192
+
193
+ return region
194
+ else
195
+ raise "Invalid region name requested: \"#{opts.first[0]}\""
196
+ end
197
+ end
198
+
199
+
200
+ # Add a Control to the current Region
201
+ #
202
+ # *Options*
203
+ #
204
+ # +control+:: specifies the Control object to be added
205
+ #
206
+ # *Example*
207
+ #
208
+ # region = Parfait::Region.new(
209
+ # :name => "Prescription List"
210
+ # )
211
+ # control = Parfait::Control.new(
212
+ # :name => "Edit Prescription"
213
+ # )
214
+ # region.add_control(control)
215
+ #
216
+ def add_control(control)
217
+
218
+ if control
219
+ if control.is_a?(Parfait::Control)
220
+ @controls[control.name] = control
221
+ if control.aliases
222
+ control.aliases.each do |my_alias|
223
+ @controls[my_alias] = control
224
+ end
225
+ end
226
+ else
227
+ raise "Control must be a Parfait::Control when being adding to a Region"
228
+ end
229
+ else
230
+ raise "Control must be specified when adding a Control to an Region"
231
+ end
232
+ self
233
+ end
234
+
235
+
236
+ # Retrieve a Control object by name or alias.
237
+ #
238
+ # Under the covers, if there is an existence directive defined for this
239
+ # control, it will be run on the current browser to confirm that it is
240
+ # indeed present.
241
+ #
242
+ # *Options*
243
+ #
244
+ # +name+:: specifies the name or alias of the control
245
+ #
246
+ # *Example*
247
+ #
248
+ # myregion.control("User ID")
249
+ #
250
+ def control(requested_name)
251
+ control = @controls[requested_name]
252
+ if control
253
+ # Confirm that the requested control is present
254
+
255
+ return control
256
+ else
257
+ raise "Invalid control name requested: \"#{requested_name}\""
258
+ end
259
+ end
260
+
261
+ end
262
+
263
+ end
264
+
265
+
266
+
267
+