Class: RuboCop::Cop::DarkFinger::ActiveModelNodeDecorator

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/rubocop/cop/dark_finger/active_model_node_decorator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, misc_method_names: []) ⇒ ActiveModelNodeDecorator

Returns a new instance of ActiveModelNodeDecorator.



10
11
12
13
14
# File 'lib/rubocop/cop/dark_finger/active_model_node_decorator.rb', line 10

def initialize(node, misc_method_names: [])
  super(node)
  raise "Cannot decorate nil node" if node.nil?
  @misc_method_names = misc_method_names
end

Instance Attribute Details

#misc_method_namesObject (readonly)

Returns the value of attribute misc_method_names.



8
9
10
# File 'lib/rubocop/cop/dark_finger/active_model_node_decorator.rb', line 8

def misc_method_names
  @misc_method_names
end

Instance Method Details

#ignore_due_to_nesting?Boolean

Returns:

  • (Boolean)


38
39
40
41
42
# File 'lib/rubocop/cop/dark_finger/active_model_node_decorator.rb', line 38

def ignore_due_to_nesting?
  return false if nested_directly_in_class?
  return false if nested_in_with_options?
  true
end

#node_typeObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rubocop/cop/dark_finger/active_model_node_decorator.rb', line 16

def node_type
  if validation?
    ModelStructure::VALIDATION
  elsif association?
    ModelStructure::ASSOCIATION
  elsif callback?
    ModelStructure::CALLBACK
  elsif scope?
    ModelStructure::SCOPE
  elsif is_include?
    ModelStructure::INCLUDE
  elsif enum?
    ModelStructure::ENUM
  elsif attributes?
    ModelStructure::ATTRIBUTES
  elsif misc_method?
    ModelStructure::MISC
  else
    nil
  end
end

#preceeding_comment(processed_source) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rubocop/cop/dark_finger/active_model_node_decorator.rb', line 44

def preceeding_comment(processed_source)
  comment = processed_source.ast_with_comments[self].last&.text
  return comment if comment

  # This is needed since, in this example, ast_with_comments maps the
  # comment to `:foo` instead of the outter `validate`. Not sure how
  # else to do this.
  #
  # ## Validations ##
  # validate { :foo }
  comment = processed_source.comments.find do |comment|
    comment.location.line == location.first_line - 1
  end

  return comment.text if comment

  if nested_in_with_options?
    ActiveModelNodeDecorator.new(parent).preceeding_comment(processed_source)
  end
end

#private_declaration?Boolean

Returns:

  • (Boolean)


65
66
67
68
# File 'lib/rubocop/cop/dark_finger/active_model_node_decorator.rb', line 65

def private_declaration?
  # need to check respond_to since this may not be called only "on_send"
  respond_to?(:method_name) && method_name == :private && receiver.nil?
end