Class: LawOfDemeterViolator::Violator

Inherits:
Object
  • Object
show all
Defined in:
lib/law_of_demeter_violator/violator.rb

Instance Method Summary collapse

Constructor Details

#initialize(class_to_be_violated) ⇒ Violator

Returns a new instance of Violator.



5
6
7
# File 'lib/law_of_demeter_violator/violator.rb', line 5

def initialize(class_to_be_violated)
  @class_to_be_violated = class_to_be_violated
end

Instance Method Details

#methods_to_violateObject



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

def methods_to_violate
  @class_to_be_violated.instance_methods(false)
end

#violateObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/law_of_demeter_violator/violator.rb', line 9

def violate
  methods_to_violate.each do |original_method_name|
    unviolated_method_name = :"__unviolated_#{original_method_name}"
    @class_to_be_violated.class_eval do
      # First, save the unviolated method.
      alias_method(unviolated_method_name, original_method_name)

      # # Then, remove the unviolated method. It's still saved via the call to
      # alias_method.
      remove_method(original_method_name)

      # Now, define a new method with the same name. It returns an OpenStruct with one key,
      # the original method name. So obj.user is now obj.user.user.
      # Pretty sure |*args| is 1.9-only
      define_method(original_method_name) do |*args|
        original_value = __send__(unviolated_method_name, *args)
        OpenStruct.new(original_method_name => original_value)
      end
    end
  end
end