Class: Sinclair::Model

Inherits:
Object show all
Defined in:
lib/sinclair/model.rb,
lib/sinclair/model/builder.rb,
lib/sinclair/model/builder_options.rb

Overview

Basic model to be used when defining new classes quickly

Author:

  • Darthjee

Defined Under Namespace

Classes: Builder, BuilderOptions

Class Method Summary collapse

Class Method Details

.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 (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

  • .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 (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:

  • (Class<Model>)

    a new class with the chosen attributes



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

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

.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, **options)
  Builder.new(self, *attributes, **options).build
end