Module: GetText::ClassInfo

Extended by:
ClassInfo
Includes:
Locale::Util::Memoizable
Included in:
ClassInfo
Defined in:
lib/gettext/runtime/class_info.rb

Overview

For normalize/finding the related classes/modules. This is used for realizing the scope of TextDomain. (see: www.yotabanana.com/hiki/ruby-gettext-scope.html)

Instance Method Summary collapse

Instance Method Details

#normalize_class(klass) ⇒ Object

normalize the class name klass should kind of the class, not object.



15
16
17
18
19
20
21
# File 'lib/gettext/runtime/class_info.rb', line 15

def normalize_class(klass)
  ret = (klass.kind_of? Module) ? klass : klass.class
  if ret.name =~ /^\#<|^$/ or ret == GetText or ret.name.nil?
    ret = Object
  end
  ret
end

Returns the classes which related to klass (klass’s ancestors, included modules and nested modules)



60
61
62
63
64
65
66
# File 'lib/gettext/runtime/class_info.rb', line 60

def related_classes(klass, all_classes = [])
  ret = related_classes_internal(klass, all_classes)
  unless ret.include? Object
    ret += [Object]
  end
  ret
end

Internal method for related_classes.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/gettext/runtime/class_info.rb', line 29

def related_classes_internal(klass, all_classes = [], analyzed_classes = [] )
  ret = []
  klass = normalize_class(klass)

  return [Object] if root_ancestors.include? klass

  ary = klass.name.split(/::/)
  while(v = ary.shift)
    ret.unshift(((ret.size == 0) ? Object.const_get(v) : ret[0].const_get(v)))
  end
  ret -= analyzed_classes
  if ret.size > 1
    ret += related_classes_internal(ret[1], all_classes, analyzed_classes)
    ret.uniq!
  end
  analyzed_classes << klass unless analyzed_classes.include? klass

  klass.ancestors[1..-1].each do |a|
    ret += related_classes_internal(a, all_classes, analyzed_classes)
    ret.uniq!
  end

  if all_classes.size > 0
    (ret & all_classes).uniq
  else
    ret.uniq
  end
end

#root_ancestorsObject

:nodoc:



23
24
25
# File 'lib/gettext/runtime/class_info.rb', line 23

def root_ancestors  # :nodoc:
  Object.ancestors
end