Class: RDL::Util

Inherits:
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



30
31
32
# File 'lib/rdl/util.rb', line 30

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

.has_singleton_marker(klass) ⇒ Object



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

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)


44
45
46
47
48
49
50
51
52
# File 'lib/rdl/util.rb', line 44

def self.method_defined?(klass, method)
  begin
    (self.to_class klass).method_defined?(method.to_sym) ||
      (self.to_class klass).private_instance_methods.include?(method.to_sym) ||
      (self.to_class klass).protected_instance_methods.include?(method.to_sym)
  rescue NameError
    return false
  end
end

.pp_klass_method(klass, meth) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/rdl/util.rb', line 63

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]



55
56
57
# File 'lib/rdl/util.rb', line 55

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



59
60
61
# File 'lib/rdl/util.rb', line 59

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

.remove_singleton_marker(klass) ⇒ Object



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

def self.remove_singleton_marker(klass)
  if klass =~ /^#{SINGLETON_MARKER_REGEXP}(.*)/
    return $1
  else
    return nil
  end
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