Module: Spoof

Defined in:
lib/spoof.rb,
lib/spoof/version.rb

Overview

Spoof is a tiny mocking script.

Constant Summary collapse

VERSION =
"2.0.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.make(*ancestors) ⇒ Object

Defines a mock class.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 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
  klass
end

Instance Method Details

#attr(name, default_value = nil) ⇒ Object

Creates an attribute getter & setter.

Parameters:

  • name (Symbol)

    The name of the attribute.

  • default_value (Object) (defaults to: nil)

    An optional default value.



24
25
26
27
# File 'lib/spoof.rb', line 24

def attr(name, default_value=nil)
  context.send :attr_accessor, name
  instance_variable_set "@#{name}", default_value
end

#attrs(*names) ⇒ Object

Creates attributes for a list of names.

Parameters:

  • names (Symbol)

    The list of attribute names.



31
32
33
# File 'lib/spoof.rb', line 31

def attrs(*names)
  names.each { |name| attr(name) }
end

#method(name) { ... } ⇒ Object

Creates a method.

Parameters:

  • name (Symbol)

    The name of the method.

Yields:

  • The block that will serve as the method body.



38
39
40
# File 'lib/spoof.rb', line 38

def method(name, &block)
  context.send :define_method, name, &block
end