Class: MethodCall

Inherits:
Object
  • Object
show all
Defined in:
lib/method_call_recorder/method_call.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(meth, *args, &blk) ⇒ MethodCall

Returns a new instance of MethodCall.



4
5
6
7
# File 'lib/method_call_recorder/method_call.rb', line 4

def initialize(meth, *args, &blk)
  @method, @args = meth, args
  @block = blk
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



9
10
11
# File 'lib/method_call_recorder/method_call.rb', line 9

def args
  @args
end

#blockObject (readonly)

Returns the value of attribute block.



9
10
11
# File 'lib/method_call_recorder/method_call.rb', line 9

def block
  @block
end

#methodObject

Returns the value of attribute method.



9
10
11
# File 'lib/method_call_recorder/method_call.rb', line 9

def method
  @method
end

Instance Method Details

#==(other) ⇒ Object



54
55
56
# File 'lib/method_call_recorder/method_call.rb', line 54

def ==(other)
  self.method == other.method && self.args == other.args
end

#call_on(obj) ⇒ Object



11
12
13
# File 'lib/method_call_recorder/method_call.rb', line 11

def call_on(obj)
  obj.send(method, *args)
end

#deep_dupObject



58
59
60
# File 'lib/method_call_recorder/method_call.rb', line 58

def deep_dup
  self.class.new(method, *args, &block)
end

#getter?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/method_call_recorder/method_call.rb', line 23

def getter?
  !setter?
end

#guess_receiver_typeObject



47
48
49
50
51
52
# File 'lib/method_call_recorder/method_call.rb', line 47

def guess_receiver_type
  case type
  when :array_reader, :array_writer then Array
  when :hash_reader,  :hash_writer then Hash
  end
end

#setter?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/method_call_recorder/method_call.rb', line 19

def setter?
  !!(method.to_s =~ /=$/)
end

#to_aObject



15
16
17
# File 'lib/method_call_recorder/method_call.rb', line 15

def to_a
  [method, *args]
end

#to_sObject



62
63
64
# File 'lib/method_call_recorder/method_call.rb', line 62

def to_s
  to_a.to_s
end

#to_setter(value) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/method_call_recorder/method_call.rb', line 27

def to_setter(value)
  new_method_call = self.deep_dup
  if setter?
    new_method_call.args[-1] = value
  else
    new_method_call.args << value
    new_method_call.method = "#{new_method_call.method}=".to_sym
  end
  new_method_call
end

#typeObject



38
39
40
41
42
43
44
45
# File 'lib/method_call_recorder/method_call.rb', line 38

def type
  case method.to_s
  when '[]'  then (args.first.is_a?(Fixnum) ? :array_reader : :hash_reader)
  when '[]=' then (args.first.is_a?(Fixnum) ? :array_writer : :hash_writer)
  when /\=$/ then :attr_writer
  else :attr_reader
  end
end