lib/sinclair.rb in sinclair-1.1.2 vs lib/sinclair.rb in sinclair-1.1.3
- old
+ new
@@ -1,14 +1,16 @@
# frozen_string_literal: true
require 'active_support'
require 'active_support/all'
+# @api public
+# @author darthjee
+#
# Builder that add instance methods to a class
#
# @example
-#
# class MyModel
# end
#
# buider = Sinclair.new(MyModel)
#
@@ -25,14 +27,15 @@
class Sinclair
require 'sinclair/options_parser'
autoload :VERSION, 'sinclair/version'
autoload :MethodDefinition, 'sinclair/method_definition'
- autoload :Matchers, 'sinclair/matchers'
include OptionsParser
+ # Returns a new instance of Sinclair
+ #
# @param klass [Class] to receive the methods
# @param options [Hash] open hash options to be used by builders inheriting from Sinclair
# through the Sinclair::OptionsParser concern
#
# @example
@@ -68,55 +71,56 @@
# MyModel.new.respond_to(:default_value) # returns false
#
# builder.build
#
# MyModel.new.respond_to(:default_value) # returns true
+ #
+ # @return [Array<MethodDefinition>]
def build
definitions.each do |definition|
definition.build(klass)
end
end
# add a method to the method list to be created on klass
#
# @overload add_method(name, code)
- # @param name [String/Symbol] name of the method to be added
+ # @param name [String,Symbol] name of the method to be added
# @param code [String] code to be evaluated when the method is ran
#
- # @example
- # class Person
- # attr_reader :first_name, :last_name
+ # @example Using string code
+ # class Person
+ # attr_reader :first_name, :last_name
#
- # def initialize(first_name, last_name)
- # @first_name = first_name
- # @last_name = last_name
- # end
+ # def initialize(first_name, last_name)
+ # @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
+ # builder = Sinclair.new(Person)
+ # builder.add_method(:full_name, '[first_name, last_name].join(" ")')
+ # builder.build
#
- # Person.new('john', 'wick').full_name # returns 'john wick'
+ # Person.new('john', 'wick').full_name # returns 'john wick'
#
# @overload add_method(name, &block)
- # @param name [String/Symbol] name of the method to be added
+ # @param name [String,Symbol] name of the method to be added
# @param block [Proc] block to be ran as method
#
- # @example
+ # @example Using block
+ # builder = Sinclair.new(Person)
+ # builder.add_method(:bond_name) { "#{last_name}, #{full_name}" }
+ # builder.build
#
- # builder = Sinclair.new(Person)
- # builder.add_method(:bond_name) { "#{last_name}, #{full_name}" }
- # builder.build
- #
- # Person.new('john', 'wick').bond_name # returns 'wick, john wick'
+ # Person.new('john', 'wick').bond_name # returns 'wick, john wick'
+ # @return [Array<MethodDefinition>]
def add_method(name, code = nil, &block)
definitions << MethodDefinition.new(name, code, &block)
end
- # evaluetes a block which will result in a [String] to be
- # then used as code for the method
+ # Evaluetes a block which will result in a String, the method code
#
# @example
#
# module InitialValuer
# extend ActiveSupport::Concern
@@ -181,17 +185,27 @@
# class Purchase
# attr_reader :value, :quantity
# end
#
# Purchase.new(2.3, 5).total_price # returns 11.5
+ # @return [Array<MethodDefinition>]
def eval_and_add_method(name, &block)
add_method(name, instance_eval(&block))
end
private
+ # @api private
+ # @private
attr_reader :klass
+ # @private
+ #
+ # @api private
+ #
+ # List of mthod definitions
+ #
+ # @return [Array<MethodDefinition>]
def definitions
@definitions ||= []
end
end