Module: FrozenRecord::Backends::Yaml

Extended by:
Yaml
Included in:
Yaml
Defined in:
lib/frozen_record/backends/yaml.rb

Instance Method Summary collapse

Instance Method Details

#filename(model_name) ⇒ String

Transforms the model name into a valid filename.

Parameters:

  • format (String)

    the model name that inherits from FrozenRecord::Base

Returns:

  • (String)

    the file name.



15
16
17
# File 'lib/frozen_record/backends/yaml.rb', line 15

def filename(model_name)
  "#{model_name.underscore.pluralize}.yml"
end

#load(file_path) ⇒ Array

Reads file in ‘file_path` and return records.

Parameters:

  • format (String)

    the file path

Returns:

  • (Array)

    an Array of Hash objects with keys being attributes.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/frozen_record/backends/yaml.rb', line 23

def load(file_path)
  if !File.exist?(file_path) && File.exist?("#{file_path}.erb")
    file_path = "#{file_path}.erb"
  end

  if FrozenRecord.deprecated_yaml_erb_backend
    yml_erb_data = File.read(file_path)
    yml_data = ERB.new(yml_erb_data).result

    unless file_path.end_with?('.erb')
      if yml_data != yml_erb_data
        basename = File.basename(file_path)
        raise "[FrozenRecord] Deprecated: `#{basename}` contains ERB tags and should be renamed `#{basename}.erb`.\nSet FrozenRecord.deprecated_yaml_erb_backend = false to enable the future behavior"
      end
    end

    load_string(yml_data)
  else
    if file_path.end_with?('.erb')
      load_string(ERB.new(File.read(file_path)).result)
    else
      load_file(file_path)
    end
  end
end