Module: DataFactory
- Defined in:
- lib/test-factory/data_factory.rb
Overview
Provides a set of tools used to create your Data Object classes.
Instance Method Summary collapse
-
#checkbox_setting(checkbox) ⇒ Object
(also: #radio_setting)
Transform for use with data object instance variables that refer to checkboxes or radio buttons.
-
#get_or_select(var, select_list) ⇒ Object
Use this method in your data object class for select list boxes that will have default values you may be interested in.
-
#requires(*elements) ⇒ Object
Items passed to this method are checked to ensure that the associated class instance variable is not nil.
-
#set_options(hash) ⇒ Object
(also: #update_options)
Add this to the bottom of your Data Object’s initialize method.
Instance Method Details
#checkbox_setting(checkbox) ⇒ Object Also known as: radio_setting
Transform for use with data object instance variables that refer to checkboxes or radio buttons.
33 34 35 |
# File 'lib/test-factory/data_factory.rb', line 33 def checkbox_setting(checkbox) checkbox.set? ? :set : :clear end |
#get_or_select(var, select_list) ⇒ Object
Use this method in your data object class for select list boxes that will have default values you may be interested in. The method will either set the select box to the value specified, or, if no value exists for the given variable in your data object, it will get the value from the UI and set the data object’s variable to that value. Note that this only supports select lists that allow a single selection.
48 49 50 51 52 53 54 |
# File 'lib/test-factory/data_factory.rb', line 48 def get_or_select(var, select_list) if var==nil select_list.[0].text else select_list.select var end end |
#requires(*elements) ⇒ Object
Items passed to this method are checked to ensure that the associated class instance variable is not nil. If it is, the script is aborted and an error is thrown, describing what information the Data Object requires before the script can run.
23 24 25 26 27 |
# File 'lib/test-factory/data_factory.rb', line 23 def requires(*elements) elements.each do |inst_var| raise "You've neglected to define a required variable for the #{self}." if inst_var==nil end end |
#set_options(hash) ⇒ Object Also known as: update_options
Add this to the bottom of your Data Object’s initialize method. Converts the contents of the hash into the class’s instance variables.
7 8 9 10 11 |
# File 'lib/test-factory/data_factory.rb', line 7 def (hash) hash.each do |key, value| instance_variable_set("@#{key}", value) end end |