Module: Refrigerator

Defined in:
lib/refrigerator.rb

Overview

Refrigerator allows for easily freezing core classes and modules.

Constant Summary collapse

CORE_MODULES =

Array of strings containing class or module names.

File.read(filepath.call).
split(/\s+/).
select{|m| eval("defined?(#{m})", nil, __FILE__, __LINE__)}.
each(&:freeze).
reverse.
freeze
OPTS =

Default frozen options hash

{}.freeze

Class Method Summary collapse

Class Method Details

.check_require(file, opts = OPTS) ⇒ Object

Check that requiring a given file does not modify any core classes. Options:

:depends

Require the given files before freezing the core (array of strings)

:modules

Define the given modules in the Object namespace before freezing the core (array of module name symbols)

:classes

Define the given classes in the Object namespace before freezing the core (array of either class name Symbols or array with class Name Symbol and Superclass name string)

:except

Don’t freeze any of the classes modules listed (array of strings)



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

def self.check_require(file, opts=OPTS)
  require 'rubygems'
  Array(opts[:depends]).each{|f| require f}
  Array(opts[:modules]).each{|m| Object.const_set(m, Module.new)}
  Array(opts[:classes]).each{|c, *sc| Object.const_set(c, Class.new(sc.empty? ? Object : eval(sc.first.to_s, nil, __FILE__, __LINE__)))}
  freeze_core(:except=>%w'Gem Gem::Specification Gem::Deprecate'+Array(opts[:exclude]))
  require file
end

.freeze_core(opts = OPTS) ⇒ Object

Freeze core classes and modules. Options:

:except

Don’t freeze any of the classes modules listed (array of strings)



26
27
28
29
# File 'lib/refrigerator.rb', line 26

def self.freeze_core(opts=OPTS)
  (CORE_MODULES - Array(opts[:except])).each{|m| eval(m, nil, __FILE__, __LINE__).freeze}
  nil
end