Skip to content

lbenedette/builder-ruby

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

A simple module to create builders classes based on Builder Pattern.

Topics

Resources

Stars

Watchers

Forks

Languages