Class: Blaml

Inherits:
Object
  • Object
show all
Defined in:
lib/blaml.rb,
lib/blaml/meta.rb,
lib/blaml/to_ruby.rb,
lib/blaml/blamed_io.rb,
lib/blaml/git_blamer.rb,
lib/blaml/tree_builder.rb

Defined Under Namespace

Classes: BlamedIO, GitBlamer, MetaArray, MetaHash, MetaNode, ToRuby, TreeBuilder

Constant Summary collapse

VERSION =

This gem’s version.

"1.0.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.default_blamerObject

The default blamer interface to use.



18
19
20
# File 'lib/blaml.rb', line 18

def default_blamer
  @default_blamer
end

Class Method Details

.blame_file(filename, blamer = nil) ⇒ Object

Blame the given file and load the output. Pass an optional blamer object for blame data parsing (defaults to Blaml.default_blamer).



62
63
64
65
# File 'lib/blaml.rb', line 62

def self.blame_file filename, blamer=nil
  blamer ||= self.default_blamer
  self.load blamer.blame(filename), blamer
end

.load(yaml, blamer = nil) ⇒ Object

Load yaml in to a Ruby data structure. If multiple documents are provided, the object contained in the first document will be returned.

Pass an optional blamer object for blame data parsing (defaults to Blaml.default_blamer).



31
32
33
34
# File 'lib/blaml.rb', line 31

def self.load yaml, blamer=nil
  result = parse(yaml, blamer)
  result ? result.to_blamed_ruby : result
end

.load_file(filename, blamer = nil) ⇒ Object

Load the blamed document contained in filename. Returns the yaml contained in filename as a ruby object



52
53
54
# File 'lib/blaml.rb', line 52

def self.load_file filename, blamer=nil
  self.load File.open(filename), blamer
end

.load_stream(yaml, blamer = nil) ⇒ Object

Load multiple documents given in yaml. Returns the parsed documents as a list. For example:

Psych.load_stream("--- foo\n...\n--- bar\n...") # => ['foo', 'bar']


43
44
45
# File 'lib/blaml.rb', line 43

def self.load_stream yaml, blamer=nil
  parse_stream(yaml, blamer).children.map { |child| child.to_blamed_ruby }
end

.parse(yaml, blamer = nil) ⇒ Object

Parse a blamed YAML string in yaml. Returns the first object of a YAML AST.

Pass an optional blamer object for blame data parsing (defaults to self.default_blamer).



75
76
77
78
79
80
# File 'lib/blaml.rb', line 75

def self.parse yaml, blamer=nil
  io = BlamedIO.new yaml, blamer

  children = parse_stream(io).children
  children.empty? ? false : children.first.children.first
end

.parse_file(filename, blamer = nil) ⇒ Object

Parse a file at filename. Returns the YAML AST.



86
87
88
89
90
# File 'lib/blaml.rb', line 86

def self.parse_file filename, blamer=nil
  File.open filename do |f|
    parse f, blamer
  end
end

.parse_stream(blaml, blamer = nil) ⇒ Object

Parse a YAML blame string in yaml. Returns the full AST for the YAML document with blame metadata. See Psych::parse_stream for more info.



106
107
108
109
110
# File 'lib/blaml.rb', line 106

def self.parse_stream blaml, blamer=nil
  parser = self.parser blaml
  parser.parse blaml
  parser.handler.root
end

.parser(blaml) ⇒ Object

Returns a default parser



96
97
98
# File 'lib/blaml.rb', line 96

def self.parser blaml
  Psych::Parser.new(TreeBuilder.new(blaml))
end