Class: FixtureRecord::Data

Inherits:
Hash
  • Object
show all
Defined in:
lib/fixture_record/data.rb

Instance Method Summary collapse

Constructor Details

#initializeData

Returns a new instance of Data.



3
4
5
6
# File 'lib/fixture_record/data.rb', line 3

def initialize(...)
  super(...)
  self.default_proc = Proc.new { |hash, klass| hash[klass] = load_fixture_for(klass) }
end

Instance Method Details

#fixture_path_for(klass) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/fixture_record/data.rb', line 23

def fixture_path_for(klass)
  if FixtureRecord.base_path.is_a?(String)
    [FixtureRecord.base_path, klass.table_name + '.yml'].join('/')
  else
    FixtureRecord.base_path.join(klass.table_name + '.yml')
  end
end

#load_fixture_for(klass) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/fixture_record/data.rb', line 8

def load_fixture_for(klass)
  if File.exist?(fixture_path_for(klass))
    YAML.load_file(fixture_path_for(klass))
  else
    {}
  end
end

#merge_record(record) ⇒ Object



31
32
33
34
35
# File 'lib/fixture_record/data.rb', line 31

def merge_record(record)
  key = record.test_fixture_name

  self[record.class].merge!(key => record._fixture_record_attributes) unless self[record.class].key?(key)
end

#write!Object



16
17
18
19
20
21
# File 'lib/fixture_record/data.rb', line 16

def write!
  FileUtils.mkdir_p(FixtureRecord.base_path)
  self.each do |klass, data|
    File.open(fixture_path_for(klass), 'w') { |f| f.write data.to_yaml }
  end
end