Class: RDL::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/rdl/util.rb

Constant Summary collapse

SINGLETON_MARKER =
"[s]"
SINGLETON_MARKER_REGEXP =
Regexp.escape(SINGLETON_MARKER)
GLOBAL_NAME =

something that’s not a valid class name

"_Globals"

Class Method Summary collapse

Class Method Details

.add_singleton_marker(klass) ⇒ Object



45
46
47
# File 'lib/rdl/util.rb', line 45

def self.add_singleton_marker(klass)
  return SINGLETON_MARKER + klass
end

.has_singleton_marker(klass) ⇒ Object



33
34
35
# File 'lib/rdl/util.rb', line 33

def self.has_singleton_marker(klass)
  return (klass =~ /^#{SINGLETON_MARKER_REGEXP}/)
end

.method_defined?(klass, method) ⇒ Boolean

Duplicate method… Klass should be a string and may have a singleton marker def self.pretty_name(klass, meth)

if klass =~ /^#{SINGLETON_MARKER_REGEXP}(.*)/
  return "#{$1}.#{meth}"
else
  return "#{klass}##{meth}"
end

end

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rdl/util.rb', line 59

def self.method_defined?(klass, method)
  begin
    sk = self.to_class klass
    msym = method.to_sym
    mstr = method.to_s
    sk.instance_method msym
  rescue NameError
    return false
  end
  klass_str = RDL::Util.to_class_str(klass).hash
  if mstr.start_with?('__rdl') and mstr.end_with?('_old_#{klass_str}')
    mstr0 = RDL::Wrap.unwrapped_name(klass, mstr)
    owner0 = sk.instance_method(mstr0).owner
    owner = sk.instance_method(mstr).owner
    return false if owner0 != owner
  end

  true
end

.pp_klass_method(klass, meth) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/rdl/util.rb', line 88

def self.pp_klass_method(klass, meth)
  klass = klass.to_s
  if has_singleton_marker klass
    remove_singleton_marker(klass) + "." + meth.to_s
  else
    klass + "#" + meth.to_s
  end
end

.rdl_type(obj) ⇒ Object

Returns the @__rdl_type field of [obj]



80
81
82
# File 'lib/rdl/util.rb', line 80

def self.rdl_type(obj)
  return (obj.instance_variable_defined?('@__rdl_type') && obj.instance_variable_get('@__rdl_type'))
end

.rdl_type_or_class(obj) ⇒ Object



84
85
86
# File 'lib/rdl/util.rb', line 84

def self.rdl_type_or_class(obj)
  return self.rdl_type(obj) || obj.class
end

.remove_singleton_marker(klass) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/rdl/util.rb', line 37

def self.remove_singleton_marker(klass)
  if klass =~ /^#{SINGLETON_MARKER_REGEXP}(.*)/
    return $1
  else
    return nil
  end
end

.singleton_class_to_class(cls) ⇒ Object



18
19
20
21
22
# File 'lib/rdl/util.rb', line 18

def self.singleton_class_to_class(cls)
  cls_str = cls.to_s
  cls_str = cls_str.split('(')[0] + '>' if cls_str['(']
  to_class cls_str[8..-2]
end

.to_class(klass) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/rdl/util.rb', line 7

def self.to_class(klass)
  return klass if klass.class == Class
  if has_singleton_marker(klass)
    klass = remove_singleton_marker(klass)
    sing = true
  end
  c = klass.to_s.split("::").inject(Object) { |base, name| base.const_get(name) }
  c = c.singleton_class if sing
  return c
end

.to_class_str(cls) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/rdl/util.rb', line 24

def self.to_class_str(cls)
  cls_str = cls.to_s
  if cls_str.start_with? '#<Class:'
    cls_str = cls_str.split('(')[0] + '>' if cls_str['(']
    cls_str = RDL::Util.add_singleton_marker(cls_str[8..-2])
  end
  cls_str
end