class Scribble::Method
Attributes
method_name[R]
receiver_class[R]
signature[R]
Public Class Methods
block?()
click to toggle source
# File lib/scribble/method.rb, line 72 def block?; false; end
eql?(other)
click to toggle source
Insert method into a registry
# 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)
click to toggle source
Subclass, setup and insert into registry
# 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)
click to toggle source
# 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()
click to toggle source
# 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()
click to toggle source
Arity
# 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
new(receiver, call, context)
click to toggle source
# File lib/scribble/method.rb, line 3 def initialize receiver, call, context @receiver, @call, @context = receiver, call, context end
register(method_name, *signature, on: Template::Context)
click to toggle source
Setup method and insert into default registry
# 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)
click to toggle source
Setup instance variables
# 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?()
click to toggle source
Default split and block
# File lib/scribble/method.rb, line 71 def split?; false; end