Method: Sinclair::Model.initialize_with

Defined in:
lib/sinclair/model.rb

.initialize_with(*attributes, writter: true, comparable: true) ⇒ Array<MethodDefinition> .initialize_with(*attributes, defaults, writter: true, comparable: true) ⇒ Array<MethodDefinition>

Adds methods needed for the model

The readers/writters, == and initializer are added

Overloads:

  • .initialize_with(*attributes, writter: true, comparable: true) ⇒ Array<MethodDefinition>

    Examples:

    A model with readers

    class Car < Sinclair::Model
      initialize_with(:brand, :model, writter: false)
    end
    
    car = Car.new(brand: :ford, model: :T)
    
    car.brand # returns :ford
    car.model # returns :T

    Parameters:

    • attributes (Array<Symbol>)

      attributes to be added in both the initialization and adding the methos to the model

    • writter (TrueClass, FalseClass) (defaults to: true)

      flag informing if the writter/setter method should be added

    • comparable (TrueClass, FalseClass) (defaults to: true)

      flag to make the class Comparable by the fields

  • .initialize_with(*attributes, defaults, writter: true, comparable: true) ⇒ Array<MethodDefinition>

    Examples:

    A model with writters

    class Job < Sinclair::Model
      initialize_with({ state: :starting }, writter: true)
    end
    
    job = Job.new
    
    job.state # returns :starting
    job.state = :done
    job.state # returns :done

    Parameters:

    • attributes (Array<Symbol>)

      attributes to be added in both the initialization and adding the methos to the model

    • defaults (Hash)

      attributes to be added with a default value in the initializer

    • writter (TrueClass, FalseClass) (defaults to: true)

      flag informing if the writter/setter method should be added

    • comparable (TrueClass, FalseClass) (defaults to: true)

      flag to make the class Comparable by the fields

Returns:



103
104
105
# File 'lib/sinclair/model.rb', line 103

def initialize_with(*attributes, **)
  Builder.new(self, *attributes, **).build
end