Class: Oso::Polar::PolarClass

Inherits:
Object
  • Object
show all
Defined in:
lib/oso/polar/host.rb

Overview

Ruby code reloaders (i.e. the one used by rails) swap out the value of a constant on code changes. Because of this, we can't reliably call `is_a?` on the constant that was passed to `register_class`.

Example (where Foo is a class defined in foo.rb):

> klass = Foo
> Foo.new.is_a? klass
  => true
> ... user changes foo.rb ...
> Foo.new.is_a? klass
  => false

To solve this, when we need to access the class (e.g. during isa), we look it up using const_get, which will always return the up-to-date version of the class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ PolarClass

Returns a new instance of PolarClass.



25
26
27
28
29
30
# File 'lib/oso/polar/host.rb', line 25

def initialize(klass)
  @name = klass.name
  # If the class doesn't have a name, it is anonymous, meaning we should
  # actually store it directly
  @anon_class = klass if klass.name.nil?
end

Instance Attribute Details

#anon_classObject (readonly)

Returns the value of attribute anon_class.



23
24
25
# File 'lib/oso/polar/host.rb', line 23

def anon_class
  @anon_class
end

#nameObject (readonly)

Returns the value of attribute name.



23
24
25
# File 'lib/oso/polar/host.rb', line 23

def name
  @name
end

Instance Method Details

#getObject



32
33
34
35
36
# File 'lib/oso/polar/host.rb', line 32

def get
  return anon_class if anon_class

  Object.const_get(name)
end