Module: DumpedRailers::FileHelper

Defined in:
lib/dumped_railers/file_helper.rb

Class Method Summary collapse

Class Method Details

.read_fixtures(*paths, yaml_column_permitted_classes: []) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/dumped_railers/file_helper.rb', line 8

def read_fixtures(*paths, yaml_column_permitted_classes: [])
  yaml_files = paths.flat_map { |path|
    if File.file?(path)
      path
    else
      [*Dir["#{path}/{**,*}/*.yml"], "#{path}.yml"].select { |f|
        ::File.file?(f)
      }
    end
  }.uniq.compact

  yaml_files.map { |file| 
    raw_data = ::File.read(file)
    YAML.safe_load(raw_data, permitted_classes: yaml_column_permitted_classes)
  }
end

.write(*fixtures, base_dir:) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dumped_railers/file_helper.rb', line 25

def write(*fixtures, base_dir:)
  fixtures.each do |table_name, fixture|
    pathname = 
      if defined?(Rails)
        Rails.root.join("#{base_dir}/#{table_name}.yml")
      else
        Pathname.new("#{base_dir}/#{table_name}.yml")
      end

    pathname.open('w') do |f|
      f.write fixture.to_yaml
    end
  end
end