lib/sinclair/model.rb in sinclair-1.13.0 vs lib/sinclair/model.rb in sinclair-1.14.0

- old
+ new

@@ -9,10 +9,12 @@ autoload :Builder, 'sinclair/model/builder' class << self # Returns a new class that inherits from model # + # @deprecated Use {.initialize_with} instead + # # @overload for(*attributes, writter: true, comparable: true) # @param attributes [Array<Symbol>] attributes to be added in both the # initialization and adding the methos to the model # @param writter [TrueClass,FalseClass] flag informing if the writter/setter # method should be added @@ -50,9 +52,56 @@ # @return [Class<Model>] a new class with the chosen attributes def for(*attributes, **options) Class.new(self) do |klass| Builder.new(klass, *attributes, **options).build end + end + + # Adds methods needed for the model + # + # The readers/writters, +==+ and initializer are added + # + # @overload initialize_with(*attributes, writter: true, comparable: true) + # @param attributes [Array<Symbol>] attributes to be added in both the + # initialization and adding the methos to the model + # @param writter [TrueClass,FalseClass] flag informing if the writter/setter + # method should be added + # @param comparable [TrueClass,FalseClass] flag to make the class {Comparable} + # by the fields + # + # @example 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 + # + # @overload initialize_with(*attributes, defaults, writter: true, comparable: true) + # @param attributes [Array<Symbol>] attributes to be added in both the + # initialization and adding the methos to the model + # @param defaults [Hash] attributes to be added with a default value in the initializer + # @param writter [TrueClass,FalseClass] flag informing if the writter/setter + # method should be added + # @param comparable [TrueClass,FalseClass] flag to make the class {Comparable} + # by the fields + # + # @example 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 + # + # @return [Array<MethodDefinition>] + def initialize_with(*attributes, **options) + Builder.new(self, *attributes, **options).build end end end end