Class: Blaml::BlamedIO

Inherits:
Object
  • Object
show all
Defined in:
lib/blaml/blamed_io.rb

Overview

IO wrapper that removes and parses blame data and makes it available as metadata

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, blamer = nil) ⇒ BlamedIO

Create new BlamedIO with a string or IO object. Pass an optional blamer object for blame data parsing (defaults to Blaml.default_blamer).



17
18
19
20
21
22
23
24
# File 'lib/blaml/blamed_io.rb', line 17

def initialize io, blamer=nil
  io = StringIO.new io if String === io

  @io        = io
  @metadata  = []
  @meta_mode = true
  @blamer    = blamer || Blaml.default_blamer
end

Instance Attribute Details

#metadataObject (readonly)

Accessor for currently available metadata.



10
11
12
# File 'lib/blaml/blamed_io.rb', line 10

def 
  @metadata
end

Instance Method Details

#getcObject

Read single char as an integer.



78
79
80
# File 'lib/blaml/blamed_io.rb', line 78

def getc
  read(1).unpack('c')[0]
end

#read(length = nil, buffer = nil) ⇒ Object

Read from the IO instance and parse the blame data.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/blaml/blamed_io.rb', line 86

def read length=nil, buffer=nil
  buffer  ||= ""
  meta_line = ""

  until buffer.length == length || @io.eof?
    if @meta_mode
      read_meta
      @meta_mode = false
    end

    char = @io.getc
    buffer    << char
    meta_line << char

    if buffer[-1..-1] == $/
      @meta_mode = true
      @metadata.last << sanitize_data(meta_line)
      meta_line = ""
    end
  end

  #puts @metadata.map{|i| i.inspect}.join("\n")
  buffer
end

#read_metaObject

Reads blame metadata.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/blaml/blamed_io.rb', line 130

def read_meta
  buffer    = ""
  meta      = nil
  start_pos = @io.pos

  until meta = @blamer.parse(buffer) do

    # Got to the end of line with no metadata.
    # Assume we're reading a regular yml IO.
    if @io.eof? || buffer =~ %r{#{$/}$}
      @io.pos = start_pos
      @metadata << [nil]
      return
    end

    buffer << @io.getc
  end

  @metadata << [meta]

  true
end

#readline(sep_string = $/) ⇒ Object Also known as: gets

Read a single line.



115
116
117
118
119
120
121
122
# File 'lib/blaml/blamed_io.rb', line 115

def readline sep_string=$/
  buffer = ""
  until buffer[-1..-1] == sep_string || @io.eof?
    buffer << read(1)
  end

  buffer
end

#sanitize_data(str) ⇒ Object

Removes leading spaces and dashes from a line of yaml data.



70
71
72
# File 'lib/blaml/blamed_io.rb', line 70

def sanitize_data str
  str.to_s.strip.gsub(%r{^(-\s)+}, "")
end

#shift_metadata_for(str) ⇒ Object

Retrieves the metadata for a given string that matches the next line in the IO stream and deletes it permanently.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/blaml/blamed_io.rb', line 36

def  str
  meta, line = @metadata.first

  if line.nil? || line.empty? || line[0..0] == '#'
    @metadata.shift
    return meta if str.empty?
  end

  meta, line = @metadata.first

  if str.length > line.length

    while str.include? @metadata.first[1] do
      tmp_meta, line = @metadata.shift
      meta = tmp_meta if
        !meta    || !meta[:updated_at]    ||
        meta     && meta[:updated_at]     &&
        tmp_meta && tmp_meta[:updated_at] &&
        tmp_meta[:updated_at] > meta[:updated_at]
    end

  else
    @metadata.first[1] = line.split(str, 2).last.strip
    @metadata.first[1].gsub!(%r{^:(\s+|$)}, "")
  end

  #puts "#{str}  ->  #{meta.inspect}"
  meta
end