Module: MethodCop

Defined in:
lib/method_cop.rb,
lib/method_cop/version.rb

Constant Summary collapse

VERSION =
"0.0.1"
@@guards =

TODO: DRY this up

{}
@@class_guards =
{}

Instance Method Summary collapse

Instance Method Details

#guard_class_method(name, options) ⇒ Object



38
39
40
# File 'lib/method_cop.rb', line 38

def guard_class_method(name, options)
  @@class_guards[name] = options
end

#guard_class_method_internal(name, options) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/method_cop.rb', line 50

def guard_class_method_internal(name, options)
  @@method_to_guard_name = name
  @@method_to_guard_options = options
  # there HAS to be a better way. need to look into it.
  class << self
    name = @@method_to_guard_name
    options = @@method_to_guard_options
    @@method_to_guard_name = nil
    @@method_to_guard_options = nil
    method = name.to_sym
    original_method = "#{name}_orig".to_sym
    alias_method  original_method, method
    define_method(method) do |*args|
      unless options[:with].nil?
        unless !self.send(options[:with].to_sym)
          self.send(original_method, *args) 
        else
          # THIS METHOD IS UNDER ARREST
          nil
        end
      else
        # no method to guard with was given
        self.send(original_method, *args)
      end
    end
  end
end

#guard_method(name, options) ⇒ Object



6
7
8
# File 'lib/method_cop.rb', line 6

def guard_method(name, options)
  @@guards[name] = options
end

#guard_method_internal(name, options) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/method_cop.rb', line 18

def guard_method_internal(name, options)
  method = name.to_sym
  original_method = "#{name}_orig".to_sym
  alias_method  original_method, method
  define_method(method) do |*args|
    unless options[:with].nil?
      unless !self.send(options[:with].to_sym)
        self.send(original_method, *args) 
      else
        # THIS METHOD IS UNDER ARREST
        nil
      end
    else
      # no method to guard with was given
      self.send(original_method, *args)
    end
  end
end

#method_added(name) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/method_cop.rb', line 10

def method_added(name)
  unless @@guards[name].nil?
    options = @@guards[name]
    @@guards = @@guards.delete(name)
    guard_method_internal(name, options)
  end
end

#singleton_method_added(name) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/method_cop.rb', line 42

def singleton_method_added(name)
  unless @@class_guards[name].nil?
    options = @@class_guards[name]
    @@class_guards = @@class_guards.delete(name)
    guard_class_method_internal(name, options)
  end
end