Class: ImproveYourCode::Context::CodeContext

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/improve_your_code/context/code_context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exp) ⇒ CodeContext

Returns a new instance of CodeContext.



19
20
21
22
23
24
# File 'lib/improve_your_code/context/code_context.rb', line 19

def initialize(exp)
  @exp                = exp
  @children           = []
  @statement_counter  = StatementCounter.new
  @refs               = AST::ObjectRefs.new
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



17
18
19
# File 'lib/improve_your_code/context/code_context.rb', line 17

def children
  @children
end

#expObject (readonly)

Returns the value of attribute exp.



17
18
19
# File 'lib/improve_your_code/context/code_context.rb', line 17

def exp
  @exp
end

#parentObject (readonly)

Returns the value of attribute parent.



17
18
19
# File 'lib/improve_your_code/context/code_context.rb', line 17

def parent
  @parent
end

#statement_counterObject (readonly)

Returns the value of attribute statement_counter.



17
18
19
# File 'lib/improve_your_code/context/code_context.rb', line 17

def statement_counter
  @statement_counter
end

Instance Method Details

#append_child_context(child) ⇒ Object



44
45
46
47
# File 'lib/improve_your_code/context/code_context.rb', line 44

def append_child_context(child)
  children << child
  self
end

#apply_current_visibility(_current_visibility) ⇒ Object



97
# File 'lib/improve_your_code/context/code_context.rb', line 97

def apply_current_visibility(_current_visibility); end

#config_for(detector_class) ⇒ Object



79
80
81
82
83
# File 'lib/improve_your_code/context/code_context.rb', line 79

def config_for(detector_class)
  parent_config_for(detector_class).merge(
    configuration_via_code_commment[detector_class.smell_type] || {}
  )
end

#each {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



31
32
33
34
35
36
37
38
# File 'lib/improve_your_code/context/code_context.rb', line 31

def each(&block)
  return enum_for(:each) unless block_given?

  yield self
  children.each do |child|
    child.each(&block)
  end
end

#full_nameObject



75
76
77
# File 'lib/improve_your_code/context/code_context.rb', line 75

def full_name
  exp.full_name('')
end

#instance_method?Boolean

Returns:

  • (Boolean)


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

def instance_method?
  false
end

#local_nodes(type, ignored = [], &blk) ⇒ Object



26
27
28
29
# File 'lib/improve_your_code/context/code_context.rb', line 26

def local_nodes(type, ignored = [], &blk)
  ignored += %i[casgn class module]
  each_node(type, ignored, &blk)
end

#matches?(candidates) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#number_of_statementsObject



85
86
87
# File 'lib/improve_your_code/context/code_context.rb', line 85

def number_of_statements
  statement_counter.value
end

#record_call_to(exp) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/improve_your_code/context/code_context.rb', line 49

def record_call_to(exp)
  receiver = exp.receiver
  type = receiver ? receiver.type : :self
  line = exp.line
  case type
  when :lvar, :lvasgn
    unless exp.object_creation_call?
      refs.record_reference(name: receiver.name, line: line)
    end
  when :self
    refs.record_reference(name: :self, line: line)
  end
end

#record_use_of_selfObject



63
64
65
# File 'lib/improve_your_code/context/code_context.rb', line 63

def record_use_of_self
  refs.record_reference(name: :self)
end

#register_with_parent(parent) ⇒ Object



40
41
42
# File 'lib/improve_your_code/context/code_context.rb', line 40

def register_with_parent(parent)
  @parent = parent.append_child_context(self) if parent
end

#singleton_method?Boolean

Returns:

  • (Boolean)


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

def singleton_method?
  false
end