Class: Layer
Overview
Stores metadata about a file. Fields stored are:
-
range - the character range the layer applies to
-
color - the color with which to highlight the piece of code
-
message - what to display to the user
-
backend - which backend generated this layer
Layer data is stored and emitted in JSON format.
Note that ranges are always exclusive, so always use three dots!
Instance Attribute Summary collapse
-
#backend ⇒ Object
Returns the value of attribute backend.
-
#color ⇒ Object
Returns the value of attribute color.
-
#message ⇒ Object
Returns the value of attribute message.
-
#range ⇒ Object
Returns the value of attribute range.
Class Method Summary collapse
-
.interpret_where(where, file = nil) ⇒ Object
Determines the correct range as defined by a few rules:.
-
.line_to_char_range(file, line) ⇒ Object
Convert a line number to a character range given a file.
-
.method_to_char_range(file, method) ⇒ Object
Convert a method name to a character range given a file.
-
.range_string_to_char_range(range) ⇒ Object
Basically implements the inverse of Range#to_s.
-
.read(original_file) ⇒ Object
Slurp up layer data from stored JSON given the original file.
Instance Method Summary collapse
-
#initialize(where, color, message, backend, file = nil) ⇒ Layer
constructor
A new instance of Layer.
- #to_json ⇒ Object
Constructor Details
#initialize(where, color, message, backend, file = nil) ⇒ Layer
Returns a new instance of Layer.
18 19 20 21 22 |
# File 'lib/layer.rb', line 18 def initialize(where, color, , backend, file = nil) @range, @color, @message, @backend = [Layer.interpret_where(where, file), color, , backend.to_s.downcase.gsub(/backend/, '')] end |
Instance Attribute Details
#backend ⇒ Object
Returns the value of attribute backend.
16 17 18 |
# File 'lib/layer.rb', line 16 def backend @backend end |
#color ⇒ Object
Returns the value of attribute color.
16 17 18 |
# File 'lib/layer.rb', line 16 def color @color end |
#message ⇒ Object
Returns the value of attribute message.
16 17 18 |
# File 'lib/layer.rb', line 16 def @message end |
#range ⇒ Object
Returns the value of attribute range.
16 17 18 |
# File 'lib/layer.rb', line 16 def range @range end |
Class Method Details
.interpret_where(where, file = nil) ⇒ Object
Determines the correct range as defined by a few rules:
-
Numbers get interpreted as line numbers
-
Strings get converted into Ranges if they include ‘…’
-
Otherwise Strings are interpreted as Class#method names
-
Ranges pass through untouched
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/layer.rb', line 32 def Layer.interpret_where where, file = nil case where when Fixnum Layer.line_to_char_range file, where when String if where =~ /\.\.\./ # grabbing this from JSON strings Layer.range_string_to_char_range where else Layer.method_to_char_range file, where end else where end end |
.line_to_char_range(file, line) ⇒ Object
Convert a line number to a character range given a file.
48 49 50 51 52 |
# File 'lib/layer.rb', line 48 def self.line_to_char_range(file, line) file = File.read(file).split("\n") start = file[0 ... line - 1].join("\n").size + 2 (start ... start + file[line - 1].size) end |
.method_to_char_range(file, method) ⇒ Object
Convert a method name to a character range given a file.
60 61 62 63 64 65 |
# File 'lib/layer.rb', line 60 def self.method_to_char_range(file, method) # TODO: get smart about what class the method is defined on... ugh start = File.read(file) =~ Regexp.new("def .*#{method.split(/#/).last}") finish = start + 5 # TODO: fix (start ... finish) # we're just layering the word "def" for now end |
.range_string_to_char_range(range) ⇒ Object
Basically implements the inverse of Range#to_s
55 56 57 |
# File 'lib/layer.rb', line 55 def self.range_string_to_char_range(range) (range.split('...').first.to_i ... range.split('...').last.to_i) end |
.read(original_file) ⇒ Object
Slurp up layer data from stored JSON given the original file.
68 69 70 71 72 73 |
# File 'lib/layer.rb', line 68 def self.read(original_file) return [] if !File.exist?(Augment.augment_path(original_file)) layers = JSON.parse(File.read(Augment.augment_path(original_file))) layers.map!{ |l| Layer.new(l['range'], l['color'], l['message'], l['backend']) } layers.sort_by{ |l| l.range.begin }.reverse end |
Instance Method Details
#to_json ⇒ Object
75 76 77 78 |
# File 'lib/layer.rb', line 75 def to_json { 'range' => @range, 'color' => @color, 'message' => @message, 'backend' => @backend}.to_json end |