Class: Metrocot::BasePattern
- Inherits:
-
Object
- Object
- Metrocot::BasePattern
- Defined in:
- lib/metrocot.rb
Overview
base class for all other patterns. Provides some reasonable default behaviours.
Direct Known Subclasses
AnythingPattern, CompositePattern, OneOrMorePattern, OptSpacePattern, PathPattern, TextPattern
Constant Summary collapse
- @@instance_count =
0
Instance Attribute Summary collapse
-
#matched ⇒ Object
Returns the value of attribute matched.
-
#metrocot ⇒ Object
Returns the value of attribute metrocot.
-
#name ⇒ Object
Returns the value of attribute name.
-
#node_scraper ⇒ Object
Returns the value of attribute node_scraper.
-
#pattern_no ⇒ Object
Returns the value of attribute pattern_no.
-
#pred ⇒ Object
Returns the value of attribute pred.
-
#source ⇒ Object
Returns the value of attribute source.
-
#succ ⇒ Object
Returns the value of attribute succ.
Class Method Summary collapse
Instance Method Summary collapse
- #default_scanner ⇒ Object
- #description ⇒ Object
- #dump(level, out) ⇒ Object
- #dump_match_map(out, level, match_map) ⇒ Object
- #each_match(match_range, match_map) ⇒ Object
-
#initialize(source) ⇒ BasePattern
constructor
A new instance of BasePattern.
- #log(s) ⇒ Object
- #log_match_data(msg, match_range, match_map) ⇒ Object
- #optional ⇒ Object
- #priority ⇒ Object
- #with_scanned_match_data(match_map, match_data) ⇒ Object
Constructor Details
#initialize(source) ⇒ BasePattern
Returns a new instance of BasePattern.
170 171 172 173 174 |
# File 'lib/metrocot.rb', line 170 def initialize( source ) @source = source @pattern_no = @@instance_count @@instance_count += 1 end |
Instance Attribute Details
#matched ⇒ Object
Returns the value of attribute matched.
166 167 168 |
# File 'lib/metrocot.rb', line 166 def matched @matched end |
#metrocot ⇒ Object
Returns the value of attribute metrocot.
166 167 168 |
# File 'lib/metrocot.rb', line 166 def metrocot @metrocot end |
#name ⇒ Object
Returns the value of attribute name.
166 167 168 |
# File 'lib/metrocot.rb', line 166 def name @name end |
#node_scraper ⇒ Object
Returns the value of attribute node_scraper.
166 167 168 |
# File 'lib/metrocot.rb', line 166 def node_scraper @node_scraper end |
#pattern_no ⇒ Object
Returns the value of attribute pattern_no.
166 167 168 |
# File 'lib/metrocot.rb', line 166 def pattern_no @pattern_no end |
#pred ⇒ Object
Returns the value of attribute pred.
166 167 168 |
# File 'lib/metrocot.rb', line 166 def pred @pred end |
#source ⇒ Object
Returns the value of attribute source.
166 167 168 |
# File 'lib/metrocot.rb', line 166 def source @source end |
#succ ⇒ Object
Returns the value of attribute succ.
166 167 168 |
# File 'lib/metrocot.rb', line 166 def succ @succ end |
Class Method Details
.parse(s) ⇒ Object
188 189 190 |
# File 'lib/metrocot.rb', line 188 def self.parse(s) raise "not supported" end |
Instance Method Details
#default_scanner ⇒ Object
280 281 282 |
# File 'lib/metrocot.rb', line 280 def default_scanner nil end |
#description ⇒ Object
192 193 194 195 196 197 198 |
# File 'lib/metrocot.rb', line 192 def description if name "#{self.class.name} \"#{name}\"" else self.class.name end end |
#dump(level, out) ⇒ Object
184 185 186 |
# File 'lib/metrocot.rb', line 184 def dump( level, out ) out << " " * level + description + " p=#{priority}\n" end |
#dump_match_map(out, level, match_map) ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/metrocot.rb', line 201 def dump_match_map( out, level, match_map ) if match_map.is_a? Hash out << "{\n" level += 1 match_map.each { |key, value| out << " " * level + "#{key} => " dump_match_map( out, level, value ) } level -= 1 out << " " * level + "}\n" elsif match_map.is_a? Array out << "[\n" level += 1 match_map.each { |value| out << " " * level dump_match_map( out, level, value ) } level -= 1 out << " " * level + "]\n" elsif match_map.is_a? String out << "\"" + match_map + "\"\n" elsif match_map.is_a? Hpricot::Elem out << "<" + match_map.stag.name + ">\n" else out << match_map.class.to_s + "\n" end end |
#each_match(match_range, match_map) ⇒ Object
240 241 242 |
# File 'lib/metrocot.rb', line 240 def each_match( match_range, match_map ) log_match_data("each_match", match_range, match_map) end |
#log(s) ⇒ Object
180 181 182 |
# File 'lib/metrocot.rb', line 180 def log( s ) metrocot.log("#{self.description}: #{s}") end |
#log_match_data(msg, match_range, match_map) ⇒ Object
230 231 232 233 234 235 236 237 |
# File 'lib/metrocot.rb', line 230 def log_match_data( msg, match_range, match_map ) log("#{msg} #{match_range.describe} map:") if @node_scraper.verbose if ! match_map.nil? && match_map != {} dump_match_map( STDOUT, 0, match_map ) end end end |
#optional ⇒ Object
176 177 178 |
# File 'lib/metrocot.rb', line 176 def optional false end |
#priority ⇒ Object
244 245 246 |
# File 'lib/metrocot.rb', line 244 def priority 0 end |
#with_scanned_match_data(match_map, match_data) ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/metrocot.rb', line 248 def with_scanned_match_data( match_map, match_data ) scanner = if name @node_scraper.scanner_by_name(name) else default_scanner end if scanner # begin match_map[name] = scanner.scan(match_data) # rescue # log("scanner error: #{$!}") # return nil # end elsif name match_map[name] = match_data elsif self.is_a?(CompositePattern) && match_data.is_a?(Hash) log("copying #{match_data.class.name} match data from #{self.description}") match_data.each { |key, value| match_map[key] = value } else log("not carrying #{match_data.class.name} match data from #{self.description}") end result = yield( match_map ) match_map.delete(name) if name return result end |