This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
require 'yaml'
|
||||
|
||||
module YAML
|
||||
|
||||
# Provides a simplistic way to check if a file is a YAML formatted
|
||||
# file:
|
||||
#
|
||||
# YAML.file?('project.yaml') #=> true
|
||||
# YAML.file?('project.xml') #=> false
|
||||
#
|
||||
# Note this isn't perfect. At present it depends on the use
|
||||
# use of an initial document separator (eg. '---'). With
|
||||
# YAML 1.1 the %YAML delaration is supposed to be manditory,
|
||||
# so in the future this can be adapted to fit that standard.
|
||||
|
||||
def self.file?(file)
|
||||
File.open(file) do |f|
|
||||
until f.eof?
|
||||
line = f.gets
|
||||
break true if line =~ /^---/
|
||||
break false unless line =~ /^(\s*#.*?|\s*)$/
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
class File
|
||||
|
||||
# Same as `YAML.file?(file)`.
|
||||
#
|
||||
# File.yaml?('project.yaml') #=> true
|
||||
# File.yaml?('project.xml') #=> false
|
||||
#
|
||||
def self.yaml?(file)
|
||||
YAML.file?(file)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
require 'yaml'
|
||||
|
||||
module Kernel
|
||||
|
||||
# The Kernel method #yaml is a shortcut to YAML::load.
|
||||
#
|
||||
# data = yaml %{
|
||||
# a: 1
|
||||
# b: 2
|
||||
# }
|
||||
# data #=> {"a"=>1, "b"=>2}
|
||||
#
|
||||
def yaml(*args,&blk)
|
||||
YAML.load(*args,&blk)
|
||||
end
|
||||
|
||||
# As with #to_yaml but removes the header line (i.e. '---') to create
|
||||
# a "YAML fragment".
|
||||
#
|
||||
# @author Trans
|
||||
#
|
||||
def to_yamlfrag
|
||||
y = to_yaml
|
||||
y.sub!(/---\ */, '')
|
||||
y
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
module YAML
|
||||
|
||||
# Alias for `YAML.load_file` and the same as:
|
||||
#
|
||||
# YAML.load(File.new(file))
|
||||
#
|
||||
def self.read(file)
|
||||
load(File.new(file))
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user