Sha256: bfa60ae573b66f9c9d763e2f01dd58562b06afa9ab7719a263c56ce50c87d815

Contents?: true

Size: 1.18 KB

Versions: 7

Compression:

Stored size: 1.18 KB

Contents

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".
  #
  # CREDT: Thomas Sawyer
  def to_yamlfrag
    y = to_yaml
    y.sub!(/---\ */, '')
    y
  end
end

class File
  # File.yaml? provides a way to check if a file is a YAML
  # formatted file:
  #
  #   File.yaml?('project.yaml')  #=> true
  #   File.yaml?('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.yaml?(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

module YAML
  # Shortcut for:
  #
  #   YAML.load(File.new(file))
  #
  def self.read(file)
    load(File.new(file))
  end
end

Version data entries

7 entries across 6 versions & 1 rubygems

Version Path
facets-2.9.3 lib/standard/facets/yaml.rb
facets-2.9.2 src/standard/facets/yaml.rb
facets-2.9.2 lib/standard/facets/yaml.rb
facets-2.9.1 lib/standard/facets/yaml.rb
facets-2.9.0 lib/more/facets/yaml.rb
facets-2.9.0.pre.2 lib/more/facets/yaml.rb
facets-2.9.0.pre.1 lib/more/facets/yaml.rb