Sha256: 719800b77eba75ae8d6537195ac6af7d3632edffb1571298f706b9a970325bf3

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 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.metaclass.send(:define_method, method) do |*_|
      result
    end
  end
  object
end

Version data entries

1 entries across 1 versions & 1 rubygems

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