Class: Aio::Text::Context
- Inherits:
-
Array
- Object
- Array
- Aio::Text::Context
- Includes:
- Ui::Verbose
- Defined in:
- lib/aio/core/text/context.rb
Defined Under Namespace
Classes: OutLineError
Instance Attribute Summary collapse
-
#backup_line ⇒ Object
readonly
Returns the value of attribute backup_line.
-
#count_line ⇒ Object
readonly
Returns the value of attribute count_line.
Instance Method Summary collapse
-
#initialize(*arr) ⇒ Context
constructor
A new instance of Context.
- #method_missing(mid, *arg, &block) ⇒ Object
-
#next ⇒ Object
到下一行.
-
#readline_match_block(reg, single = false, &block) ⇒ Object
对每行进行read, 判断是单步运行还是循环运行 修改了此处的backup_line 逻辑.
-
#readline_match_block_loop(start, stop, &block) ⇒ Object
单行循环 stop 为停止出,在停止处之前的每一行都将输出.
-
#readline_range(start, stop, opt = {:last=>false}, &block) ⇒ Object
读取一个区间,并返回块 只会执行一次 stop 作为分裂符 opt 当last为false时,不会对stop一行输出 opt 当before为true时,只会分裂stop的前一行,count_line保留.
-
#readline_range_if(start, condition, &block) ⇒ Object
读取一个区间并返回块 在起始位置之后的行中,满足条件, 则放入同一区间。否则返回.
- #readline_range_if_loop(start, condition, &block) ⇒ Object
-
#readline_range_loop(start, stop, opt = {:last=>false}, &block) ⇒ Object
当需要多次循环时使用 此方法用于循环更加安全.
Methods included from Ui::Verbose
#clear_line, #print_error, #print_good, #progress_bar
Constructor Details
#initialize(*arr) ⇒ Context
Returns a new instance of Context.
12 13 14 15 16 17 18 19 |
# File 'lib/aio/core/text/context.rb', line 12 def initialize(*arr) super self.each_with_index do |str, i| self[i] = Aio::Text::LineString.new(str.chomp) end @backup_line = nil @count_line = 0 end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(mid, *arg, &block) ⇒ Object
204 205 206 207 208 |
# File 'lib/aio/core/text/context.rb', line 204 def method_missing(mid, *arg, &block) print_error "#{mid} 方法未定义,请检查方法名拼写" puts caller[0] super end |
Instance Attribute Details
#backup_line ⇒ Object (readonly)
Returns the value of attribute backup_line.
10 11 12 |
# File 'lib/aio/core/text/context.rb', line 10 def backup_line @backup_line end |
#count_line ⇒ Object (readonly)
Returns the value of attribute count_line.
10 11 12 |
# File 'lib/aio/core/text/context.rb', line 10 def count_line @count_line end |
Instance Method Details
#next ⇒ Object
到下一行
22 23 24 |
# File 'lib/aio/core/text/context.rb', line 22 def next @count_line += 1 end |
#readline_match_block(reg, single = false, &block) ⇒ Object
对每行进行read, 判断是单步运行还是循环运行修改了此处的backup_line 逻辑
28 29 30 31 32 33 34 35 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/aio/core/text/context.rb', line 28 def readline_match_block(reg, single = false, &block) reg = Aio::Base::Toolkit::Regexp.safe(reg) first = true loop do line_str = self[@count_line] if first @backup_line = @count_line end # 如果出界,有可能是因为循环导致 # 跳出循环 # 此处有问题,无法判断是否为最后一个,并跳出此模块 if @backup_line > self.size and line_str.nil? @count_line = @backup_line if Aio::Base::Debug.mismatch? puts "[-] Can't match this line: " puts caller puts " line_string: #{self[@backup_line]}" puts " regexp: #{reg}" end raise OutLineError end if line_str.nil? @count_line = @backup_line break end first = false begin res = line_str.match_block(reg, self, @count_line) do |b| block.call(b) unless block.nil? end rescue IndexError => e print_error "IndexError: 请检查模块block提取信息名称" puts "caller" puts caller exit rescue NoMethodError if Aio::Base::Debug.module_debug? print_error "NoMethodError: 请检查模块block中useful是否定义正确" puts caller end exit rescue ArgumentError => e if Aio::Base::Debug.module_debug? puts "ArgumentError:" puts e. puts caller end end @count_line += 1 # 如果匹配到,跳出循环 break if res # 如果只是单步运行,就返回 break if single end end |
#readline_match_block_loop(start, stop, &block) ⇒ Object
单行循环stop 为停止出,在停止处之前的每一行都将输出
95 96 97 98 99 100 101 102 |
# File 'lib/aio/core/text/context.rb', line 95 def readline_match_block_loop(start, stop, &block) stop = Aio::Base::Toolkit::Regexp.safe(stop) loop do readline_match_block(start, true, &block) break if stop.match(self[@count_line]) break if self[@count_line].nil? end end |
#readline_range(start, stop, opt = {:last=>false}, &block) ⇒ Object
读取一个区间,并返回块只会执行一次stop 作为分裂符opt 当last为false时,不会对stop一行输出opt 当before为true时,只会分裂stop的前一行,count_line保留
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/aio/core/text/context.rb', line 109 def readline_range(start, stop, opt={:last=>false}, &block) str_context = Context.new start = Aio::Base::Toolkit::Regexp.safe(start) stop = Aio::Base::Toolkit::Regexp.safe(stop) backup_line = @count_line loop do line_str = self[@count_line] if line_str.nil? @count_line = backup_line raise OutLineError end if start =~ line_str str_context << line_str @count_line += 1 break end @count_line += 1 end loop do line_str = self[@count_line] #raise OutLineError if line_str.nil? if stop =~ line_str or line_str.nil? str_context << line_str if opt[:last] @count_line += 1 unless opt[:before] break else str_context << line_str end @count_line += 1 end block.call(str_context) end |
#readline_range_if(start, condition, &block) ⇒ Object
读取一个区间并返回块在起始位置之后的行中,满足条件, 则放入同一区间。否则返回
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/aio/core/text/context.rb', line 161 def readline_range_if(start, condition, &block) str_context = Context.new start = Aio::Base::Toolkit::Regexp.safe(start) condition = Aio::Base::Toolkit::Regexp.safe(condition) loop do line_str = self[@count_line] raise OutLineError if line_str.nil? if start =~ line_str str_context << line_str @count_line += 1 break end @count_line += 1 end loop do line_str = self[@count_line] if condition =~ line_str str_context << line_str @count_line += 1 else break end end block.call(str_context) end |
#readline_range_if_loop(start, condition, &block) ⇒ Object
190 191 192 193 194 195 196 197 198 |
# File 'lib/aio/core/text/context.rb', line 190 def readline_range_if_loop(start, condition, &block) loop do begin readline_range_if(start, condition, &block) rescue OutLineError break end end end |
#readline_range_loop(start, stop, opt = {:last=>false}, &block) ⇒ Object
当需要多次循环时使用此方法用于循环更加安全
149 150 151 152 153 154 155 156 157 |
# File 'lib/aio/core/text/context.rb', line 149 def readline_range_loop(start, stop, opt={:last=>false}, &block) loop do begin readline_range(start, stop, opt, &block) rescue OutLineError break end end end |