Class: Dill::FieldGroup
- Defined in:
- lib/dill/widgets/field_group.rb
Overview
Explain how to use locators when defining fields, including what happens when locators are omitted.
A group of form fields.
Direct Known Subclasses
Constant Summary
Constants included from Dill
Instance Attribute Summary
Attributes inherited from Widget
Field definition macros collapse
-
.check_box(name, locator = nil) ⇒ Object
Creates a new checkbox accessor.
-
.field(name, locator, type) ⇒ Object
private
Defines a new field.
-
.select(name, locator = nil) ⇒ Object
Creates a new select accessor.
-
.text_field(name, locator = nil) ⇒ Object
Creates a new text field accessor.
Class Method Summary collapse
- .default_locator(type = nil, &block) ⇒ Object
-
.field_names ⇒ Set
The names of all the fields that belong to this field group.
Instance Method Summary collapse
-
#fields ⇒ Object
This field group’s field widgets.
-
#set(attributes) ⇒ Object
Sets the given form attributes.
-
#to_table ⇒ Array<Array>
Converts the current field group into a table suitable for diff’ing with Cucumber::Ast::Table.
Methods inherited from Widget
action, #class?, #classes, #click, filter, filter?, find_all_in, find_in, #has_action?, #hover, #html, #id, #initialize, not_present_in?, present_in?, root, selector, #text, #to_cell, #to_s, #value, widget_delegator
Methods included from Widgets::DSL
Methods included from WidgetParts::Container
#has_widget?, #not_visible?, #visible?, #widget, #widgets
Methods included from Dill
Methods included from Constructors
Methods included from WidgetParts::Struct
Constructor Details
This class inherits a constructor from Dill::Widget
Class Method Details
.check_box(name, locator = nil) ⇒ Object
Handle checkbox access when the field is disabled (raise an exception?)
Creates a new checkbox accessor.
Adds the following methods to the widget:
- <name>
-
Gets the current checkbox state, as a boolean. Returns
true
if the corresponding check box is checked,false
otherwise. - <name>=
-
Sets the current checkbox state. Pass
true
to check the checkbox,false
otherwise.
72 73 74 |
# File 'lib/dill/widgets/field_group.rb', line 72 def self.check_box(name, locator = nil) field name, locator, CheckBox end |
.default_locator(type = nil, &block) ⇒ Object
9 10 11 12 13 |
# File 'lib/dill/widgets/field_group.rb', line 9 def self.default_locator(type = nil, &block) alias_method :name_to_locator, type if type define_method :name_to_locator, &block if block end |
.field(name, locator, type) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Defines a new field.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/dill/widgets/field_group.rb', line 83 def self.field(name, locator, type) raise TypeError, "can't convert `#{name}' to Symbol" \ unless name.respond_to?(:to_sym) field_names << name.to_sym label = name.to_s.gsub(/_/, ' ').capitalize locator ||= label name, locator, type do define_method :label do label end end define_method "#{name}=" do |val| (name).set val end define_method name do (name).get end end |
.field_names ⇒ Set
The names of all the fields that belong to this field group.
Field names are automatically added to this group as long as you use the field definition macros.
23 24 25 |
# File 'lib/dill/widgets/field_group.rb', line 23 def self.field_names @field_names ||= Set.new end |
.select(name, locator = nil) ⇒ Object
Handle select access when the field is disabled (raise an exception?)
Raise an exception when an option doesn’t exist.
Allow passing the option value to set an option.
Ensure an option with no text returns the empty string.
What to do when nil
is passed to the writer?
Creates a new select accessor.
Adds the following methods to the widget:
- <name>
-
Gets the current selected option. Returns the label of the selected option, or
nil
, if no option is selected. - <name>=
-
Selects an option on the current select. Pass the label of the option you want to select.
160 161 162 |
# File 'lib/dill/widgets/field_group.rb', line 160 def self.select(name, locator = nil) field name, locator, Select end |
.text_field(name, locator = nil) ⇒ Object
Handle text field access when the field is disabled (raise an exception?)
Creates a new text field accessor.
Adds the following methods to the widget:
- <name>
-
Returns the current text field value, or
nil
if no value has been set. - <name>=
-
Sets the current text field value.
<name>? Returns true
if the current text field has content or
+false+ otherwise
211 212 213 214 215 216 217 |
# File 'lib/dill/widgets/field_group.rb', line 211 def self.text_field(name, locator = nil) define_method "#{name}?" do (name).content? end field name, locator, TextField end |
Instance Method Details
#fields ⇒ Object
Returns This field group’s field widgets.
222 223 224 |
# File 'lib/dill/widgets/field_group.rb', line 222 def fields self.class.field_names.map { |name| (name) } end |
#set(attributes) ⇒ Object
Sets the given form attributes.
231 232 233 234 235 236 237 |
# File 'lib/dill/widgets/field_group.rb', line 231 def set(attributes) attributes.each do |k, v| send "#{k}=", v end self end |
#to_table ⇒ Array<Array>
Converts the current field group into a table suitable for diff’ing with Cucumber::Ast::Table.
Field labels are determined by the widget name.
Field values correspond to the return value of each field’s to_s
.
247 248 249 250 251 252 |
# File 'lib/dill/widgets/field_group.rb', line 247 def to_table headers = fields.map { |field| field.label.downcase } body = fields.map { |field| field.to_s.downcase } [headers, body] end |