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



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

def database_version
  @database_version ||= 0
end

Class Method Details

.clear_cache!Object



45
46
47
48
49
50
51
# File 'lib/fixturize.rb', line 45

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

.collectionObject



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

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

  database.collection(collection_name)
end

.collection_nameObject



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

def collection_name
  "fixturize_#{database_version}_"
end

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



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/fixturize.rb', line 103

def fixturize(name = nil, &block)
  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
  ivar_instrumentations = collection.find({ :name => name, :type => INSTRUMENT_IVARS }).to_a

  if db_instrumentations.any? || ivar_instrumentations.any?
    uninstall!

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

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

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



53
54
55
56
57
58
59
60
61
# File 'lib/fixturize.rb', line 53

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 => YAML.dump(args)
  })
end

.instrument_ivars(ivars, context) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/fixturize.rb', line 63

def instrument_ivars(ivars, context)
  ivars.each do |ivar|
    obj = context.instance_variable_get(ivar)

    # TODO: Use duck typing?
    if defined?(MongoMapper) && obj.kind_of?(MongoMapper::Document)
      collection.insert({
        :type => INSTRUMENT_IVARS,
        :name => current_instrumentation,
        :ivar => ivar,
        :model => obj.class.to_s,
        :id => obj.id
      })
    end
  end
end

.load_data_from(instrumentation) ⇒ Object



80
81
82
83
# File 'lib/fixturize.rb', line 80

def load_data_from(instrumentation)
  collection = database.collection(instrumentation['collection_name'])
  collection.send(instrumentation['method_name'], *YAML.load(instrumentation['args']))
end

.load_ivars_from(instrumentation, target_obj) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/fixturize.rb', line 85

def load_ivars_from(instrumentation, target_obj)
  ivar = instrumentation['ivar']
  model_str = instrumentation['model']
  id = instrumentation['id']

  model = Object.const_get(model_str)
  obj = model.find(id)
  target_obj.instance_variable_set(ivar, obj)
end

.refresh!(name = nil) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/fixturize.rb', line 95

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

.reset_version!Object



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

def reset_version!
  @database_version = nil
end