Class: MongoMapperSweatShop::SweatShop

Inherits:
Object
  • Object
show all
Defined in:
lib/mongomapper-sweatshop/sweatshop.rb

Defined Under Namespace

Classes: NoFixtureError

Constant Summary collapse

@@models =

the model to fixture map

{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#modelsHash

Returns Hash of models to scopes and their lazily loaded attributes.

Returns:

  • (Hash)

    Hash of models to scopes and their lazily loaded attributes



8
# File 'lib/mongomapper-sweatshop/sweatshop.rb', line 8

@@models = {}

Class Method Details

.[](klass, name = :default, overrides = {}) ⇒ Mixed

Gets a fixture

Parameters:

  • klass (Class)

    The class to scope the fixture query by

  • name (Symbol) (defaults to: :default)

    the fixture name to scope the query by

  • overrides (Hash) (defaults to: {})

    the overrides that may be provided

Returns:

  • (Mixed)

    the results of the proc, or false



28
29
30
31
32
33
34
# File 'lib/mongomapper-sweatshop/sweatshop.rb', line 28

def [] klass, name=:default, overrides={}
  overrides, name = name, :default if name.is_a? Hash
  @@models[klass][name].last.call.merge(overrides)
rescue NoFixtureError
  puts "A fixture with that name has not been defined, using the default"
  @@models[klass][:default].last.call.merge(overrides)
end

.[]=(klass, name = :default, proc) ⇒ Proc

Stores a fixture

Parameters:

  • klass (Class)

    klass is the class to provide a fixture

  • name (Symbol) (defaults to: :default)

    defines the scope of the fixture, defaults to :default

  • proc (Proc)

    the attribute list to lazily evaled

Returns:

  • (Proc)

    The proc originally passed



19
20
21
# File 'lib/mongomapper-sweatshop/sweatshop.rb', line 19

def []= klass, name=:default, proc
  ((@@models[klass] ||= {})[name] ||= []) << proc
end