Sha256: 258ca0e74073e96e5810d9f2a16f51b657f2af2c5e31be6987413e60abc87096

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

# Override
#
# This is the pure esence of the stubbing concept: it takes an object,
# a hash of methods/results, and proceeds to rewrite each method in the
# object. It can be used as a stubbing strategy in most cases, and I'd
# say that cases that don't fit this pattern have a very bad code smell,
# because are either dealing with internals or with side effects.
#
# Usage
#
#     require 'override'
#
#     @user = User.spawn
#     override(@user, :name => "Foobar", :email => "foobar@example.org")
#     override(User, :find => @user)
#
# Or alternatively:
#
#     override(User, :find => override(User.spawn, :name => "Foobar, :email => "foobar@example.org"))
#
# In case you don't know what spawn means, check my other library for
# testing at http://github.com/soveran/spawner.
#
# Note: the arity strictness in Ruby 1.9 demands for that trick that
# drops the arguments passed to the redefined method. It's not necessary
# in Ruby 1.8.
#
require "rubygems"
require "metaid"

def override object, methods
  methods.each do |method, result|
    object.meta_def(method) { |*_| result }
  end

  object
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
soveran-override-0.0.5 lib/override.rb