Class: Must::Expectation

Inherits:
BasicObject
Defined in:
lib/ruby_ext/more/must.rb

Direct Known Subclasses

NegativeExpectation, PositiveExpectation

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Expectation

Returns a new instance of Expectation.



15
16
17
# File 'lib/ruby_ext/more/must.rb', line 15

def initialize object
  @object = object
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (protected)



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ruby_ext/more/must.rb', line 35

def method_missing method, *args, &block
  original_method = method
  method, prefix, human_method = if method =~ /^be_/
    [:"#{method.to_s[3..-1]}?", :be, :"#{method.to_s[3..-1]}"]
  elsif method =~ /^have_/
    [:"#{method.to_s[5..-1]}?", :have, :"#{method.to_s[5..-1]}"]
  elsif method =~ /^[a-z_0-9]+$/
    [:"#{method}?", nil, method]
  else
    [method, nil, method]
  end
  special_method = :"_#{method}"

  # Defining method, to make it faster for future calls.
  instance_methods = ::Must::Expectation.instance_methods false
  if instance_methods.include? special_method
    define_special_method original_method, special_method, human_method, prefix
  else
    define_method original_method, method, human_method, prefix
  end

  __send__ original_method, *args, &block
end

Instance Method Details

#be(*args) ⇒ Object

Special case, shortcut to write ‘a.must.be b` instead of `a.must.be_equal_to b`.



22
23
24
25
26
27
28
29
30
# File 'lib/ruby_ext/more/must.rb', line 22

def be *args
  if args.size == 1
    be_equal_to args.first
  elsif args.empty?
    self
  else
    raise ArgumentError, "invalid usage!"
  end
end

#to_sObject



19
# File 'lib/ruby_ext/more/must.rb', line 19

def to_s; inspect end