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



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

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

.relative_path_rootObject

Returns the value of attribute relative_path_root.



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

def relative_path_root
  @relative_path_root
end

Class Method Details

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



122
123
124
125
126
127
128
129
130
# File 'lib/fixturize.rb', line 122

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



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

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

.clear_old_versions!Object



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

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

.collectionObject



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

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

  database.collection(collection_name)
end

.collection_nameObject



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

def collection_name
  "fixturize_#{database_version}_"
end

.enabled?Boolean

Returns:

  • (Boolean)


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

def enabled?
  enabled ? true : false
end

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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/fixturize.rb', line 75

def fixture_name(name = nil, &block)
  if !name && block.respond_to?(:source_location)
    # is this portable?
    file_name, line_number = block.source_location

    if relative_path_root && file_name.start_with?(relative_path_root)
      file_name = file_name[relative_path_root.length + 1 .. -1]
    end

    name = [file_name, line_number].join(":")
  end

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

  name.to_s
end

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

Raises:

  • (LocalJumpError)


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/fixturize.rb', line 94

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

  name = fixture_name(name, &block)

  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



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

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

.reset_version!Object



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

def reset_version!
  @database_version = nil
end