fixturize 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +15 -0
- data/lib/fixturize.rb +200 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
YTc3ZTM0NzQ4YzlmYjg4ZDlkZjlmMjI2ZGIxNmE3NzU1MTllMDg2OQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
YTU3NTljYmJlMzI5OTVmYWU2OWIzNzI4YTA0NTRmZmM5MDFhOGE2MQ==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
YTU2MzIwODY3MDRiOTY1YzRhNWU5ODJlNGY0NzJkYjM4ODJmNjM2ZjBkZjEw
|
10
|
+
MWVkMjlkMGYyODgzZWE0Yzg1NjRiNjk0MzM0MjkzM2ZjNjMwMGFhMGZjNGIz
|
11
|
+
NzVkYmQyMDA4ZDJkMzVmYTA1ZTU2YmVmZGU0NWYxMmIyMjU5Zjk=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ODBiODIzOTRkOGEyZDc3M2NiYjI2MTZhMWIyMWMyMjc4MjQ1NmU3YzBmZGEz
|
14
|
+
Yzk3YWFkYWUwMjdiNDlmZmQwY2NiYmRhNjM0YjQyMDc2MjAzNGRmODQ4ODU3
|
15
|
+
NDRlZDlhOTVlMDFjN2U0ODFlMmY0YmFkM2VmOWNjODk2MDk1OGY=
|
data/lib/fixturize.rb
ADDED
@@ -0,0 +1,200 @@
|
|
1
|
+
require 'mongo'
|
2
|
+
require 'yaml'
|
3
|
+
require 'set'
|
4
|
+
|
5
|
+
class Fixturize
|
6
|
+
METHODS_FOR_INSTRUMENTATION = [
|
7
|
+
:save,
|
8
|
+
:insert,
|
9
|
+
:remove,
|
10
|
+
:update,
|
11
|
+
:drop,
|
12
|
+
:rename,
|
13
|
+
]
|
14
|
+
|
15
|
+
class << self
|
16
|
+
attr_accessor :database
|
17
|
+
attr_accessor :current_instrumentation
|
18
|
+
attr_writer :database_version
|
19
|
+
|
20
|
+
def database_version
|
21
|
+
@database_version ||= 0
|
22
|
+
end
|
23
|
+
|
24
|
+
def reset_version!
|
25
|
+
@database_version = nil
|
26
|
+
end
|
27
|
+
|
28
|
+
def collections
|
29
|
+
[
|
30
|
+
db_updates_collection_name,
|
31
|
+
db_saved_ivars_collection_name,
|
32
|
+
]
|
33
|
+
end
|
34
|
+
|
35
|
+
def db_updates_collection_name
|
36
|
+
"mongo_saved_contexts_#{database_version}_"
|
37
|
+
end
|
38
|
+
|
39
|
+
def db_saved_ivars_collection_name
|
40
|
+
"mongo_saved_ivars_#{database_version}_"
|
41
|
+
end
|
42
|
+
|
43
|
+
def saved_contexts_collection
|
44
|
+
if !database
|
45
|
+
raise "Fixturize is not yet setup! Make sure the database is set!"
|
46
|
+
end
|
47
|
+
|
48
|
+
database.collection(db_updates_collection_name)
|
49
|
+
end
|
50
|
+
|
51
|
+
def saved_ivars_collection
|
52
|
+
if !database
|
53
|
+
raise "Fixturize is not yet setup! Make sure the database is set!"
|
54
|
+
end
|
55
|
+
|
56
|
+
database.collection(db_saved_ivars_collection_name)
|
57
|
+
end
|
58
|
+
|
59
|
+
def clear_cache!
|
60
|
+
database.collections.each do |c|
|
61
|
+
if c.name == /mongo_saved_/
|
62
|
+
c.drop
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def instrument_database(collection_name, method_name, *args)
|
68
|
+
saved_contexts_collection.insert_aliased_from_mongo_saved_context({
|
69
|
+
:name => current_instrumentation,
|
70
|
+
:collection_name => collection_name.to_s,
|
71
|
+
:method_name => method_name.to_s,
|
72
|
+
:args => YAML.dump(args)
|
73
|
+
})
|
74
|
+
end
|
75
|
+
|
76
|
+
def instrument_ivars(ivars, context)
|
77
|
+
ivars.each do |ivar|
|
78
|
+
obj = context.instance_variable_get(ivar)
|
79
|
+
|
80
|
+
# TODO: Use duck typing?
|
81
|
+
if defined?(MongoMapper) && obj.kind_of?(MongoMapper::Document)
|
82
|
+
saved_ivars_collection.insert({
|
83
|
+
:name => current_instrumentation,
|
84
|
+
:ivar => ivar,
|
85
|
+
:model => obj.class.to_s,
|
86
|
+
:id => obj.id
|
87
|
+
})
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def load_data_from(instrumentation)
|
93
|
+
collection = database.collection(instrumentation['collection_name'])
|
94
|
+
collection.send(instrumentation['method_name'], *YAML.load(instrumentation['args']))
|
95
|
+
end
|
96
|
+
|
97
|
+
def load_ivars_from(instrumentation, target_obj)
|
98
|
+
ivar = instrumentation['ivar']
|
99
|
+
model_str = instrumentation['model']
|
100
|
+
id = instrumentation['id']
|
101
|
+
|
102
|
+
model = Object.const_get(model_str)
|
103
|
+
obj = model.find(id)
|
104
|
+
target_obj.instance_variable_set(ivar, obj)
|
105
|
+
end
|
106
|
+
|
107
|
+
def refresh!(name = nil)
|
108
|
+
if name
|
109
|
+
saved_contexts_collection.remove({ :name => name.to_s })
|
110
|
+
else
|
111
|
+
saved_contexts_collection.drop()
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def fixturize(name = nil, &block)
|
116
|
+
if !name && block.respond_to?(:source_location)
|
117
|
+
# is this portable?
|
118
|
+
name = block.source_location.join(":")
|
119
|
+
end
|
120
|
+
|
121
|
+
if !name
|
122
|
+
raise "A name must be given to fixturize"
|
123
|
+
end
|
124
|
+
|
125
|
+
name = name.to_s
|
126
|
+
self.current_instrumentation = name
|
127
|
+
db_instrumentations = saved_contexts_collection.find({ :name => name }).to_a
|
128
|
+
ivar_instrumentations = saved_ivars_collection.find({ :name => name }).to_a
|
129
|
+
|
130
|
+
if db_instrumentations.any? || ivar_instrumentations.any?
|
131
|
+
uninstall!
|
132
|
+
|
133
|
+
db_instrumentations.each do |instrumentation|
|
134
|
+
load_data_from(instrumentation)
|
135
|
+
end
|
136
|
+
|
137
|
+
ivar_instrumentations.each do |instrumentation|
|
138
|
+
load_ivars_from(instrumentation, caller_of_block(block))
|
139
|
+
end
|
140
|
+
else
|
141
|
+
safe_install(&block)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
private
|
146
|
+
|
147
|
+
def caller_of_block(block)
|
148
|
+
block.binding.eval("self")
|
149
|
+
end
|
150
|
+
|
151
|
+
def safe_install(&block)
|
152
|
+
install!(&block)
|
153
|
+
ensure
|
154
|
+
self.current_instrumentation = nil
|
155
|
+
uninstall!
|
156
|
+
end
|
157
|
+
|
158
|
+
def install!(&block)
|
159
|
+
METHODS_FOR_INSTRUMENTATION.each do |method_name|
|
160
|
+
Mongo::Collection.class_eval <<-HERE, __FILE__, __LINE__
|
161
|
+
unless instance_methods.include?(:#{method_name}_aliased_from_mongo_saved_context)
|
162
|
+
alias_method :#{method_name}_aliased_from_mongo_saved_context, :#{method_name}
|
163
|
+
|
164
|
+
def #{method_name}(*args, &block)
|
165
|
+
Fixturize.instrument_database(@name, :#{method_name}, *args, &block)
|
166
|
+
#{method_name}_aliased_from_mongo_saved_context(*args, &block)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
HERE
|
170
|
+
end
|
171
|
+
|
172
|
+
block_caller = caller_of_block(block)
|
173
|
+
ivars_before_block = block_caller.instance_variables
|
174
|
+
|
175
|
+
yield.tap do
|
176
|
+
new_ivars = (Set.new(block_caller.instance_variables) - Set.new(ivars_before_block)).to_a
|
177
|
+
instrument_ivars(new_ivars, block_caller)
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
def uninstall!
|
182
|
+
METHODS_FOR_INSTRUMENTATION.each do |method_name|
|
183
|
+
Mongo::Collection.class_eval <<-HERE, __FILE__, __LINE__
|
184
|
+
if instance_methods.include?(:#{method_name}_aliased_from_mongo_saved_context)
|
185
|
+
alias_method :#{method_name}, :#{method_name}_aliased_from_mongo_saved_context
|
186
|
+
remove_method :#{method_name}_aliased_from_mongo_saved_context
|
187
|
+
end
|
188
|
+
HERE
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
def fixturize(*args, &block)
|
195
|
+
Fixturize.fixturize(*args, &block)
|
196
|
+
end
|
197
|
+
|
198
|
+
if defined?(MongoMapper && MongoMapper.database)
|
199
|
+
Fixturize.database = MongoMapper.database
|
200
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fixturize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Scott Taylor
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-06-06 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: fixturize your mongo(mapper) tests inline by caching blocks of created
|
14
|
+
objects
|
15
|
+
email: scott@railsnewbie.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/fixturize.rb
|
21
|
+
homepage: http://github.com/smtlaissezfaire/fixturize
|
22
|
+
licenses:
|
23
|
+
- MIT
|
24
|
+
metadata: {}
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 2.2.2
|
42
|
+
signing_key:
|
43
|
+
specification_version: 4
|
44
|
+
summary: fixturize your mongo tests inline
|
45
|
+
test_files: []
|