Module: Watchdog

Defined in:
lib/watchdog.rb,
lib/watchdog/error.rb,
lib/watchdog/version.rb,
lib/watchdog/german_shepard.rb

Defined Under Namespace

Modules: GermanShepard Classes: Error, ExtensionMethodExistsError, MethodExistsError

Constant Summary collapse

VERSION =
'0.1.0'

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.extensionsObject

Returns the value of attribute extensions.



7
8
9
# File 'lib/watchdog.rb', line 7

def extensions
  @extensions
end

Class Method Details

.guard(obj, meth) ⇒ Object

Guards extension methods from being overwritten



11
12
13
14
15
# File 'lib/watchdog.rb', line 11

def self.guard(obj, meth)
  if extensions[obj].instance_methods.map(&:to_sym).include?(meth)
    raise ExtensionMethodExistsError.new(meth, obj, extensions[obj])
  end
end

.setup_guard(extension, extended) ⇒ Object



17
18
19
20
# File 'lib/watchdog.rb', line 17

def self.setup_guard(extension, extended)
  extensions[extended] = extension
  extended.extend GermanShepard
end

Instance Method Details

#append_features(mod) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/watchdog.rb', line 22

def append_features(mod)
  Watchdog.setup_guard(self, mod)
  existing = mod.private_instance_methods + mod.instance_methods
  (existing & self.instance_methods).each do |m|
    raise MethodExistsError.new(m, self, mod)
  end
  super
end

#extend_object(obj) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/watchdog.rb', line 31

def extend_object(obj)
  Watchdog.setup_guard(self, obj)
  self.instance_methods.each do |m|
    raise MethodExistsError.new(m, self, obj) if obj.respond_to?(m, true)
  end
  super
end