Use Case: Read key/value pairs from yaml files and create classes for each of them. The dynamic classes should have an initialize method that calls the load_page_objects_with_globals method on each yml file.
Problem: The initialize method does not get implemented with each class created dynamically.
def load_page_object_with_globals page_objects_file_name
load_page_object_file(open("filepath/page_objects/#{page_objects_file_name}"))
load_page_object_file(open("filepath/page_objects/global_page_object.yml"))
puts page_objects_file_name
end
#dynamically creates methods based key/value pairs in yaml file
def load_page_object_file(file)
page_object = YAML.load(File.read(file))
page_object.each do |k, v|
self.class.__send__ :define_method, k do v end
end
end
#
def load_page_object_classes
page_object_class_names = Array.new
#camelizes the filenames into class name format
Dir.glob("filepath/page_objects/*.yml").each do |item|
filename = File.basename(item, ".*")
filename = filename.dup.split(/[_-]/).map(&:capitalize).join("")
page_object_class_names.push(filename)
end
puts page_object_class_names.inspect
#PROBLEM CODE - dynamically create a class for each page object name and create initalize method
page_object_class_names.each {
|class_name|
page_object_classes = Class.new(Object) do
define_method(initialize) do
load_page_object_with_globals(open("filepath/page_objects/#{class_name}.yml"))
end
end
Class.const_set(class_name, page_object_classes)
puts page_object_classes.inspect
}
end
load_page_object_classes
define_method(:initialize)?