Sha256: 4aba702f408eee9afe22c121a42d6e90b0553db42fc414a319a33373dc59d695

Contents?: true

Size: 1.53 KB

Versions: 3

Compression:

Stored size: 1.53 KB

Contents

require 'surrogate/argument_errorizer'

class Surrogate

  # A surrogate's `define` keyword results in one of these
  class MethodDefinition
    attr_accessor :name, :options, :default_proc

    def initialize(name, options, default_proc)
      self.name, self.options, self.default_proc = name, options, default_proc
    end

    def has?(name)
      options.has_key? name
    end

    def [](key)
      options[key]
    end

    def to_hash
      options
    end

    def must_match!(args)
      default_proc && errorizer.match!(*args)
    end

    def default(instance, invocation)
      if options.has_key? :default
        options[:default]
      elsif default_proc
        default_proc_as_method_on(instance).call(*invocation.args, &invocation.block)
      else
        raise "You should never get here. Please open a issue at http://github.com/JoshCheek/surrogate"
      end
    end

    private

    def errorizer
      @errorizer ||= ArgumentErrorizer.new name, to_method_definition(default_proc)
    end

    def default_proc_as_method_on(instance)
      unique_name = "surrogate_temp_method_#{Time.now.to_i}_#{rand 10000000}"
      klass = instance.singleton_class
      klass.__send__ :define_method, unique_name, &default_proc
      as_method = klass.instance_method unique_name
      klass.__send__ :remove_method, unique_name
      as_method.bind instance
    end

    def to_method_definition(default_proc)
      object = Object.new
      object.define_singleton_method(:temp_method, &default_proc)
      object.method(:temp_method)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
surrogate-0.8.1 lib/surrogate/method_definition.rb
surrogate-0.8.0 lib/surrogate/method_definition.rb
surrogate-0.7.0 lib/surrogate/method_definition.rb