Skip to content

Latest commit

 

History

History
42 lines (29 loc) · 1.27 KB

File metadata and controls

42 lines (29 loc) · 1.27 KB

Builder

A simple module to create builders classes using the Builder Pattern.

This module was inspired in a builder class to create test objects in a Java/SpringBoot project. You can see a example in this file: AccountBuilder.java

How to use

class AccountBuilder
    include Builder
    build_parameters :name, :email

    def initialize
        @name = "Default name"
        @email = "default@example.com"
    end

    def build
        # setting the values
    end
end

Including the module inside a class you have access to the method build_parameters(*attributes) that will create the methods with_name(name) and with_email(email) so is possible to set new values everytime you use the builder.

All the with_parameter return the instace of the class, so is possible to use method chaining:

account = AccountBuilder.new.with_name("John Doe").build
p account.name # John Doe
p account.email # default@example.com

Inside the build_parameters method all parameters will pass to attr_acessor, creating the get and set methods to them automatically.

Next steps

  • create project
  • create tests and organize