Class: Blaml::BlamedIO
- Inherits:
-
Object
- Object
- Blaml::BlamedIO
- 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
-
#metadata ⇒ Object
readonly
Accessor for currently available metadata.
Instance Method Summary collapse
-
#getc ⇒ Object
Read single char as an integer.
-
#initialize(io, blamer = nil) ⇒ BlamedIO
constructor
Create new BlamedIO with a string or IO object.
-
#read(length = nil, buffer = nil) ⇒ Object
Read from the IO instance and parse the blame data.
-
#read_meta ⇒ Object
Reads blame metadata.
-
#readline(sep_string = $/) ⇒ Object
(also: #gets)
Read a single line.
-
#sanitize_data(str) ⇒ Object
Removes leading spaces and dashes from a line of yaml data.
-
#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.
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
#metadata ⇒ Object (readonly)
Accessor for currently available metadata.
10 11 12 |
# File 'lib/blaml/blamed_io.rb', line 10 def @metadata end |
Instance Method Details
#getc ⇒ Object
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 ||= "" = "" until buffer.length == length || @io.eof? if @meta_mode @meta_mode = false end char = @io.getc buffer << char << char if buffer[-1..-1] == $/ @meta_mode = true @metadata.last << sanitize_data() = "" end end #puts @metadata.map{|i| i.inspect}.join("\n") buffer end |
#read_meta ⇒ Object
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 buffer = "" = nil start_pos = @io.pos until = @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 << [] 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 , line = @metadata.first if line.nil? || line.empty? || line[0..0] == '#' @metadata.shift return if str.empty? end , line = @metadata.first if str.length > line.length while str.include? @metadata.first[1] do , line = @metadata.shift = if ! || ![:updated_at] || && [:updated_at] && && [:updated_at] && [:updated_at] > [:updated_at] end else @metadata.first[1] = line.split(str, 2).last.strip @metadata.first[1].gsub!(%r{^:(\s+|$)}, "") end #puts "#{str} -> #{meta.inspect}" end |