Sha256: e9147ad46cb3ad8087fda0c3496d6840a66ca35dd3a76b8670c30ed793ecccae

Contents?: true

Size: 1.53 KB

Versions: 15

Compression:

Stored size: 1.53 KB

Contents

#--
# Ruby Whois
#
# An intelligent pure Ruby WHOIS client and parser.
#
# Copyright (c) 2009-2014 Simone Carletti <weppos@weppos.net>
#++


require 'strscan'


module Whois
  class Record
    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
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
whois-3.6.2 lib/whois/record/scanners/scannable.rb
whois-3.6.1 lib/whois/record/scanners/scannable.rb
whois-3.6.0 lib/whois/record/scanners/scannable.rb
whois-3.5.9 lib/whois/record/scanners/scannable.rb
whois-3.5.8 lib/whois/record/scanners/scannable.rb
whois-3.5.7 lib/whois/record/scanners/scannable.rb
whois-3.5.6 lib/whois/record/scanners/scannable.rb
whois-3.5.5 lib/whois/record/scanners/scannable.rb
whois-3.5.4 lib/whois/record/scanners/scannable.rb
whois-3.5.3 lib/whois/record/scanners/scannable.rb
whois-3.5.2 lib/whois/record/scanners/scannable.rb
whois-3.5.1 lib/whois/record/scanners/scannable.rb
whois-3.5.0 lib/whois/record/scanners/scannable.rb
whois-3.4.5 lib/whois/record/scanners/scannable.rb
whois-3.4.4 lib/whois/record/scanners/scannable.rb