In the previous post in data setting, I talked about the start of a data builder pattern. Sometimes it’s nice to have your automation use descriptive phrases that stand in for specific bits of data. So here I’ll describe how Symbiont allows this by adhering to a data builder pattern.
As with the last post, here I’ll be using the warp factor calculator area of the Dialogic site. You might want to follow the directions in the last post regarding having a local copy available.
The data builder module builds off of the data setter concept. To see how this works, start with the following script and page definition:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
require 'symbiont' include Symbiont::Factory include Symbiont::DataBuilder class Warp attach Symbiont url_is 'http://localhost:9292/warp' text_field :warpFactor, id: 'warpInput' text_field :velocity, id: 'velocityInput' text_field :distance, id: 'distInput' end |
Notice in particular the line that includes the DataBuilder. I’ll come back to that. With that in place, put in the following script logic:
1 2 3 |
Symbiont.set_browser on_view(Warp).using(data_about('alpha centauri')) |
Here notice that the using
action (from data setter) is being used but is also referring to another action, data_about
. What this is doing is telling Symbiont that you want to use a specific set of data that is given by a name (in this case, “alpha centauri”). Note that to be able to call data_about
directly as is done here, you must have the include line that brings in the DataBuilder module.
But where is this data coming from? Symbiont has a convention built in that if you don’t tell it a specific data file to use, it will assume a file called default.yml that is stored in a data directory. To see this work, create a directory called data and put the following in a file called default.yml:
1 2 3 4 |
alpha centauri: warpFactor: 1 velocity: 2 distance: 4.3 |
So here the “data_about” action will now find the identifier called “alpha centauri”. That file contains names that match element definitions on the page definition. For each element, there is a data value specified.
You are able to specify specific data, just as you could with the data setter. So, for example, the above action could be this:
1 |
on_view(Warp).using(data_about('alpha centauri', :distance => '12')) |
Here the data set for ‘alpha centauri’ will be used but the value of distance in that file (4.3) will be replaced by the specified new value for distance (12).
What if you want to use a data file that is not called default.yml? What if, for example, I have my ‘alpha centauri’ data set in a file called ‘warp_factor.yml’. If that file is in your data directory, you could simply do this:
1 |
on_view(Warp).using(data_about('warp_factor/alpha_centauri')) |
That still assumes the warp_factor file is in the data directory. What if you want to have data files in a different directory? You can specify a directory that the DataBuilder module should use. Let’s say my warp_factor.yml is stored in a common/data directory. You could add a command like this to your script:
1 |
Symbiont::DataBuilder.data_path = 'common/data' |
Then the same line as before
1 |
on_view(Warp).using(data_about('warp_factor/alpha centauri')) |
will now look for a warp_factor.yml file in the common/data directory.
Finally, you can load up a specific data file if you want by doing this:
1 |
Symbiont::DataBuilder.load 'warp_factor.yml' |
That allows you to get access to the data and perhaps sent it into a further data building mechanism that you include as part of your library.
At this point, I’m not sure how much further I’m going to evolve the data builder pattern in Symbiont. The goal was to provide a base upon which you can build your own data mechanisms that are specific to your needs. I’m trying not to clutter the Symbiont code base with too many of my own assumptions on what people will need. So right now I’ve put in a base that allows you to use concise and expressive statements to work with data.