1

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
6
  • 1
    define_method(:initialize)? Commented Sep 13, 2018 at 14:49
  • 5
    Could you create a smaller code example to illustrate the problem, reading files and YAML parsing isn't part of the problem, presumably. Commented Sep 13, 2018 at 15:52
  • 2
    "The initialize method does not get implemented with each class created dynamically." is not a precise enough error description for us to help you. What doesn't work? How doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do they differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and how do they differ? Commented Sep 14, 2018 at 0:58
  • 1
    Can you provide a precise specification of what it is that you want to happen, including any and all rules, exceptions from those rules, corner cases, special cases, boundary cases, and edge cases? Can you provide sample inputs and outputs demonstrating what you expect to happen, both in normal cases, and in all the exceptions, corner cases, special cases, boundary cases, and edge cases? Please, also make sure to provide a minimal reproducible example. In particular, your code seems to fail both the "minimal" and the "complete" part. It is incomplete because it requires several YAML files to run which you don't … Commented Sep 14, 2018 at 1:00
  • 2
    … give us. And it is not minimal … because it requires several YAML files to run and is over 30 lines of code, whereas I think (if I understood the problem correctly, which I am however not sure about, because there is so much code and I have no idea whether or not the YAML files I created are correct), the problem should be reproducible in 5 lines or less. Commented Sep 14, 2018 at 1:02

1 Answer 1

2

Something like this worked for me:

require 'active_support/all'

def multiply_by(number1, number2)
  number1 * number2
end

hash = { foo: 1, bar: 2, baz: 3 }

hash.each do |k, v|
  klass_name = k.to_s.camelize
  klass = Class.new(Object) do
    define_method(:initialize) do |param|
      @test = multiply_by(v, param)
    end
  end
  Object.const_set(klass_name, klass)
end

puts Foo.new(1).inspect
puts Bar.new(2).inspect
puts Baz.new(3).inspect
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.