Sha256: 3fb3700e7edadcbc91e1b8e158da2c0bcf2937cc5339541eceab3bc57ac36914

Contents?: true

Size: 1.36 KB

Versions: 8

Compression:

Stored size: 1.36 KB

Contents

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


require 'strscan'


module Whois
  class Record
    class Parser
      module Features

        # The Ast module tries to emulate a super-simple Abstract Syntax Tree structure
        # including method for accessing ast nodes.
        #
        # == Usage
        #
        # Include the Ast module and provide a <tt>parse</tt> instance method.
        # <tt>parse</tt> should returns a Hash representing the AST.
        #
        #   def parse
        #     Scanner.new.parse
        #   end
        #   # => { "created_on" => "2009-12-12", ... }
        #
        # 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 Ast

          def ast
            @ast ||= parse
          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

        end

      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
whois-2.0.7 lib/whois/record/parser/features/ast.rb
whois-2.0.6 lib/whois/record/parser/features/ast.rb
whois-2.0.5 lib/whois/record/parser/features/ast.rb
whois-2.0.4 lib/whois/record/parser/features/ast.rb
whois-2.0.3 lib/whois/record/parser/features/ast.rb
whois-2.0.2 lib/whois/record/parser/features/ast.rb
whois-2.0.1 lib/whois/record/parser/features/ast.rb
whois-2.0.0 lib/whois/record/parser/features/ast.rb