Class: Sinclair::MethodDefinition Private
- Includes:
- OptionsParser
- Defined in:
- lib/sinclair/method_definition.rb,
lib/sinclair/method_definition/stringifier.rb,
lib/sinclair/method_definition/block_helper.rb,
lib/sinclair/method_definition/call_definition.rb,
lib/sinclair/method_definition/block_definition.rb,
lib/sinclair/method_definition/parameter_helper.rb,
lib/sinclair/method_definition/parameter_builder.rb,
lib/sinclair/method_definition/string_definition.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Definition of the code or block to be aded as method
Direct Known Subclasses
Defined Under Namespace
Modules: BlockHelper Classes: BlockDefinition, CallDefinition, ParameterBuilder, ParameterHelper, StringDefinition, Stringifier
Constant Summary collapse
- DEFAULT_OPTIONS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Default options of initialization
{ cached: false }.freeze
Instance Attribute Summary collapse
-
#name ⇒ String, Symbol
readonly
private
name of the method.
Attributes included from OptionsParser
Class Method Summary collapse
-
.build_with(builder_class) ⇒ Symbol
private
Defines builder for a definition class.
-
.default_value(method_name, value) ⇒ Symbol
private
Builds a method that will return the same value always.
-
.for(type, *args, **options, &block) ⇒ Sinclair::MethodDefinition
private
creates a definition.
-
.from(name, code = nil, **options, &block) ⇒ Base
private
builds a method definition based on arguments.
Instance Method Summary collapse
-
#build(_klass, _type) ⇒ Symbol
abstract
private
Adds the method to given klass.
-
#initialize(name, **options) ⇒ MethodDefinition
constructor
private
A new instance of MethodDefinition.
Methods included from OptionsParser
Constructor Details
#initialize(name, **options) ⇒ MethodDefinition
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of MethodDefinition.
112 113 114 115 |
# File 'lib/sinclair/method_definition.rb', line 112 def initialize(name, **) @name = name @options = DEFAULT_OPTIONS.merge() end |
Instance Attribute Details
#name ⇒ String, Symbol (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
name of the method
24 25 26 |
# File 'lib/sinclair/method_definition.rb', line 24 def name @name end |
Class Method Details
.build_with(builder_class) ⇒ Symbol
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Defines builder for a definition class
101 102 103 104 105 |
# File 'lib/sinclair/method_definition.rb', line 101 def build_with(builder_class) define_method(:build) do |klass, type| builder_class.build(klass, self, type: type) end end |
.default_value(method_name, value) ⇒ Symbol
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Builds a method that will return the same value always
35 36 37 |
# File 'lib/sinclair/method_definition.rb', line 35 def default_value(method_name, value) define_method(method_name) { value } end |
.for(type, *args, **options, &block) ⇒ Sinclair::MethodDefinition
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
creates a definition
The creation is based on type which will be used to infer which subclass of Sinclair::MethodDefinition to be used
If type is nil
then call is delegated to from which will infer the type from the arguments
71 72 73 74 75 76 |
# File 'lib/sinclair/method_definition.rb', line 71 def for(type, *args, **, &block) return from(*args, **, &block) unless type klass = const_get("#{type}_definition".camelize) klass.new(*args, **, &block) end |
.from(name, code = nil, **options, &block) ⇒ Base
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
builds a method definition based on arguments
when block is given, returns a BlockDefinition and returns a StringDefinition otherwise
52 53 54 55 56 57 58 |
# File 'lib/sinclair/method_definition.rb', line 52 def from(name, code = nil, **, &block) if block BlockDefinition.new(name, **, &block) else StringDefinition.new(name, code, **) end end |
Instance Method Details
#build(_klass, _type) ⇒ Symbol
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds the method to given klass
This should be implemented on child classes
132 133 134 135 |
# File 'lib/sinclair/method_definition.rb', line 132 def build(_klass, _type) raise NotImplementedError, 'Build is implemented in subclasses. ' \ "Use #{self.class}.from to initialize a proper object" end |