Module: RackConsole::MethodIntrospection

Included in:
AppHelpers
Defined in:
lib/rack_console/method_introspection.rb

Constant Summary collapse

DUMMY_SOURCE_LOCATION =
[ "".freeze, 0 ].freeze

Instance Method Summary collapse

Instance Method Details

#instance_method_names(owner) ⇒ Object



63
64
65
66
67
68
# File 'lib/rack_console/method_introspection.rb', line 63

def instance_method_names owner
  ( owner.instance_methods(false) |
    owner.private_instance_methods(false) |
    owner.protected_instance_methods(false)
    ).sort
end

#match_pred(value, m = nil) ⇒ Object



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

def match_pred value, m = nil
  if value != nil && value != '*' && value != ''
    value = value.send(m) if m
  else
    value = nil
  end
  value
end

#methods_for_module(owner, name_p = nil, kind_p = nil, file_p = nil, seen = { }, to_methods = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/rack_console/method_introspection.rb', line 31

def methods_for_module owner, name_p = nil, kind_p = nil, file_p = nil, seen = { }, to_methods = nil
  methods = to_methods || [ ]
  methods_for_module_by_kind([ :i, :instance_method_names, :instance_method ],
    owner, name_p, kind_p, file_p, seen, methods)
  methods_for_module_by_kind([ :c, :singleton_method_names, :singleton_method ],
    owner, name_p, kind_p, file_p, seen, methods)
  sort_methods! methods unless to_methods
  methods
end

#methods_for_module_by_kind(access, owner, name_p, kind_p, file_p, seen, methods) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rack_console/method_introspection.rb', line 41

def methods_for_module_by_kind access, owner, name_p, kind_p, file_p, seen, methods
  kind, method_names, method_getter = *access
  unless kind_p && kind_p != kind
    send(method_names, owner).each do | name |
      next if name_p && name_p != (name = name.to_sym)
      if meth = (owner.send(method_getter, name) rescue nil) and key = [ owner, kind, name ] and ! seen[key]
        seen[key] = true
        if file_p
          f = meth.source_location and f = f.first
          next if f != file_p
        end
        methods << MockMethod.new(meth, name, kind, owner)
      end
    end
  end
  methods
end

#methods_matching(params) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rack_console/method_introspection.rb', line 5

def methods_matching params
  name_p  = match_pred(params[:name], :to_sym)
  kind_p  = match_pred(params[:kind], :to_sym)
  owner_p = match_pred(params[:owner])
  file_p  = match_pred(params[:file])

  methods = [ ]
  seen = { }
  ObjectSpace.each_object(::Module) do | owner |
    next unless (owner.name rescue nil)
    next if owner_p && owner_p != owner.name
    methods_for_module(owner, name_p, kind_p, file_p, seen, methods)
  end
  sort_methods! methods
  methods
end

#methods_within_file(file) ⇒ Object



74
75
76
77
# File 'lib/rack_console/method_introspection.rb', line 74

def methods_within_file file
  methods = methods_matching(file: file)
  sort_methods_by_source_location! methods
end

#singleton_method_names(owner) ⇒ Object



70
71
72
# File 'lib/rack_console/method_introspection.rb', line 70

def singleton_method_names owner
  owner.singleton_methods(false)
end

#sort_methods!(methods) ⇒ Object



59
60
61
# File 'lib/rack_console/method_introspection.rb', line 59

def sort_methods! methods
  methods.sort_by!{|x| [ x.owner.to_s, x.kind, x.name ]}
end

#sort_methods_by_source_location!(methods) ⇒ Object



79
80
81
# File 'lib/rack_console/method_introspection.rb', line 79

def sort_methods_by_source_location! methods
  methods.sort_by!{|x| x.source_location || DUMMY_SOURCE_LOCATION }
end