Class: ActiveSupport::Dependencies::ClassCache

Inherits:
Object
  • Object
show all
Defined in:
lib/active_support/dependencies.rb

Instance Method Summary collapse

Constructor Details

#initializeClassCache

Returns a new instance of ClassCache.



540
541
542
# File 'lib/active_support/dependencies.rb', line 540

def initialize
  @store = Hash.new
end

Instance Method Details

#clear!Object



573
574
575
# File 'lib/active_support/dependencies.rb', line 573

def clear!
  @store.clear
end

#empty?Boolean

Returns:

  • (Boolean)


544
545
546
# File 'lib/active_support/dependencies.rb', line 544

def empty?
  @store.empty?
end

#get(key) ⇒ Object Also known as: []



552
553
554
555
# File 'lib/active_support/dependencies.rb', line 552

def get(key)
  key = key.name if key.respond_to?(:name)
  @store[key] ||= Inflector.constantize(key)
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


548
549
550
# File 'lib/active_support/dependencies.rb', line 548

def key?(key)
  @store.key?(key)
end

#safe_get(key) ⇒ Object



558
559
560
561
562
563
564
# File 'lib/active_support/dependencies.rb', line 558

def safe_get(key)
  key = key.name if key.respond_to?(:name)
  @store[key] || begin
    klass = Inflector.safe_constantize(key)
    @store[key] = klass
  end
end

#store(klass) ⇒ Object

Raises:

  • (ArgumentError)


566
567
568
569
570
571
# File 'lib/active_support/dependencies.rb', line 566

def store(klass)
  return self unless klass.respond_to?(:name)
  raise(ArgumentError, 'anonymous classes cannot be cached') if klass.name.empty?
  @store[klass.name] = klass
  self
end