Module: Spec::Api::UnderscoreSugar

Included in:
Module
Defined in:
lib/spec/api/sugar.rb

Overview

This module adds syntactic sugar that allows usage of should_* instead of should.*

Defined Under Namespace

Modules: SugarizeForRspec

Instance Method Summary collapse

Instance Method Details

#sugarize_for_rspec!Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/spec/api/sugar.rb', line 7

def sugarize_for_rspec!
  original_method_missing = instance_method(:method_missing)
  class_eval do
    include SugarizeForRspec # This is meant to add a signature to the object that sugarization occurred.
    def method_missing(sym, *args, &block)
      _method_missing(sym, args, block)
    end

    define_method :_method_missing do |sym, args, block|
      return original_method_missing.bind(self).call(sym, *args, &block) unless __is_sweetened?(sym)

      object = self
      calls = sym.to_s.split("_")
      while calls.length > 1
        remainder = calls.join("_")
        break if (object.respond_to?(remainder))
        call = calls.shift
        object = object.__send__(call)
        break if call == "be"
      end
      return object.__send__(calls.join("_"), *args, &block)
    end

    def __is_sweetened?(sym) #:nodoc:
      return true if sym.to_s =~ /^should_/
    end
  end
end