Sha256: a2d8b7ae3915cb3673c5071cb1aa540581a695a3cce34819c0923dcaed513498

Contents?: true

Size: 1.29 KB

Versions: 4

Compression:

Stored size: 1.29 KB

Contents

require 'reek/source/sexp_node'
require 'reek/source/sexp_extensions'

module Reek
  module Source
    #
    # Adorns an abstract syntax tree with mix-in modules to make accessing
    # the tree more understandable and less implementation-dependent.
    #
    class TreeDresser
      def initialize(extensions_module = SexpExtensions, node_module = SexpNode)
        @extensions_module = extensions_module
        @node_module = node_module
      end

      def dress(sexp)
        extend_sexp(sexp)
        sexp.each_sexp { |sub| dress(sub) }
        sexp
      end

      private

      def extend_sexp(sexp)
        sexp.extend(@node_module)
        extension_module = extension_for(sexp)
        sexp.extend(extension_module) if extension_module
      end

      def extension_for(sexp)
        extension_map[sexp.sexp_type]
      end

      def extension_map
        @extension_map ||= begin
                             assoc = @extensions_module.constants.map { |const|
                               [
                                 const.to_s.sub(/Node$/, '').downcase.to_sym,
                                 @extensions_module.const_get(const)
                               ]
                             }
                             Hash[assoc]
                           end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
reek-1.3.8 lib/reek/source/tree_dresser.rb
reek-1.3.7 lib/reek/source/tree_dresser.rb
reek-1.3.6 lib/reek/source/tree_dresser.rb
reek-1.3.5 lib/reek/source/tree_dresser.rb