Class: Banalize::Numbered

Inherits:
Mash
  • Object
show all
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:

p @shebang # { 1 => "#!/bin/bash" }

p @code
# { 
#   2 => 'set -e',
#   3 => 'set -u'
# }

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Numbered) initialize(*p)

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

- (Object) search

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

- (Object) add(line, number = 0)

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

    @shebang.add lines.shift if lines.first =~ /^#!/

Parameters:

  • line (String, Numbered)

    Line or Numbered object to add

  • number (Finxum) (defaults to: 0)

    Line number in the analized script. If First param is Banalize::Numbered instance, then this argument is not used.



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

- (Hash) delete(lines)

Delete numbered line by its number

Delete line from Banalize::Numbered and return it as single element Banalize::Numbered object with line number.

Parameters:

  • lines (Fixnum, Array[Fixnum], Numbered)

    Lines to delete from Numbered instance.

Returns:



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

- (Boolean) does_not_have?(*p) Also known as: dont_have?

Opposite of #has?

Returns:

  • (Boolean)

See Also:



45
46
47
# File 'lib/banalize/parser/numbered.rb', line 45

def does_not_have? *p
  ! has? *p
end

- (Numbered) grep(pattern)

Grep lines of the Numbered object (i.e. values of the hash) and return all lines together with numbers that match

Parameters:

  • pattern (Regexp)

    Search Regexp pattern for lines. It should be regexp, not string, since regular Array grep is not used here.

Returns:



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

- (Boolean) has?(*params) Also known as: have?

Return true if values of instance have specifid pattern, returned by #grep.

Returns:

  • (Boolean)

See Also:



37
38
39
# File 'lib/banalize/parser/numbered.rb', line 37

def has? *params
  !grep(*params).empty?
end

- (Object) lines 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

- (Numbered) slice(from, to)

Return all lines with numbers between from and to

Parameters:

  • from (Finxum)

    Starting line

  • to (Finxum)

    Ending line

Returns:

  • (Numbered)

    Instance of Numbered with all lines beween specified numbers. Numbers can be not sequntial.



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

- (Array) sort

Sort by the order of lines numbers

>> 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\ 
...

Returns:

  • (Array)

    Sorted array of 2-elements arrays:



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

- (Object) to_s 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