Class: Fixturize

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

Constant Summary collapse

METHODS_FOR_INSTRUMENTATION =
[
  :save,
  :insert,
  :remove,
  :update,
  :drop,
  :rename,
]
INSERT_TYPES =
[
  INSTRUMENT_DATABASE = "instrument_database",
  INSTRUMENT_IVARS = "instrument_ivar"
]

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.current_instrumentationObject

Returns the value of attribute current_instrumentation.



22
23
24
# File 'lib/fixturize.rb', line 22

def current_instrumentation
  @current_instrumentation
end

.databaseObject

Returns the value of attribute database.



21
22
23
# File 'lib/fixturize.rb', line 21

def database
  @database
end

.database_versionObject



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

def database_version
  @database_version ||= 0
end

.enabledObject

Returns the value of attribute enabled.



23
24
25
# File 'lib/fixturize.rb', line 23

def enabled
  @enabled
end

Class Method Details

._instrument_database(collection_name, method_name, *args) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/fixturize.rb', line 110

def _instrument_database(collection_name, method_name, *args)
  collection.insert_aliased_from_fixturize({
    :type => INSTRUMENT_DATABASE,
    :name => current_instrumentation,
    :collection_name => collection_name.to_s,
    :method_name => method_name.to_s,
    :args => BSON::Binary.new(Marshal.dump(args))
  })
end

.clear_cache!Object



50
51
52
53
54
55
56
# File 'lib/fixturize.rb', line 50

def clear_cache!
  database.collections.each do |c|
    if c.name =~ /fixturize_/
      c.drop
    end
  end
end

.clear_old_versions!Object



58
59
60
61
62
63
64
# File 'lib/fixturize.rb', line 58

def clear_old_versions!
  database.collections.each do |c|
    if c.name =~ /fixturize_/ && c.name != self.collection_name
      c.drop
    end
  end
end

.collectionObject



42
43
44
45
46
47
48
# File 'lib/fixturize.rb', line 42

def collection
  if !database
    raise "Fixturize is not yet setup!  Make sure the database is set!"
  end

  database.collection(collection_name)
end

.collection_nameObject



38
39
40
# File 'lib/fixturize.rb', line 38

def collection_name
  "fixturize_#{database_version}_"
end

.enabled?Boolean

Returns:

  • (Boolean)


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

def enabled?
  enabled ? true : false
end

.fixturize(name = nil, &block) ⇒ Object

Raises:

  • (LocalJumpError)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/fixturize.rb', line 74

def fixturize(name = nil, &block)
  raise LocalJumpError.new("fixturize requires a block") unless block_given?
  return yield if !enabled?

  if !name && block.respond_to?(:source_location)
    # is this portable?
    name = block.source_location.join(":")
  end

  if !name
    raise "A name must be given to fixturize"
  end

  name = name.to_s
  self.current_instrumentation = name
  db_instrumentations = collection.find({ :name => name, :type => INSTRUMENT_DATABASE }).to_a

  if db_instrumentations.any?
    uninstall!

    db_instrumentations.each do |instrumentation|
      load_data_from(instrumentation)
    end

    ivar_instrumentations = collection.find({ :name => name, :type => INSTRUMENT_IVARS }).to_a

    if ivar_instrumentations.any?
      ivar_instrumentations.each do |instrumentation|
        load_ivars_from(instrumentation, caller_of_block(block))
      end
    end
  else
    safe_install(&block)
  end
end

.refresh!(name = nil) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/fixturize.rb', line 66

def refresh!(name = nil)
  if name
    collection.remove({ :name => name.to_s })
  else
    collection.drop()
  end
end

.reset_version!Object



34
35
36
# File 'lib/fixturize.rb', line 34

def reset_version!
  @database_version = nil
end