Class: Scribble::Method
- Inherits:
-
Object
- Object
- Scribble::Method
- Defined in:
- lib/scribble/method.rb
Direct Known Subclasses
Class Attribute Summary collapse
-
.method_name ⇒ Object
readonly
Returns the value of attribute method_name.
-
.receiver_class ⇒ Object
readonly
Returns the value of attribute receiver_class.
-
.signature ⇒ Object
readonly
Returns the value of attribute signature.
Class Method Summary collapse
- .block? ⇒ Boolean
-
.eql?(other) ⇒ Boolean
Insert method into a registry.
-
.implement(receiver_class, method_name, signature, registry, as: nil, to: nil, cast: nil, returns: nil, split: nil) ⇒ Object
Subclass, setup and insert into registry.
- .insert(registry) ⇒ Object
- .max_arity ⇒ Object
-
.min_arity ⇒ Object
Arity.
-
.register(method_name, *signature, on: Template::Context) ⇒ Object
Setup method and insert into default registry.
-
.setup(receiver_class, method_name, signature) ⇒ Object
Setup instance variables.
-
.split? ⇒ Boolean
Default split and block.
Instance Method Summary collapse
-
#initialize(receiver, call, context) ⇒ Method
constructor
A new instance of Method.
Constructor Details
#initialize(receiver, call, context) ⇒ Method
Returns a new instance of Method.
3 4 5 |
# File 'lib/scribble/method.rb', line 3 def initialize receiver, call, context @receiver, @call, @context = receiver, call, context end |
Class Attribute Details
.method_name ⇒ Object (readonly)
Returns the value of attribute method_name.
8 9 10 |
# File 'lib/scribble/method.rb', line 8 def method_name @method_name end |
.receiver_class ⇒ Object (readonly)
Returns the value of attribute receiver_class.
8 9 10 |
# File 'lib/scribble/method.rb', line 8 def receiver_class @receiver_class end |
.signature ⇒ Object (readonly)
Returns the value of attribute signature.
8 9 10 |
# File 'lib/scribble/method.rb', line 8 def signature @signature end |
Class Method Details
.block? ⇒ Boolean
72 |
# File 'lib/scribble/method.rb', line 72 def block?; false; end |
.eql?(other) ⇒ Boolean
Insert method into a registry
19 20 21 |
# File 'lib/scribble/method.rb', line 19 def eql? other receiver_class == other.receiver_class && method_name == other.method_name && signature == other.signature end |
.implement(receiver_class, method_name, signature, registry, as: nil, to: nil, cast: nil, returns: nil, split: nil) ⇒ Object
Subclass, setup and insert into registry
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/scribble/method.rb', line 39 def implement receiver_class, method_name, signature, registry, as: nil, to: nil, cast: nil, returns: nil, split: nil Class.new(self) do setup receiver_class, method_name, signature raise "Received multiple implementation options for method" unless [as, to, cast, returns].compact.size <= 1 raise "Method option :as requires String, got #{as.inspect}" unless as.nil? || as.is_a?(String) raise "Method option :to requires Proc, got #{to.inspect}" unless to.nil? || to.is_a?(Proc) raise "Method option :cast must be 'to_boolean' or 'to_string'" unless [nil, 'to_boolean', 'to_string'].include? cast raise "Method option :split must be true" unless [nil, true].include? split # Redefine split? define_singleton_method :split? do true end if split # Implement method send :define_method, method_name do |*args| if to @receiver.instance_exec *args, &to elsif cast registry.evaluate method_name, registry.send(cast, @receiver), args, @call, @context elsif !returns.nil? returns else @receiver.send (as || method_name), *args end end end.insert registry end |
.insert(registry) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/scribble/method.rb', line 23 def insert registry raise "Duplicate method #{method_name} on #{receiver_class}" if registry.methods.any? { |method| eql? method } raise "Method #{method_name} must be a #{'non-' if split?}split method" unless [nil, split?].include? registry.split?(method_name) raise "Method #{method_name} must be a #{'non-' if block?}block method" unless [nil, block?].include? registry.block?(method_name) registry.methods << self end |
.max_arity ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/scribble/method.rb', line 82 def max_arity @max_arity ||= signature.reduce 0 do |max_arity, element| if max_arity element.is_a?(Array) ? element[1] && max_arity + element[1] : max_arity + 1 end end end |
.min_arity ⇒ Object
Arity
76 77 78 79 80 |
# File 'lib/scribble/method.rb', line 76 def min_arity @min_arity ||= signature.reduce 0 do |min_arity, element| element.is_a?(Array) ? min_arity : min_arity + 1 end end |
.register(method_name, *signature, on: Template::Context) ⇒ Object
Setup method and insert into default registry
32 33 34 35 |
# File 'lib/scribble/method.rb', line 32 def register method_name, *signature, on: Template::Context setup on, method_name, signature insert Registry.instance end |
.setup(receiver_class, method_name, signature) ⇒ Object
Setup instance variables
12 13 14 15 |
# File 'lib/scribble/method.rb', line 12 def setup receiver_class, method_name, signature raise "Method name needs to be a Symbol, got #{method_name.inspect}" unless method_name.is_a?(Symbol) @receiver_class, @method_name, @signature = receiver_class, method_name, signature end |
.split? ⇒ Boolean
Default split and block
71 |
# File 'lib/scribble/method.rb', line 71 def split?; false; end |