Class: MicroformatParser::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/uformatparser.rb

Overview

Implements a rule.

A rule identifies matching nodes using a selector, and a means to extract their value using an extractor. The rule also identifies an instance variable and attribute accessor to retrieve the extracted value, and the cardinality of that value.

For more information see MicroformatParser.rule.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, selector, extractor, limit) ⇒ Rule

Returns a new instance of Rule.



380
381
382
383
384
385
386
# File 'lib/uformatparser.rb', line 380

def initialize(name, selector, extractor, limit)
    # Change the rule name to the attribute name holding the result
    @name = "@#{name.to_s}".to_sym
    @selector = selector
    @extractor = extractor
    @limit = limit
end

Instance Attribute Details

#extractorObject (readonly)

The rule extractor



378
379
380
# File 'lib/uformatparser.rb', line 378

def extractor
  @extractor
end

#limitObject

The rule cardinality (or value limit)

0

No value (disabled)

1

First value extracted

n

Up to n values (array)

-1

Unbound (array)



374
375
376
# File 'lib/uformatparser.rb', line 374

def limit
  @limit
end

#nameObject (readonly)

The instance variable/attribute accessor name.



368
369
370
# File 'lib/uformatparser.rb', line 368

def name
  @name
end

#selectorObject (readonly)

The rule selector



376
377
378
# File 'lib/uformatparser.rb', line 376

def selector
  @selector
end

Instance Method Details

#inspectObject



432
433
434
# File 'lib/uformatparser.rb', line 432

def inspect
    @selector ? "[to #{@name} from #{@selector.inspect}, #{@extractor.inspect}, limit #{@limit}]" : "[to #{@name} from #{@extractor.inspect}, limit #{@limit}]"
end

#process(node, context) ⇒ Object

Called to process this rule on a node with a context object.

Returns true if the rule was processed and should be reduced (not applied to any child nodes). Otherwise, returns false.



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/uformatparser.rb', line 392

def process(node, context)
    # Do nothing if rule is disabled (limit = 0), reduce it.
    return true if @limit == 0
    # Do nothing if rule is singular (limit = 1) and a value was already set
    current = context.instance_variable_get(@name)
    return true if @limit == 1 and current
    # Match the current node, do nothing if not matched
    # (Selector may be nil if rule created to wrap a proc)
    if @selector
        return false unless @selector.instance_of?(UnboundMethod) ? @selector.bind(context).call(node) :
            @selector.instance_of?(Selector) ? @selector.match(node) : @selector.call(node)
    end
    # Extract the value. Do nothing if nothing extracted
    value = case @extractor
    when UnboundMethod
        @extractor.bind(context).call(node)
    when Extractor
        @extractor.extract(node)
    when Proc, Method
        @extractor.call(node)
    when Class
        @extractor.parse(node)
    end
    return false unless value
    # If limit=1, set the new value (singular)
    # If no current value, create new array with new value
    # Otherwise, if no limit or limit not reach, append value to
    # existing array
    if @limit == 1
        context.instance_variable_set(@name, value)
    elsif not current
        context.instance_variable_set(@name, [value])
    elsif current.instance_of? Array and (@limit < 0 or current.size < @limit)
        current << value
    end
    # We always return true, since there's no point in applying
    # the rule to any child nodes.
    return true
end