module EDSL::PageObject::Population
This module serves as a mixin for element container to support populating their fields via a hash.
Public Class Methods
Returns the currently defined fixture fetch function or a lambda that returns nil
# File lib/edsl/page_object/population.rb, line 31 def self.fixture_fetcher @@fixture_fetcher ||= lambda { |_key| nil } end
This method allows you to specify a function to be used to fetch fixture data, given a key. The value passed should either be a proc, or a method name for send.
Examples: EDSL::Population.fixture_fetch= :data_for
EDSL::Population.fixture_fetch= lambda.new { |key| data_for key }
Both of these examples would call data_for from DataMagic
@param proc_or_name [Proc, String, Symbol] A Proc to call or the name of a method to send.
# File lib/edsl/page_object/population.rb, line 26 def self.fixture_fetcher=(proc_or_name) @@fixture_fetcher = proc_or_name end
Public Instance Methods
Fetch a value from our fixtures using a key.
@param key [String, Symbol] What to fetch
# File lib/edsl/page_object/population.rb, line 38 def fixture_fetch(key) ff = EDSL::PageObject::Population.fixture_fetcher data = ff.call(key) if ff.is_a?(Proc) data = send(ff, key) unless ff.is_a?(Proc) EDSL::PageObject.fixture_cache[key] = data data end
# File lib/edsl/page_object/population.rb, line 82 def populate populate_with(fixture_fetch(populate_key)) end
This method will provide a value that can be used as a key for selecting data out of a hash for a specific container. It uses the class name of the object and converts it into snake case so LoginPage would have a populate key of login_page.
# File lib/edsl/page_object/population.rb, line 78 def populate_key self.class.to_s.snakecase end
This method will populate the various elements within a container, using a hash.
If the hash contains a key that matches the populate_key
of the container, the value contained there will be used instead of the passed hash. Otherwise, the data will be used directly.
For each key in the hash, this method will call send(“#{key}=”) passing it the value from the hash (if we respond to that message).
This function makes no attempt to determine the type of element being set, it assumes that what ever you put in the value can be consumed by the assignment function.
Values are populated in the order they appear in the hash.
@param data [Hash] the data to use to populate this container. The key can be either a string or a symbol.
# File lib/edsl/page_object/population.rb, line 63 def populate_with(data) data = data.fetch(populate_key, data) data = data.fetch(populate_key.to_sym, data) data.each do |k, v| begin send("#{k}=", v) if respond_to?("#{k}=") rescue Exception => ex raise "#{ex.message} raised with setting #{k}" end end end