Class: Fixturize
- Inherits:
-
Object
- Object
- Fixturize
- 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
-
.current_instrumentation ⇒ Object
Returns the value of attribute current_instrumentation.
-
.database ⇒ Object
Returns the value of attribute database.
- .database_version ⇒ Object
-
.enabled ⇒ Object
Returns the value of attribute enabled.
-
.relative_path_root ⇒ Object
Returns the value of attribute relative_path_root.
Class Method Summary collapse
- ._instrument_database(collection_name, method_name, *args) ⇒ Object
- .clear_cache! ⇒ Object
- .clear_old_versions! ⇒ Object
- .collection ⇒ Object
- .collection_name ⇒ Object
- .enabled? ⇒ Boolean
- .fixture_name(name = nil, &block) ⇒ Object
- .fixturize(name = nil, &block) ⇒ Object
- .index! ⇒ Object
- .refresh!(name = nil) ⇒ Object
- .reset_version! ⇒ Object
Class Attribute Details
.current_instrumentation ⇒ Object
Returns the value of attribute current_instrumentation.
24 25 26 |
# File 'lib/fixturize.rb', line 24 def current_instrumentation @current_instrumentation end |
.database ⇒ Object
Returns the value of attribute database.
23 24 25 |
# File 'lib/fixturize.rb', line 23 def database @database end |
.database_version ⇒ Object
33 34 35 |
# File 'lib/fixturize.rb', line 33 def database_version @database_version ||= 0 end |
.enabled ⇒ Object
Returns the value of attribute enabled.
25 26 27 |
# File 'lib/fixturize.rb', line 25 def enabled @enabled end |
.relative_path_root ⇒ Object
Returns the value of attribute relative_path_root.
27 28 29 |
# File 'lib/fixturize.rb', line 27 def relative_path_root @relative_path_root end |
Class Method Details
._instrument_database(collection_name, method_name, *args) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/fixturize.rb', line 147 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)), :timestamp => Time.now.to_f, # unused, just for reference # :json_args => args.to_json, }) end |
.clear_cache! ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/fixturize.rb', line 53 def clear_cache! database.collections.each do |c| if c.name =~ /fixturize_/ c.drop end end end |
.clear_old_versions! ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/fixturize.rb', line 61 def clear_old_versions! return unless enabled? database.collections.select do |c| c.name =~ /fixturize_/ && c.name != self.collection_name end.each do |c| c.drop end end |
.collection ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/fixturize.rb', line 45 def collection if !database raise "Fixturize is not yet setup! Make sure the database is set!" end database.collection(collection_name) end |
.collection_name ⇒ Object
41 42 43 |
# File 'lib/fixturize.rb', line 41 def collection_name "fixturize_#{database_version}_" end |
.enabled? ⇒ Boolean
29 30 31 |
# File 'lib/fixturize.rb', line 29 def enabled? enabled ? true : false end |
.fixture_name(name = nil, &block) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/fixturize.rb', line 91 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(":") if block.respond_to?(:source) name += ":" + Digest::SHA1.hexdigest(block.source.strip) end end if !name raise "A name must be given to fixturize" end name.to_s end |
.fixturize(name = nil, &block) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/fixturize.rb', line 114 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 all_instrumentations = collection. find({ :name => name }). sort({ :_id => Mongo::ASCENDING }). to_a db_instrumentations = all_instrumentations.select { |i| i['type'] == INSTRUMENT_DATABASE } if db_instrumentations.any? uninstall! db_instrumentations.each do |instrumentation| load_data_from(instrumentation) end ivar_instrumentations = all_instrumentations.select { |i| i['type'] == INSTRUMENT_IVARS } 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 |
.index! ⇒ Object
82 83 84 85 86 87 88 89 |
# File 'lib/fixturize.rb', line 82 def index! return unless enabled? collection.ensure_index({ :name => Mongo::ASCENDING, :type => Mongo::ASCENDING, }) end |
.refresh!(name = nil) ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/fixturize.rb', line 71 def refresh!(name = nil) return unless enabled? if name name = fixture_name(name) collection.remove({ :name => name }) else collection.drop() end end |
.reset_version! ⇒ Object
37 38 39 |
# File 'lib/fixturize.rb', line 37 def reset_version! @database_version = nil end |