Class: Banalize::Numbered
- Inherits:
-
Mash
- Object
- Mash
- Banalize::Numbered
- Defined in:
- lib/banalize/parser/numbered.rb
Overview
Class numbered implements simple model of numbered lines data structure. It’s Mash (think Hash).
Each pair is { line_number => row }. Line numbers are not necessarily sequential, i.e. line numbers in Numbered instance corresponds to those of actuall analyzed script.
Base of numbers is 1. Examples:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ruby p @shebang # { 1 => “#!/bin/bash” }
p @code # { # 2 => ‘set -e’, # 3 => ‘set -u’ # }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Instance Attribute Summary collapse
-
#search ⇒ Object
Search attribute always contains last result of search (grep) operation.
Instance Method Summary collapse
-
#add(line, number = 0) ⇒ Object
Add new line to the collection of lines.
-
#delete(lines) ⇒ Hash
Delete numbered line by its number.
-
#does_not_have?(*p) ⇒ Boolean
(also: #dont_have?)
Opposite of #has?.
-
#grep(pattern) ⇒ Numbered
Grep lines of the Numbered object (i.e. values of the hash) and return all lines together with numbers that match.
-
#has?(*params) ⇒ Boolean
(also: #have?)
Return true if values of instance have specifid pattern, returned by #grep.
-
#initialize(*p) ⇒ Numbered
constructor
A new instance of Numbered.
-
#lines ⇒ Object
(also: #numbers)
Helper method to display only line numbers of the search result.
-
#slice(from, to) ⇒ Numbered
Return all lines with numbers between from and to.
-
#sort ⇒ Array
Sort by the order of lines numbers.
-
#to_s ⇒ Object
(also: #inspect)
Human readable form.
Constructor Details
#initialize(*p) ⇒ Numbered
Returns a new instance of Numbered.
26 27 28 29 |
# File 'lib/banalize/parser/numbered.rb', line 26 def initialize *p @search = nil super *p end |
Instance Attribute Details
#search ⇒ Object
Search attribute always contains last result of search (grep) operation.
73 74 75 |
# File 'lib/banalize/parser/numbered.rb', line 73 def search @search end |
Instance Method Details
#add(line, number = 0) ⇒ Object
Add new line to the collection of lines.
Attention: since Banalize::Numbered is a hash, adding line with the number that already exists will overwrite existing one.
## Example
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ruby
@shebang.add lines.shift if lines.first =~ /^#!/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
152 153 154 155 156 157 158 159 |
# File 'lib/banalize/parser/numbered.rb', line 152 def add line, number=0 case line when String self[number.to_i] = line.chomp when Numbered self.merge! line end end |
#delete(lines) ⇒ Hash
Delete numbered line by its number
Delete line from Banalize::Numbered and return it as
single element {Numbered} object with line number.
171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/banalize/parser/numbered.rb', line 171 def delete lines ret = self.class.new case lines when Fixnum ret.add super(lines), lines when Array lines.each { |line| ret.add super(line), line } when Numbered ret.add self.delete lines.keys end ret end |
#does_not_have?(*p) ⇒ Boolean Also known as: dont_have?
Opposite of #has?
45 46 47 |
# File 'lib/banalize/parser/numbered.rb', line 45 def does_not_have? *p ! has? *p end |
#grep(pattern) ⇒ Numbered
Grep lines of the Numbered object (i.e. values of the hash) and return all lines together with numbers that match
130 131 132 |
# File 'lib/banalize/parser/numbered.rb', line 130 def grep pattern @search = self.class.new(self.select { |idx,line| line =~ pattern }) end |
#has?(*params) ⇒ Boolean Also known as: have?
Return true if values of instance have specifid pattern, returned by #grep.
37 38 39 |
# File 'lib/banalize/parser/numbered.rb', line 37 def has? *params !grep(*params).empty? end |
#lines ⇒ Object Also known as: numbers
Helper method to display only line numbers of the search result.
Comma separated string with line numbers of #search
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/banalize/parser/numbered.rb', line 56 def lines line = search.keys.join ', ' if Banalize::TRUNCATE line.truncate( Banalize::TRUNCATE, separator: ' ', omission: "... (total #{search.keys.length})" ) end end |
#slice(from, to) ⇒ Numbered
Return all lines with numbers between from and to
98 99 100 101 |
# File 'lib/banalize/parser/numbered.rb', line 98 def slice from, to from, to = from.to_i, to.to_i ret = self.class.new self.select { |k,v| k.to_i.between?(from,to) } end |
#sort ⇒ Array
Sort by the order of lines numbers
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ruby >> a.comments.sort { |a,b| a.first.to_i <=> b.first.to_i }
> [[“2”, “ Use perldoc functions to see documentation for this file.”],
["4", ":<<\"=cut\
… ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
115 116 117 |
# File 'lib/banalize/parser/numbered.rb', line 115 def sort self.sort { |a,b| a.first.to_i <=> b.first.to_i } end |
#to_s ⇒ Object Also known as: inspect
Human readable form. Only lines without numbers.
78 79 80 81 82 83 84 |
# File 'lib/banalize/parser/numbered.rb', line 78 def to_s if self.count == 1 self.values.first.to_s else self.values.to_s end end |