Class: Surrogate::ApiComparer

Inherits:
Object
  • Object
show all
Defined in:
lib/surrogate/api_comparer.rb

Overview

compares a surrogate to an object

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(actual, surrogate) ⇒ ApiComparer

Returns a new instance of ApiComparer.



11
12
13
14
15
16
# File 'lib/surrogate/api_comparer.rb', line 11

def initialize(actual, surrogate)
  unless surrogate.instance_variable_get(:@hatchery).kind_of?(Hatchery) && surrogate.instance_variable_get(:@hatchling).kind_of?(Hatchling)
    surrogate, actual = actual, surrogate
  end
  self.surrogate, self.actual = surrogate, actual
end

Instance Attribute Details

#actualObject

Returns the value of attribute actual.



9
10
11
# File 'lib/surrogate/api_comparer.rb', line 9

def actual
  @actual
end

#surrogateObject

Returns the value of attribute surrogate.



9
10
11
# File 'lib/surrogate/api_comparer.rb', line 9

def surrogate
  @surrogate
end

Instance Method Details

#actual_methodsObject



22
23
24
# File 'lib/surrogate/api_comparer.rb', line 22

def actual_methods
  @actual_methods ||= PorcReflector.new(actual).methods
end

#class_namesObject

names are only shown for methods on both objects



94
95
96
97
98
99
100
# File 'lib/surrogate/api_comparer.rb', line 94

def class_names
  class_methods_that_should_match.each_with_object Hash.new do |method_name, hash|
    surrogate_name, actual_name = class_parameter_names_for method_name
    next if surrogate_name == actual_name || method_name == :clone # ugh :(
    hash[method_name] = { surrogate: surrogate_name, actual: actual_name }
  end
end

#class_not_on_actualObject



57
58
59
# File 'lib/surrogate/api_comparer.rb', line 57

def class_not_on_actual
  surrogate_methods[:class][:api] - actual_methods[:class][:inherited] - actual_methods[:class][:other]
end

#class_not_on_surrogateObject



52
53
54
55
# File 'lib/surrogate/api_comparer.rb', line 52

def class_not_on_surrogate
  (actual_methods[:class][:inherited] + actual_methods[:class][:other]) -
    (surrogate_methods[:class][:inherited] + surrogate_methods[:class][:api])
end

#class_typesObject

types are only shown for methods on both objects



76
77
78
79
80
81
82
# File 'lib/surrogate/api_comparer.rb', line 76

def class_types
  class_methods_that_should_match.each_with_object Hash.new do |name, hash|
    surrogate_type, actual_type = class_types_for name
    next if surrogate_type == actual_type || name == :clone # ugh :(
    hash[name] = { surrogate: surrogate_type, actual: actual_type }
  end
end

#compareObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/surrogate/api_comparer.rb', line 26

def compare
  @compare ||= {
    instance: {
      not_on_surrogate: instance_not_on_surrogate,
      not_on_actual:    instance_not_on_actual,
      types:            instance_types,
      names:            instance_names,
    },
    class: {
      not_on_surrogate: class_not_on_surrogate,
      not_on_actual:    class_not_on_actual,
      types:            class_types,
      names:            class_names,
    },
  }
end

#instance_namesObject

names are only shown for methods on both objects



103
104
105
106
107
108
109
# File 'lib/surrogate/api_comparer.rb', line 103

def instance_names
  instance_methods_that_should_match.each_with_object Hash.new do |method_name, hash|
    surrogate_name, actual_name = instance_parameter_names_for method_name
    next if surrogate_name == actual_name
    hash[method_name] = { surrogate: surrogate_name, actual: actual_name }
  end
end

#instance_not_on_actualObject



48
49
50
# File 'lib/surrogate/api_comparer.rb', line 48

def instance_not_on_actual
  surrogate_methods[:instance][:api] - actual_methods[:instance][:inherited] - actual_methods[:instance][:other]
end

#instance_not_on_surrogateObject



43
44
45
46
# File 'lib/surrogate/api_comparer.rb', line 43

def instance_not_on_surrogate
  (actual_methods[:instance][:inherited] + actual_methods[:instance][:other]) -
    (surrogate_methods[:instance][:inherited] + surrogate_methods[:instance][:api])
end

#instance_typesObject

types are only shown for methods on both objects



85
86
87
88
89
90
91
# File 'lib/surrogate/api_comparer.rb', line 85

def instance_types
  instance_methods_that_should_match.each_with_object Hash.new do |name, hash|
    surrogate_type, actual_type = instance_types_for name
    next if surrogate_type == actual_type
    hash[name] = { surrogate: surrogate_type, actual: actual_type }
  end
end

#surrogate_methodsObject



18
19
20
# File 'lib/surrogate/api_comparer.rb', line 18

def surrogate_methods
  @surrogate_methods ||= SurrogateClassReflector.new(surrogate).methods
end