Method: Sinclair::Model.for

Defined in:
lib/sinclair/model.rb

.for(*attributes, writter: true, comparable: true) ⇒ Class<Model> .for(*attributes, defaults, writter: true, comparable: true) ⇒ Class<Model>

Deprecated.

Use initialize_with instead

Returns a new class that inherits from model

Overloads:

  • .for(*attributes, writter: true, comparable: true) ⇒ Class<Model>

    Examples:

    A model with readers

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

    Parameters:

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

    • (defaults to: true)

      flag informing if the writter/setter method should be added

    • (defaults to: true)

      flag to make the class Comparable by the fields

  • .for(*attributes, defaults, writter: true, comparable: true) ⇒ Class<Model>

    Examples:

    A model with writters

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

    Parameters:

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

    • attributes to be added with a default value in the initializer

    • (defaults to: true)

      flag informing if the writter/setter method should be added

    • (defaults to: true)

      flag to make the class Comparable by the fields

Returns:

  • a new class with the chosen attributes

API:

  • public



54
55
56
57
58
# File 'lib/sinclair/model.rb', line 54

def for(*attributes, **)
  Class.new(self) do |klass|
    Builder.new(klass, *attributes, **).build
  end
end