Class: FixtureDependencies

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

Defined Under Namespace

Modules: HelperMethods Classes: SequelTestCase

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.class_mapObject

Returns the value of attribute class_map.



88
89
90
# File 'lib/fixture_dependencies.rb', line 88

def class_map
  @class_map
end

.fixture_pathObject

Returns the value of attribute fixture_path.



88
89
90
# File 'lib/fixture_dependencies.rb', line 88

def fixture_path
  @fixture_path
end

.fixturesObject (readonly)

Returns the value of attribute fixtures.



87
88
89
# File 'lib/fixture_dependencies.rb', line 87

def fixtures
  @fixtures
end

.loadedObject (readonly)

Returns the value of attribute loaded.



87
88
89
# File 'lib/fixture_dependencies.rb', line 87

def loaded
  @loaded
end

.verboseObject

Returns the value of attribute verbose.



88
89
90
# File 'lib/fixture_dependencies.rb', line 88

def verbose
  @verbose
end

Class Method Details

.build(record, attributes = {}) ⇒ Object

Loads the attribute for a single record, merging optional attributes.



45
46
47
48
49
50
51
# File 'lib/fixture_dependencies.rb', line 45

def self.build(record, attributes = {})
  obj = FixtureDependencies.load_attributes([record])

  attributes.each { |key, value| obj.send("#{key}=", value) }

  obj
end

.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.



33
34
35
# File 'lib/fixture_dependencies.rb', line 33

def self.load(*records)
  load_with_options(records)
end

.load_attributes(*records) ⇒ Object

Load the attributes for the record arguments. This method responds to the same interface as ‘load’, the difference being that has_many associations are not loaded.



40
41
42
# File 'lib/fixture_dependencies.rb', line 40

def self.load_attributes(*records)
  load_with_options(records, :attributes_only=>true)
end

.load_with_options(records, opts = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/fixture_dependencies.rb', line 53

def self.load_with_options(records, opts = {})
  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)
    unless class_map[model_name.to_sym].nil?
      record = "#{class_map[model_name.to_sym].to_s.underscore}__#{name}"
    end
    if name
      use(record.to_sym, opts)
    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}", opts)}
    end
  end
  ret.length == 1 ? ret[0] : ret
end