module Spoof

Spoof is a tiny mocking script.

Constants

VERSION

Public Class Methods

make(*ancestors) click to toggle source

Defines a mock class.

# File lib/spoof.rb, line 5
def self.make(*ancestors)
  superclass = ancestors.shift if ancestors.first.is_a?(Class)
  superclass ||= Object
  mixins = ancestors
  klass = Class.new(superclass) do
    mixins.each { |mixin| send :include, mixin }
    def initialize(*args)
      @args = args
      super unless self.class.superclass == Object
    end
  end
  klass.extend Spoof
  klass.send :include, Spoof
  Object.const_set "Spoof#{klass.object_id}", klass
  klass
end
new(*args) click to toggle source
Calls superclass method
# File lib/spoof.rb, line 11
def initialize(*args)
  @args = args
  super unless self.class.superclass == Object
end

Public Instance Methods

attr(name, default_value=nil) click to toggle source

Creates an attribute getter & setter. @param [Symbol] name The name of the attribute. @param [Object] default_value An optional default value.

# File lib/spoof.rb, line 25
def attr(name, default_value=nil)
  context.send :attr_accessor, name
  instance_variable_set "@#{name}", default_value
end
attrs(*names) click to toggle source

Creates attributes for a list of names. @param [Symbol] names The list of attribute names.

# File lib/spoof.rb, line 32
def attrs(*names)
  names.each { |name| attr(name) }
end
method(name, &block) click to toggle source

Creates a method. @param [Symbol] name The name of the method. @yield The block that will serve as the method body.

# File lib/spoof.rb, line 39
def method(name, &block)
  context.send :define_method, name, &block
end

Private Instance Methods

context() click to toggle source
# File lib/spoof.rb, line 45
def context
  @context = class << self; self; end if is_a? Class
  @context ||= self.class
end