Class: FixtureDependencies

Inherits:
Object
  • Object
show all
Defined in:
lib/fixture_dependencies.rb

Defined Under Namespace

Classes: SequelTestCase

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.fixture_pathObject

Returns the value of attribute fixture_path.



64
65
66
# File 'lib/fixture_dependencies.rb', line 64

def fixture_path
  @fixture_path
end

.fixturesObject (readonly)

Returns the value of attribute fixtures.



63
64
65
# File 'lib/fixture_dependencies.rb', line 63

def fixtures
  @fixtures
end

.loadedObject (readonly)

Returns the value of attribute loaded.



63
64
65
# File 'lib/fixture_dependencies.rb', line 63

def loaded
  @loaded
end

.verboseObject

Returns the value of attribute verbose.



64
65
66
# File 'lib/fixture_dependencies.rb', line 64

def verbose
  @verbose
end

Class Method Details

.load(*records) ⇒ Object

Load all record arguments into the database. If a single argument is given and it corresponds to a single fixture, return the the model instance corresponding to that fixture. If a single argument if given and it corresponds to a model, return all model instances corresponding to that model. If multiple arguments are given, return a list of model instances (for single fixture arguments) or list of model instances (for model fixture arguments). If no arguments, return the empty list. If any of the arguments is a hash, assume the key specifies the model and the values specify the fixture, and treat it as though individual symbols specifying both model and fixture were given.

Examples:

  • load(:posts) # All post fixtures, not recommended

  • load(:posts, :comments) # All post and comment fixtures, again not recommended

  • load(:post__post1) # Just the post fixture named post1

  • load(:post__post1, :post__post2) # Post fixtures named post1 and post2

  • load(:posts=>[:post1, :post2]) # Post fixtures named post1 and post2

  • load(:post__post1, :comment__comment2) # Post fixture named post1 and comment fixture named comment2

  • load(:post2], :comment__comment2) # Post fixtures named post1 and post2 and comment fixture named comment2

This will load the data from the yaml files for each argument whose model is not already in the fixture hash.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fixture_dependencies.rb', line 31

def self.load(*records)
  ret = records.map do |record|
    if record.is_a?(Hash)
      record.map do |k, vals|
        model = k.to_s.singularize
        vals.map{|v| :"#{model}__#{v}"}
      end
    else
      record
    end
  end.flatten.compact.map do |record| 
    model_name, name = split_name(record)
    if name
      use(record.to_sym)
    else
      model_name = model_name.singularize
      unless loaded[model_name.to_sym]
        puts "loading #{model_name}.yml" if verbose > 0
        load_yaml(model_name) 
      end
      fixtures[model_name.to_sym].keys.map{|name| use(:"#{model_name}__#{name}")}
    end
  end
  ret.length == 1 ? ret[0] : ret
end