Module: DataMapper::Spec

Defined in:
lib/dm-core/spec/setup.rb,
lib/dm-core/spec/lib/spec_helper.rb,
lib/dm-core/spec/lib/adapter_helpers.rb,
lib/dm-core/spec/lib/pending_helpers.rb,
lib/dm-core/spec/lib/collection_helpers.rb

Defined Under Namespace

Modules: Adapters, CollectionHelpers, Helpers, PendingHelpers

Class Method Summary collapse

Class Method Details

.cleanup_modelsObject

global model cleanup



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dm-core/spec/lib/spec_helper.rb', line 15

def self.cleanup_models
  descendants = DataMapper::Model.descendants.to_a

  while model = descendants.shift
    model_name = model.name.to_s.strip

    unless model_name.empty? || model_name[0] == ?#
      parts         = model_name.split('::')
      constant_name = parts.pop.to_sym
      base          = parts.empty? ? Object : DataMapper::Ext::Object.full_const_get(parts.join('::'))

      base.class_eval { remove_const(constant_name) if const_defined?(constant_name) }
    end

    remove_ivars(model)
    model.instance_methods(false).each { |method| model.send(:undef_method, method) }

  end

  DataMapper::Model.descendants.clear
end

.configureObject



24
25
26
27
28
29
30
31
# File 'lib/dm-core/spec/setup.rb', line 24

def configure
  @configured = begin
    setup_logger
    require_plugins
    require_spec_adapter
    true
  end
end

.configured?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/dm-core/spec/setup.rb', line 33

def configured?
  @configured
end

.remove_ivars(object, instance_variables = object.instance_variables) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/dm-core/spec/lib/spec_helper.rb', line 37

def self.remove_ivars(object, instance_variables = object.instance_variables)
  seen  = {}
  stack = instance_variables.map { |var| [ object, var ] }

  while node = stack.pop
    object, ivar = node

    # skip "global" and non-DM objects
    next if object.kind_of?(DataMapper::Logger)                    ||
            object.kind_of?(DataMapper::DescendantSet)             ||
            object.kind_of?(DataMapper::Adapters::AbstractAdapter) ||
            object.class.name[0, 13] == 'DataObjects::'

    # skip classes and modules in the DataMapper namespace
    next if object.kind_of?(Module) &&
            !object.name.nil?       &&
            object.name[0, 12] == 'DataMapper::'

    # skip when the ivar is no longer defined in the object
    next unless object.instance_variable_defined?(ivar)

    value = object.instance_variable_get(ivar)

    # skip descendant sets
    next if value.kind_of?(DataMapper::DescendantSet)

    object.__send__(:remove_instance_variable, ivar) unless object.frozen?

    # skip when the value was seen
    next if seen.key?(value.object_id)
    seen[value.object_id] = true

    stack.concat value.instance_variables.map { |ivar| [ value, ivar ] }
  end
end

.require_pluginsObject



54
55
56
57
58
59
60
61
# File 'lib/dm-core/spec/setup.rb', line 54

def require_plugins
  plugins = ENV['PLUGINS'] || ENV['PLUGIN']
  plugins = plugins.to_s.split(/[,\s]+/)
  unless ENV['ADAPTER'] == 'in_memory'
    plugins.push('dm-migrations')
  end
  plugins.uniq.each { |plugin| require plugin }
end

.require_spec_adapterObject



44
45
46
47
48
49
50
51
52
# File 'lib/dm-core/spec/setup.rb', line 44

def require_spec_adapter
  desired_adapter = ENV['ADAPTER']
  if desired_adapter.nil? || desired_adapter == 'in_memory'
    ENV['ADAPTER_SUPPORTS'] = 'all'
    Adapters.use(Adapters::InMemoryAdapter)
  else
    require "dm-#{desired_adapter}-adapter/spec/setup"
  end
end

.rootObject



8
9
10
# File 'lib/dm-core/spec/setup.rb', line 8

def root
  @root ||= default_root
end

.root=(path) ⇒ Object



12
13
14
# File 'lib/dm-core/spec/setup.rb', line 12

def root=(path)
  @root = Pathname(path)
end

.setup_loggerObject



37
38
39
40
41
42
# File 'lib/dm-core/spec/setup.rb', line 37

def setup_logger
  if log = ENV['LOG']
    logger = DataMapper::Logger.new(log_stream(log), :debug)
    logger.auto_flush = true
  end
end

.spec_adaptersObject



63
64
65
# File 'lib/dm-core/spec/setup.rb', line 63

def spec_adapters
  @spec_adapters ||= {}
end