Module: SuperModule::SingletonMethodDefinitionStore

Defined in:
lib/super_module/singleton_method_definition_store.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



85
86
87
88
89
# File 'lib/super_module/singleton_method_definition_store.rb', line 85

def self.extended(base)
  base.extend(SuperModule::ModuleBodyMethodCallRecorder) unless base.respond_to?(:__record_method_call)
  base.singleton_method_added(:__method_signature)
  base.singleton_method_added(:__record_method_call)
end

Instance Method Details

#__build_singleton_method_body_source(method_name) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/super_module/singleton_method_definition_store.rb', line 57

def __build_singleton_method_body_source(method_name)
  method_body = self.method(method_name).source
  method_args = __singleton_method_args(method_name, method_body)
  method_body = "def #{method_name}\n#{method_body}\nend" if method_args.nil?
  class_self_method_def_enclosure = "class << self\ndefine_method('#{method_name}') do |#{method_args}|\n#{__singleton_method_call_recorder(method_name, method_args)}\n"
  method_body.sub(__singleton_method_definition_regex(method_name), class_self_method_def_enclosure) + "\nend\n"
end

#__overwrite_singleton_method_from_current_super_module(method_name, method_body) ⇒ Object



70
71
72
73
74
75
# File 'lib/super_module/singleton_method_definition_store.rb', line 70

def __overwrite_singleton_method_from_current_super_module(method_name, method_body)
  if __super_module_having_method(method_name).nil?
    __super_module_singleton_methods_excluded_from_base_definition << method_name
    class_eval(method_body)
  end
end

#__singleton_method_args(method_name, method_body) ⇒ Object



53
54
55
# File 'lib/super_module/singleton_method_definition_store.rb', line 53

def __singleton_method_args(method_name, method_body)
  method_arg_match = method_body.match(__singleton_method_definition_regex(method_name)).to_a[4]
end

#__singleton_method_body(method_name) ⇒ Object



65
66
67
68
# File 'lib/super_module/singleton_method_definition_store.rb', line 65

def __singleton_method_body(method_name)
    super_module_having_method = __super_module_having_method(method_name)
    super_module_having_method ? __singleton_method_body_for(super_module_having_method, method_name) : __build_singleton_method_body_source(method_name) 
end

#__singleton_method_body_for(super_module, method_name) ⇒ Object



37
38
39
# File 'lib/super_module/singleton_method_definition_store.rb', line 37

def __singleton_method_body_for(super_module, method_name)
  super_module.__super_module_singleton_methods.detect {|sm_method_name, sm_method_body| sm_method_name == method_name}[1]
end

#__singleton_method_definition_regex(method_name) ⇒ Object



49
50
51
# File 'lib/super_module/singleton_method_definition_store.rb', line 49

def __singleton_method_definition_regex(method_name)
  /(send)?[ \t(:"']*def(ine_method)?[ \t,:"']+(self\.)?#{method_name}\)?[ \tdo{(|]*([^\n)|;]*)?[ \t)|;]*/m
end

#__super_module_having_method(method_name) ⇒ Object



45
46
47
# File 'lib/super_module/singleton_method_definition_store.rb', line 45

def __super_module_having_method(method_name)
  included_super_modules.detect {|included_super_module| included_super_module.methods.map(&:to_s).include?(method_name.to_s)}
end

#__super_module_singleton_methodsObject



33
34
35
# File 'lib/super_module/singleton_method_definition_store.rb', line 33

def __super_module_singleton_methods
  @__super_module_singleton_methods ||= []
end

#__super_module_singleton_methods_excluded_from_base_definitionObject

excluded list of singleton methods to define (perhaps give a better name)



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/super_module/singleton_method_definition_store.rb', line 7

def __super_module_singleton_methods_excluded_from_base_definition
  @__super_module_singleton_methods_excluded_from_base_definition ||= [
    :__all_module_body_method_calls_in_definition_order,
    :__build_singleton_method_body_source,
    :__define_super_module_singleton_methods,
    :__invoke_module_body_method_calls,
    :__module_body_method_calls,
    :__overwrite_singleton_method_from_current_super_module,
    :__singleton_method_args,
    :__singleton_method_body,
    :__singleton_method_body_for,
    :__singleton_method_call_recorder,
    :__singleton_method_definition_regex,
    :__super_module_having_method,
    :__super_module_singleton_methods,
    :__super_module_singleton_methods_excluded_from_base_definition,
    :__super_module_singleton_methods_excluded_from_call_recording,
    :class_eval,
    :dbg_print, #debugger library friendly exclusion
    :dbg_puts, #debugger library friendly exclusion
    :included, 
    :included_super_modules,
    :singleton_method_added
  ]
end

#included_super_modulesObject



41
42
43
# File 'lib/super_module/singleton_method_definition_store.rb', line 41

def included_super_modules
  included_modules.select {|m| m.include?(SuperModule)}
end

#singleton_method_added(method_name) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/super_module/singleton_method_definition_store.rb', line 77

def singleton_method_added(method_name)
  unless __super_module_singleton_methods_excluded_from_base_definition.include?(method_name)
    method_body = __singleton_method_body(method_name)
    __super_module_singleton_methods << [method_name, method_body] 
    __overwrite_singleton_method_from_current_super_module(method_name, method_body)
  end
end