Class: ArchSpec::Rules::Naming::Requires

Inherits:
Object
  • Object
show all
Defined in:
lib/archspec/rules/naming_rules.rb

Overview

Requires each selected method to have a sibling named by a template, in the same component or another (+on:+), at a given scope. The template interpolates the selector's named captures, as in requires("without_%{base}"). Rule id naming.requires.

Instance Method Summary collapse

Constructor Details

#initialize(template, on: nil, scope: :instance, because: nil) ⇒ Requires

Returns a new instance of Requires.

Raises:



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/archspec/rules/naming_rules.rb', line 109

def initialize(template, on: nil, scope: :instance, because: nil)
  unless NamingRule::VALID_SCOPES.include?(scope)
    raise Error, "requires scope: must be :instance or :class, got #{scope.inspect}"
  end
  raise Error, "requires expects a String template, got #{template.inspect}" unless template.is_a?(String)

  @template = template
  @on = on
  @target_scope = scope
  @because = because
end

Instance Method Details

#diagnostics(selected, rule, graph) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/archspec/rules/naming_rules.rb', line 125

def diagnostics(selected, rule, graph)
  target = @on || rule.source
  existing = existing_names(graph, target)

  selected.filter_map do |definition, match|
    sibling = expand(match)
    next if sibling.nil? || existing.include?(sibling.to_sym)

    Diagnostic.new(
      rule: id,
      message: message_for(definition, sibling, target, rule),
      location: definition.location,
      evidence: "#{definition.owner} defines ##{definition.name}, expected ##{sibling}"
    )
  end
end

#idObject



121
122
123
# File 'lib/archspec/rules/naming_rules.rb', line 121

def id
  'naming.requires'
end