Sha256: 2cffedb998a85e16956459a8ea32a8fd6e4bf497c819eee9b81ab30b023c58e0

Contents?: true

Size: 1.27 KB

Versions: 9

Compression:

Stored size: 1.27 KB

Contents

require 'strscan'

module Whois
  module Scanners

    # The Scannable module tries to emulate a super-simple Abstract Syntax Tree structure
    # including method for accessing ast nodes.
    #
    # == Usage
    #
    # Include the Scannable module and set the `self.scanner` value.
    #
    #   class ParserFoo
    #     include Scannable
    #
    #     self.scanner = ScannerFoo
    #   end
    #
    # Now you can access the AST using the <tt>node</tt> method.
    #
    #   node "created_on"
    #   # => "2009-12-12"
    #
    #   node? "created_on"
    #   # => true
    #
    #   node? "created_at"
    #   # => false
    #
    module Scannable

      def self.included(base)
        base.class_attribute :scanner
      end

      def node(key)
        if block_given?
          value = ast[key]
          value = yield(value) unless value.nil?
          value
        else
          ast[key]
        end
      end

      def node?(key)
        !ast[key].nil?
      end

      def parse
        scanner  = self.scanner.is_a?(Array) ? self.scanner.first : self.scanner
        settings = self.scanner.is_a?(Array) ? self.scanner.last  : {}
        scanner.new(settings).parse(content_for_scanner)
      end

      private

      def ast
        @ast ||= parse
      end

    end

  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
whois-parser-2.0.0 lib/whois/scanners/scannable.rb
whois-parser-1.2.0 lib/whois/scanners/scannable.rb
whois-parser-1.1.0 lib/whois/scanners/scannable.rb
whois-parser-1.0.1 lib/whois/scanners/scannable.rb
hetzner-whois-parser-1.0.1.rc2 lib/whois/scanners/scannable.rb
hetzner-whois-parser-1.0.1.rc1 lib/whois/scanners/scannable.rb
whois-parser-1.0.0 lib/whois/scanners/scannable.rb
whois-parser-1.0.0.pre.beta2 lib/whois/scanners/scannable.rb
whois-parser-1.0.0.pre.beta1 lib/whois/scanners/scannable.rb