Module: FluentFixtures::Collection
- Extended by:
- Loggability
- Defined in:
- lib/fluent_fixtures/collection.rb
Overview
The extension module for declaring a collection of fixtures. It has three responsibilities: it makes the Module an extension itself for setting up individual fixture modules, it adds the methods for loading fixtures by name (or loading them all), and it adds data structures to the extended module so it can serve as a repository for loaded fixtures.
Constant Summary collapse
- FIXTURE_FILE_PATTERN =
The glob pattern to use when matching files in the search_path.
'*.rb'
Instance Attribute Summary collapse
-
#modules ⇒ Object
readonly
The Hash of fixture modules declared within this Collection.
Class Method Summary collapse
-
.extended(collection_mod) ⇒ Object
Extension callback for the
collection_mod
.
Instance Method Summary collapse
-
#add_base_fixture(name, fixture_mod) ⇒ Object
Add a global fixture method with the specified
name
to the top-level Fixtures module. -
#default_base_fixture_name(mod) ⇒ Object
Return the default base fixture name based on the name of the given
mod
(a Module). -
#extended(fixture_mod) ⇒ Object
Extension callback – add some stuff to every fixture module.
-
#fixture_path_prefix(new_prefix = nil) ⇒ Object
Declare one or more
prefixes
to use when searching for fixtures to load for the Collection. -
#load(*types) ⇒ Object
Load fixtures of the specified
types
. -
#load_all ⇒ Object
Load all available fixture modules from loaded gems.
-
#reset! ⇒ Object
Clear all declared fixtures from the Cozy::FluentFixtures namespace.
Instance Attribute Details
#modules ⇒ Object (readonly)
The Hash of fixture modules declared within this Collection
46 47 48 |
# File 'lib/fluent_fixtures/collection.rb', line 46 def modules @modules end |
Class Method Details
.extended(collection_mod) ⇒ Object
Extension callback for the collection_mod
. Sets up data structures for new collection_mods.
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/fluent_fixtures/collection.rb', line 29 def self::extended( collection_mod ) super prefix = collection_mod.name || nil prefix &&= prefix.sub( /::.*\z/, '' ).downcase default_fixture_path_prefix = File.join( *[prefix, 'fixtures'].compact ) collection_mod.extend( Loggability ) collection_mod.log_to( :fluent_fixtures ) collection_mod.instance_variable_set( :@modules, {} ) collection_mod.instance_variable_set( :@fixture_path_prefix, default_fixture_path_prefix ) end |
Instance Method Details
#add_base_fixture(name, fixture_mod) ⇒ Object
Add a global fixture method with the specified name
to the top-level Fixtures module.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/fluent_fixtures/collection.rb', line 100 def add_base_fixture( name, fixture_mod ) self.log.debug "Adding a base fixture to %p: %p as %p" % [ self, fixture_mod, name ] if previous_name = self.modules.key( fixture_mod ) self.modules.delete( previous_name ) # ugh, no remove_singleton_method self.singleton_class.instance_exec( previous_name ) do |methodname| remove_method( methodname ) end end if self.modules.key?( name ) raise ScriptError, "Already have a base fixture called %s: %p" % [ name, self.modules[name] ] end self.modules[ name ] = fixture_mod define_singleton_method( name, &fixture_mod.method(:factory) ) end |
#default_base_fixture_name(mod) ⇒ Object
Return the default base fixture name based on the name of the given mod
(a Module).
122 123 124 125 126 |
# File 'lib/fluent_fixtures/collection.rb', line 122 def default_base_fixture_name( mod ) modname = mod.name or return nil name = Inflecto.singularize( Inflecto.demodulize(modname).downcase ) return name.to_sym end |
#extended(fixture_mod) ⇒ Object
Extension callback – add some stuff to every fixture module. Note that this is called by fixtures which extend a collection module, not by the collection itself.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/fluent_fixtures/collection.rb', line 78 def extended( fixture_mod ) super fixture_mod.extend( Loggability ) fixture_mod.log_to( :fluent_fixtures ) fixture_mod.extend( FluentFixtures::DSL ) fixture_mod.instance_variable_set( :@decorators, {} ) fixture_mod.instance_variable_set( :@decorator_options, {} ) fixture_mod.instance_variable_set( :@fixtured_class, nil ) fixture_mod.instance_variable_set( :@base_fixture, nil ) fixture_mod.collection = self if default_name = self.default_base_fixture_name( fixture_mod ) self.add_base_fixture( default_name, fixture_mod ) end end |
#fixture_path_prefix(new_prefix = nil) ⇒ Object
Declare one or more prefixes
to use when searching for fixtures to load for the Collection.
51 52 53 54 |
# File 'lib/fluent_fixtures/collection.rb', line 51 def fixture_path_prefix( new_prefix=nil ) @fixture_path_prefix = new_prefix if new_prefix return @fixture_path_prefix end |
#load(*types) ⇒ Object
Load fixtures of the specified types
.
58 59 60 61 62 63 |
# File 'lib/fluent_fixtures/collection.rb', line 58 def load( *types ) types.each do |type| requirename = File.join( self.fixture_path_prefix, type.to_s ) require( requirename ) end end |
#load_all ⇒ Object
Load all available fixture modules from loaded gems.
67 68 69 70 71 72 73 |
# File 'lib/fluent_fixtures/collection.rb', line 67 def load_all pattern = File.join( self.fixture_path_prefix, FIXTURE_FILE_PATTERN ) Gem.find_files( pattern ).each do |fixture_path| fixture_name = File.basename( fixture_path, '.rb' ) self.load( fixture_name ) end end |
#reset! ⇒ Object
Clear all declared fixtures from the Cozy::FluentFixtures namespace. Mostly used for testing the fixtures system itself.
131 132 133 134 135 136 137 138 139 |
# File 'lib/fluent_fixtures/collection.rb', line 131 def reset! self.modules.each do |name, mod| self.log.warn "Removing base fixture method for %p: %p" % [ mod, name ] self.singleton_class.instance_exec( name ) do |methodname| remove_method( methodname ) if method_defined?( methodname ) end end self.modules.clear end |