FluentFixtures::

Collection module

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.

Constants

FIXTURE_FILE_PATTERN

The glob pattern to use when matching files in the search_path.

Attributes

modules R

The Hash of fixture modules declared within this Collection

Public Class Methods

extended( collection_mod )

Extension callback for the collection_mod. Sets up data structures for new +collection_mod+s.

# File lib/fluent_fixtures/collection.rb, line 28
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

Public Instance Methods

add_base_fixture( name, fixture_mod )

Add a global fixture method with the specified name to the top-level Fixtures module.

# File lib/fluent_fixtures/collection.rb, line 99
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 )

Return the default base fixture name based on the name of the given mod (a Module).

# File lib/fluent_fixtures/collection.rb, line 121
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 )

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.

# File lib/fluent_fixtures/collection.rb, line 77
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 )

Declare one or more prefixes to use when searching for fixtures to load for the Collection.

# File lib/fluent_fixtures/collection.rb, line 50
def fixture_path_prefix( new_prefix=nil )
        @fixture_path_prefix = new_prefix if new_prefix
        return @fixture_path_prefix
end
load( *types )

Load fixtures of the specified types.

# File lib/fluent_fixtures/collection.rb, line 57
def load( *types )
        types.each do |type|
                requirename = File.join( self.fixture_path_prefix, type.to_s )
                require( requirename )
        end
end
load_all()

Load all available fixture modules from loaded gems.

# File lib/fluent_fixtures/collection.rb, line 66
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!()

Clear all declared fixtures from the Cozy::FluentFixtures namespace. Mostly used for testing the fixtures system itself.

# File lib/fluent_fixtures/collection.rb, line 130
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