Sha256: 8b39d824e444c2054c180eda871da8677702fd8c2a37257ae6b1403306054af2

Contents?: true

Size: 1.94 KB

Versions: 30

Compression:

Stored size: 1.94 KB

Contents

# frozen_string_literal: true

module Mutant
  # An abstract context where mutations can be applied to.
  class Context
    include Adamantium::Flat, Concord::Public.new(:scope, :source_path)
    extend AST::Sexp

    NAMESPACE_DELIMITER = '::'

    # Return root node for mutation
    #
    # @return [Parser::AST::Node]
    def root(node)
      nesting.reverse.reduce(node) do |current, scope|
        self.class.wrap(scope, current)
      end
    end

    # Identification string
    #
    # @return [String]
    def identification
      scope.name
    end

    # Wrap node into ast node
    #
    # @param [Class, Module] scope
    # @param [Parser::AST::Node] node
    #
    # @return [Parser::AST::Class]
    #   if scope is of kind Class
    #
    # @return [Parser::AST::Module]
    #   if scope is of kind module
    def self.wrap(scope, node)
      name = s(:const, nil, scope.name.split(NAMESPACE_DELIMITER).last.to_sym)
      case scope
      when Class
        s(:class, name, nil, node)
      when Module
        s(:module, name, node)
      end
    end

    # Nesting of scope
    #
    # @return [Enumerable<Class,Module>]
    def nesting
      const = Object
      name_nesting.map do |name|
        const = const.const_get(name)
      end
    end
    memoize :nesting

    # Unqualified name of scope
    #
    # @return [String]
    def unqualified_name
      name_nesting.last
    end

    # Match expressions for scope
    #
    # @return [Enumerable<Expression>]
    def match_expressions
      name_nesting.each_index.reverse_each.map do |index|
        Expression::Namespace::Recursive.new(
          scope_name: name_nesting.take(index.succ).join(NAMESPACE_DELIMITER)
        )
      end
    end
    memoize :match_expressions

    # Scope wrapped by context
    #
    # @return [Module|Class]
    attr_reader :scope

  private

    def name_nesting
      scope.name.split(NAMESPACE_DELIMITER)
    end
    memoize :name_nesting

  end # Context
end # Mutant

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
mutant-0.10.25 lib/mutant/context.rb
mutant-0.10.24 lib/mutant/context.rb
mutant-0.10.23 lib/mutant/context.rb
mutant-0.10.22 lib/mutant/context.rb
mutant-0.10.21 lib/mutant/context.rb
mutant-0.10.20 lib/mutant/context.rb
mutant-0.10.19 lib/mutant/context.rb
mutant-0.10.18 lib/mutant/context.rb
mutant-0.10.17 lib/mutant/context.rb
mutant-0.10.16 lib/mutant/context.rb
mutant-0.10.15 lib/mutant/context.rb
mutant-0.10.14 lib/mutant/context.rb
mutant-0.10.13 lib/mutant/context.rb
mutant-0.10.12 lib/mutant/context.rb
mutant-0.10.11 lib/mutant/context.rb
mutant-0.10.10 lib/mutant/context.rb
mutant-0.10.9 lib/mutant/context.rb
mutant-0.10.8 lib/mutant/context.rb
mutant-0.10.7 lib/mutant/context.rb
mutant-0.10.6 lib/mutant/context.rb