lib/sinclair.rb in sinclair-1.14.2 vs lib/sinclair.rb in sinclair-1.15.0
- old
+ new
@@ -10,18 +10,18 @@
#
# @example Stand alone usage
# class MyModel
# end
#
-# buider = Sinclair.new(MyModel)
-#
# value = 10
-# builder.add_method(:default_value) { value }
-# builder.add_method(:value, '@value || default_value')
-# builder.add_method(:value=) { |val| @value = val }
-# builder.build
#
+# Sinclair.build(MyModel) do
+# add_method(:default_value) { value }
+# add_method(:value, '@value || default_value')
+# add_method(:value=) { |val| @value = val }
+# end
+#
# instance = MyModel.new
# instance.value # returns 10
# instance.value = 20
# instance.value # returns 20
#
@@ -81,10 +81,11 @@
# server.url # return 'http://server.com'
class Sinclair
require 'sinclair/options_parser'
autoload :VERSION, 'sinclair/version'
+ autoload :Caster, 'sinclair/caster'
autoload :ClassMethods, 'sinclair/class_methods'
autoload :Config, 'sinclair/config'
autoload :ConfigBuilder, 'sinclair/config_builder'
autoload :ConfigClass, 'sinclair/config_class'
autoload :ConfigFactory, 'sinclair/config_factory'
@@ -102,10 +103,12 @@
include OptionsParser
extend ClassMethods
# @method self.build(klass, options = {}, &block)
+ # @api public
+ #
# Runs build using a block for adding the methods
#
# The block is executed adding the methods and after the builder
# runs build building all the methods
#
@@ -133,11 +136,10 @@
# end
#
# builder = Sinclair.new(Purchase)
#
# @example Passing building options (Used on subclasses)
- #
# class MyBuilder < Sinclair
# def add_methods
# if options_object.rescue_error
# add_safe_method
# else
@@ -157,15 +159,14 @@
# end
#
# class MyModel
# end
#
- # builder = MyBuilder.new(MyModel, rescue_error: true)
+ # MyBuilder.build(MyModel, rescue_error: true) do
+ # add_method
+ # end
#
- # builder.add_method
- # builder.build
- #
# instance = MyModel.new
#
# instance.symbolize # returns :default
def initialize(klass, options = {})
@klass = klass
@@ -214,13 +215,13 @@
# @first_name = first_name
# @last_name = last_name
# end
# end
#
- # builder = Sinclair.new(Person)
- # builder.add_method(:full_name, '[first_name, last_name].join(" ")')
- # builder.build
+ # Sinclair.build(Person) do
+ # add_method(:full_name, '[first_name, last_name].join(" ")')
+ # end
#
# Person.new('john', 'wick').full_name # returns 'john wick'
#
# @overload add_method(name, **options, &block)
# @param name [String,Symbol] name of the method to be added
@@ -238,13 +239,13 @@
# @first_name = first_name
# @last_name = last_name
# end
# end
#
- # builder = Sinclair.new(Person)
- # builder.add_method(:bond_name) { "#{last_name}, #{first_name} #{last_name}" }
- # builder.build
+ # Sinclair.build(Person) do
+ # add_method(:bond_name) { "#{last_name}, #{first_name} #{last_name}" }
+ # end
#
# Person.new('john', 'wick').bond_name # returns 'wick, john wick'
#
# @overload add_method(*args, type:, **options, &block)
# @param args [Array<Object>] arguments to be passed to the definition
@@ -265,14 +266,14 @@
# @first_name = first_name
# @last_name = last_name
# end
# end
#
- # builder = Sinclair.new(Person)
- # builder.add_method(:bond_name, type: :block, cached: true) do
- # "{last_name}, #{first_name} #{last_name}"
+ # Sinclair.build(Person) do
+ # add_method(:bond_name, type: :block, cached: true) do
+ # "{last_name}, #{first_name} #{last_name}"
+ # end
# end
- # builder.build
#
# person.Person.new('john', 'wick')
#
# person.bond_name # returns 'wick, john wick'
# person.first_name = 'Johny'