Class: RuboCop::Cop::Betterment::Utils::HardcodedAttribute

Inherits:
Object
  • Object
show all
Extended by:
NodePattern::Macros
Defined in:
lib/rubocop/cop/betterment/utils/hardcoded_attribute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ HardcodedAttribute

Returns a new instance of HardcodedAttribute.



12
13
14
15
16
# File 'lib/rubocop/cop/betterment/utils/hardcoded_attribute.rb', line 12

def initialize(node)
  @node = node
  @let_node = node.parent&.parent&.parent
  @let_name = extract_let_name(@let_node)
end

Instance Attribute Details

#let_nameObject (readonly)

Returns the value of attribute let_name.



10
11
12
# File 'lib/rubocop/cop/betterment/utils/hardcoded_attribute.rb', line 10

def let_name
  @let_name
end

#let_nodeObject (readonly)

Returns the value of attribute let_node.



10
11
12
# File 'lib/rubocop/cop/betterment/utils/hardcoded_attribute.rb', line 10

def let_node
  @let_node
end

#nodeObject (readonly)

Returns the value of attribute node.



10
11
12
# File 'lib/rubocop/cop/betterment/utils/hardcoded_attribute.rb', line 10

def node
  @node
end

Instance Method Details

#correctable?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/rubocop/cop/betterment/utils/hardcoded_attribute.rb', line 18

def correctable?
  key == :id && !let_name.nil?
end

#each_integer_referenceObject



26
27
28
29
30
# File 'lib/rubocop/cop/betterment/utils/hardcoded_attribute.rb', line 26

def each_integer_reference
  each_possible_reference(:int) do |ref|
    yield ref if ref.value == value
  end
end

#each_range_within_string(reference) ⇒ Object



38
39
40
41
42
# File 'lib/rubocop/cop/betterment/utils/hardcoded_attribute.rb', line 38

def each_range_within_string(reference)
  reference.source.enum_for(:scan, value_pattern).each do
    yield create_range(reference, Regexp.last_match)
  end
end

#each_string_referenceObject



32
33
34
35
36
# File 'lib/rubocop/cop/betterment/utils/hardcoded_attribute.rb', line 32

def each_string_reference
  each_possible_reference(:str) do |ref|
    yield ref if ref.value.match?(value_pattern)
  end
end

#replacementObject



22
23
24
# File 'lib/rubocop/cop/betterment/utils/hardcoded_attribute.rb', line 22

def replacement
  "#{let_name}.#{key}"
end