module FrozenRecord::Backends::Yaml

Public Instance Methods

filename(model_name) click to toggle source

Transforms the model name into a valid filename.

@param format [String] the model name that inherits

from FrozenRecord::Base

@return [String] the file name.

# File lib/frozen_record/backends/yaml.rb, line 15
def filename(model_name)
  "#{model_name.underscore.pluralize}.yml"
end
load(file_path) click to toggle source

Reads file in `file_path` and return records.

@param format [String] the file path @return [Array] an Array of Hash objects with keys being attributes.

# 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

Private Instance Methods

load_file(path) click to toggle source
# File lib/frozen_record/backends/yaml.rb, line 58
def load_file(path)
  YAML.unsafe_load_file(path, freeze: true) || Dedup::EMPTY_ARRAY
end
load_string(yaml) click to toggle source
# File lib/frozen_record/backends/yaml.rb, line 62
def load_string(yaml)
  YAML.unsafe_load(yaml, freeze: true) || Dedup::EMPTY_ARRAY
end