Module: EDSL::PageObject::Population

Included in:
Page, Section
Defined in:
lib/edsl/page_object/population.rb

Overview

This module serves as a mixin for element container to support populating their fields via a hash.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fixture_fetcherObject

Returns the currently defined fixture fetch function or a lambda that returns nil



31
32
33
# File 'lib/edsl/page_object/population.rb', line 31

def self.fixture_fetcher
  @@fixture_fetcher ||= lambda { |_key| nil }
end

.fixture_fetcher=(proc_or_name) ⇒ Object

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

Parameters:

  • proc_or_name (Proc, String, Symbol)

    A Proc to call or the name of a method to send.



26
27
28
# File 'lib/edsl/page_object/population.rb', line 26

def self.fixture_fetcher=(proc_or_name)
  @@fixture_fetcher = proc_or_name
end

Instance Method Details

#fixture_fetch(key) ⇒ Object

Fetch a value from our fixtures using a key.

Parameters:

  • key (String, Symbol)

    What to fetch



38
39
40
41
42
43
44
# 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

#populateObject



82
83
84
# File 'lib/edsl/page_object/population.rb', line 82

def populate
  populate_with(fixture_fetch(populate_key))
end

#populate_keyObject

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.



78
79
80
# File 'lib/edsl/page_object/population.rb', line 78

def populate_key
  self.class.to_s.snakecase
end

#populate_with(data) ⇒ Object

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.

can be either a string or a symbol.

Parameters:

  • data (Hash)

    the data to use to populate this container. The key



63
64
65
66
67
68
69
70
71
72
73
# 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