class Mudguard::Infrastructure::Persistence::PolicyFile

A file containing the Mudguard-Policies

Public Class Methods

read(project_path) click to toggle source
# File lib/mudguard/infrastructure/persistence/policy_file.rb, line 13
def read(project_path)
  policy_file = File.join(project_path, ".mudguard.yml")
  policy_exists = File.exist?(policy_file)

  unless policy_exists
    template_file = File.join(__dir__, ".mudguard.template.yml")
    FileUtils.cp(template_file, policy_file)
  end

  read_yml(policy_file)
end

Private Class Methods

read_yml(policy_file) click to toggle source
# File lib/mudguard/infrastructure/persistence/policy_file.rb, line 27
def read_yml(policy_file)
  yaml_file = File.read(policy_file)
  yaml = YAML.safe_load(yaml_file, [Symbol], [], policy_file) || {}
  yaml.transform_values { |value| (value || []).map(&method(:unsymbolize)) }
rescue Psych::SyntaxError => e
  raise Mudguard::Domain::Error, "#{policy_file} is invalid (#{e.message})"
end
unsymbolize(dependency) click to toggle source
# File lib/mudguard/infrastructure/persistence/policy_file.rb, line 35
def unsymbolize(dependency)
  if dependency.is_a?(Symbol)
    ":#{dependency}"
  else
    dependency
  end
end