lib/sinclair/method_definition.rb in sinclair-1.9.0 vs lib/sinclair/method_definition.rb in sinclair-1.10.0
- old
+ new
@@ -9,10 +9,11 @@
include Sinclair::OptionsParser
autoload :BlockHelper, 'sinclair/method_definition/block_helper'
autoload :BlockDefinition, 'sinclair/method_definition/block_definition'
autoload :StringDefinition, 'sinclair/method_definition/string_definition'
+ autoload :CallDefinition, 'sinclair/method_definition/call_definition'
# @method name
#
# name of the method
#
@@ -22,34 +23,49 @@
# Default options of initialization
DEFAULT_OPTIONS = {
cached: false
}.freeze
- # Builds a method that will return the same value always
- #
- # @return [Symbol]
- def self.default_value(method_name, value)
- define_method(method_name) { value }
- end
+ class << self
+ # Builds a method that will return the same value always
+ #
+ # @return [Symbol]
+ def default_value(method_name, value)
+ define_method(method_name) { value }
+ end
- # @param name [String,Symbol] name of the method
- # @param code [String] code to be evaluated as method
- # @param block [Proc] block with code to be added as method
- # @param options [Hash] Options of construction
- # @option options cached [Boolean] Flag telling to create a block
- # with cache
- #
- # builds a method definition based on arguments
- #
- # when block is given, returns a {BlockDefinition} and
- # returns a {StringDefinition} otherwise
- #
- # @return [Base]
- def self.from(name, code = nil, **options, &block)
- if block
- BlockDefinition.new(name, **options, &block)
- else
- StringDefinition.new(name, code, **options)
+ # @param name [String,Symbol] name of the method
+ # @param code [String] code to be evaluated as method
+ # @param block [Proc] block with code to be added as method
+ # @param options [Hash] Options of construction
+ # @option options cached [Boolean] Flag telling to create a block
+ # with cache
+ #
+ # builds a method definition based on arguments
+ #
+ # when block is given, returns a {BlockDefinition} and
+ # returns a {StringDefinition} otherwise
+ #
+ # @return [Base]
+ def from(name, code = nil, **options, &block)
+ if block
+ BlockDefinition.new(name, **options, &block)
+ else
+ StringDefinition.new(name, code, **options)
+ end
+ end
+
+ # creates a definition
+ #
+ # The creation is based on type which will be used to infer
+ # which subclass of {Sinclair::MethodDefinition} to be used
+ #
+ # @param type [Symbol] the method definition type
+ #
+ # @return [Sinclair::MethodDefinition] an instance of a subclass
+ def for(type, *args, **options, &block)
+ klass = const_get("#{type}_definition".camelize)
+ klass.new(*args, **options, &block)
end
end
# @param name [String,Symbol] name of the method
# @param options [Hash] Options of construction