lib/shale/builder.rb in shale-builder-0.1.3 vs lib/shale/builder.rb in shale-builder-0.1.9

- old
+ new

@@ -1,10 +1,13 @@ # frozen_string_literal: true +# typed: true require 'shale' +require 'sorbet-runtime' require_relative 'builder/version' +require_relative 'attribute' module Shale # It's meant to be included in subclasses of `Shale::Mapper` # to provide an easier way of building instances. # @@ -35,64 +38,78 @@ # p.expiration_month = 12 # end # end # module Builder + extend T::Helpers + class << self + extend T::Sig + # Gets called after including this module in a module or class. - # - # @param mod [Module, Class] - # @return [void] + sig { params(mod: Module).void } def included(mod) mod.extend ClassMethods Builder.prepare_mod(mod) end # Prepares the received module or class # for dynamic method definition. - # - # @param mod [Module, Class] - # @return [void] + sig { params(mod: Module).void } def prepare_mod(mod) builder_methods_module = ::Module.new mod.instance_variable_set :@builder_methods_module, builder_methods_module mod.include builder_methods_module end end # Class methods provided by `Shale::Builder` module ClassMethods - # @param subclass [Class] - # @return [void] + extend T::Sig + extend T::Generic + abstract! + has_attached_class! + + sig { params(subclass: Class).void } def inherited(subclass) super Builder.prepare_mod(subclass) end # Contains overridden getter methods for attributes # with complex types (so that they accept a block for building) - # - # @return [Module] + sig { returns(Module) } attr_reader :builder_methods_module - # @return [Class, nil] - attr_accessor :request_class - - # @yieldparam [self] - # @return [self] - def build + sig { params(_block: T.proc.params(arg0: T.attached_class).void).returns(T.attached_class) } + def build(&_block) body = new yield(body) body end - # @param name [String, Symbol] - # @param type [Class] - # @return [void] - def attribute(name, type, *args, collection: false, **kwargs, &block) - super + sig { abstract.params(props: T.anything).returns(T.attached_class) } + def new(**props); end + + sig { abstract.returns(T::Hash[Symbol, Shale::Attribute]) } + def attributes; end + + sig do + params( + name: T.any(String, Symbol), + type: Class, + collection: T::Boolean, + default: T.nilable(Proc), + doc: T.nilable(String), + kwargs: Object, + block: T.nilable(T.proc.void), + ).void + end + def attribute(name, type, collection: false, default: nil, doc: nil, **kwargs, &block) + super(name, type, collection:, default:, **kwargs, &block) + attributes[name.to_sym]&.doc = doc # add doc to the attribute return unless type < ::Shale::Mapper if collection @builder_methods_module.class_eval <<~RUBY, __FILE__, __LINE__ + 1 def #{name} # def clients @@ -118,8 +135,10 @@ end # end RUBY end end + + mixes_in_class_methods(ClassMethods) end end