class Hashie::Extensions::Parsers::YamlParser
Parses YAML without evaluating ERB templates.
@example
require 'tempfile' file = Tempfile.new(['hashie-example', '.yml'], '.') begin file.write("name: Ada\n") file.close Hashie::Extensions::Parsers::YamlParser.perform(file.path) # => {'name' => 'Ada'} ensure file.close! end
Public Class Methods
new(file_path, options = {})
click to toggle source
@example Read a Pathname and retain parsing options
require 'tempfile' file = Tempfile.new(['hashie-example', '.yml'], '.') begin file.write("name: Ada\n") file.close parser = Hashie::Extensions::Parsers::YamlParser.new(Pathname.new(file.path), aliases: false) parser.perform # => {'name' => 'Ada'} ensure file.close! end
# File lib/hashie/extensions/parsers/yaml_parser.rb, line 33 def initialize(file_path, options = {}) @content = File.read(file_path) @file_path = file_path.is_a?(Pathname) ? file_path.to_s : file_path @options = options end
perform(file_path, options = {})
click to toggle source
@example Load YAML in one step
require 'tempfile' file = Tempfile.new(['hashie-example', '.yml'], '.') begin file.write("count: 3\n") file.close Hashie::Extensions::Parsers::YamlParser.perform(file.path, aliases: false) # => {'count' => 3} ensure file.close! end
# File lib/hashie/extensions/parsers/yaml_parser.rb, line 64 def self.perform(file_path, options = {}) new(file_path, options).perform end
Public Instance Methods
perform()
click to toggle source
@example Leave ERB untouched when loading YAML
require 'tempfile' file = Tempfile.new(['hashie-example', '.yml'], '.') begin file.write("value: <%= 1 + 2 %>\n") file.close parser = Hashie::Extensions::Parsers::YamlParser.new(file.path) parser.perform # => {'value' => '<%= 1 + 2 %>'} ensure file.close! end
# File lib/hashie/extensions/parsers/yaml_parser.rb, line 50 def perform yaml_safe_load(@content) end
Private Instance Methods
yaml_safe_load(content)
click to toggle source
# File lib/hashie/extensions/parsers/yaml_parser.rb, line 70 def yaml_safe_load(content) YAML.safe_load( content, permitted_classes: @options.fetch(:permitted_classes) { [] }, permitted_symbols: @options.fetch(:permitted_symbols) { [] }, aliases: @options.fetch(:aliases, true) ) end