Class: Reek::Context::CodeContext Private

Inherits:
Object
  • Object
show all
Defined in:
lib/reek/context/code_context.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Superclass for all types of source code context. Each instance represents a code element of some kind, and each provides behaviour relevant to that code element. CodeContexts form a tree in the same way the code does, with each context holding a reference to a unique outer context.

Direct Known Subclasses

MethodContext, ModuleContext

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, exp) ⇒ CodeContext

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes a new CodeContext.

context - *_context from the ‘core` namespace exp - Reek::Source::ASTNode

Examples:

Given something like:

class Omg; def foo(x); puts x; end; end

the first time this is instantianted from TreeWalker ‘context` is a RootContext:

#<Reek::Context::RootContext:0x00000002231098 @name="">

and ‘exp` looks like this:

(class
  (const nil :Omg) nil
  (def :foo
    (args
      (arg :x))
    (send nil :puts
      (lvar :x))))

The next time we instantiate a CodeContext via TreeWalker ‘context` would be:

Reek::Context::ModuleContext

and ‘exp` is:

(def :foo

(args
  (arg :x))
(send nil :puts
  (lvar :x)))


51
52
53
54
# File 'lib/reek/context/code_context.rb', line 51

def initialize(context, exp)
  @context = context
  @exp     = exp
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Bounces messages up the context tree to the first enclosing context that knows how to deal with the request.



80
81
82
# File 'lib/reek/context/code_context.rb', line 80

def method_missing(method, *args)
  @context.send(method, *args)
end

Instance Attribute Details

#expObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



13
14
15
# File 'lib/reek/context/code_context.rb', line 13

def exp
  @exp
end

Instance Method Details

#config_for(detector_class) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



93
94
95
96
# File 'lib/reek/context/code_context.rb', line 93

def config_for(detector_class)
  context_config_for(detector_class).merge(
    config[detector_class.smell_type] || {})
end

#each_node(type, ignoring, &blk) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



64
65
66
# File 'lib/reek/context/code_context.rb', line 64

def each_node(type, ignoring, &blk)
  @exp.each_node(type, ignoring, &blk)
end

#full_nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



88
89
90
91
# File 'lib/reek/context/code_context.rb', line 88

def full_name
  context = @context ? @context.full_name : ''
  exp.full_name(context)
end

#local_nodes(type, &blk) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



60
61
62
# File 'lib/reek/context/code_context.rb', line 60

def local_nodes(type, &blk)
  each_node(type, [:casgn, :class, :module], &blk)
end

#matches?(candidates) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


68
69
70
71
72
73
74
# File 'lib/reek/context/code_context.rb', line 68

def matches?(candidates)
  my_fq_name = full_name
  candidates.any? do |candidate|
    candidate = Regexp.quote(candidate) if candidate.is_a?(String)
    /#{candidate}/ =~ my_fq_name
  end
end

#nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



56
57
58
# File 'lib/reek/context/code_context.rb', line 56

def name
  @exp.name
end

#num_methodsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



84
85
86
# File 'lib/reek/context/code_context.rb', line 84

def num_methods
  0
end