Sha256: 5ed881ea05a88898d98993cc3b6b3eb0df42609ab1bbab2dae3414fe2b117041

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

module VirtualKeywords
  class ParserStrategy
    # Factory method
    # Create the appropriate strategy object for the current Ruby version.
    def self.new
      if RUBY_VERSION.start_with? '1.8'
        ParseTreeStrategy.new(ParseTree, SexpProcessor.new)
      else
        RubyParserStrategy.new(RubyParser.new)
      end
    end
  end
  # One problem that needs to be solved is that of converting source code form
  # files into sexps.

  # In Ruby 1.8, we use ParseTree and then SexpProcessor.
  # In Ruby 1.9, we use RubyParser.
  # Note that neither set of libraries seems to work in the other version.
  # Use the strategy pattern, and initialize whichever class is appropriate.
  class ParseTreeStrategy
    def initialize(parse_tree, sexp_processor)
      @parse_tree = parse_tree
      @sexp_processor = sexp_processor
    end

    def translate_instance_method(klass, method_name)
      @sexp_processor.process(@parse_tree.translate(klass, method_name))
    end
  end

  class RubyParserStrategy
    def initialize ruby_parser
      @ruby_parser = ruby_parser
    end

    def translate_instance_method(klass, method_name)
      @ruby_parser.parse(klass.instance_method(method_name).source)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
virtual_keywords-0.3.0 lib/virtual_keywords/parser_strategy.rb