Module: Rspec::Dunlop::SelfTest::Integrity

Extended by:
ActiveSupport::Concern
Included in:
Rspec::Dunlop::SelfTest
Defined in:
lib/rspec/dunlop/self_test/integrity.rb

Defined Under Namespace

Classes: DunlopValidationError, Pair

Instance Method Summary collapse

Instance Method Details

#properly_installed_and_configured?Boolean

it{ should be_properly_installed_and_configured }

Returns:

  • (Boolean)


6
7
8
9
10
11
12
13
14
# File 'lib/rspec/dunlop/self_test/integrity.rb', line 6

def properly_installed_and_configured?
  if defined?(TargetFile)
    target_file_valid?
  end
  if defined?(SourceFile)
    source_file_valid?
  end
  true
end

#source_file_valid?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/rspec/dunlop/self_test/integrity.rb', line 21

def source_file_valid?
  test_duplicate_model_names SourceFile.classes
end

#target_file_valid?Boolean

Returns:

  • (Boolean)


16
17
18
19
# File 'lib/rspec/dunlop/self_test/integrity.rb', line 16

def target_file_valid?
  # Naming uniqueness
  test_duplicate_model_names TargetFile.classes
end

#test_duplicate_model_names(models) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rspec/dunlop/self_test/integrity.rb', line 25

def test_duplicate_model_names(models)
  singular_names = models.map{|cls| Pair.new(cls.name, cls.model_name.human) }
  duplicate_singular_names = singular_names.group_by(&:value).select { |k, vs| vs.size > 1 }
  if duplicate_singular_names.present?
    message = duplicate_singular_names.map{|name, pairs| "The models #{pairs.map(&:key).join(', ')} have a duplicate singular name #{name}"}.join("\n")
    raise DunlopValidationError.new(message)
  end

  plural_names = models.map{|cls| Pair.new(cls.name, cls.model_name.human_plural) }
  duplicate_plural_names = plural_names.group_by(&:value).select { |k, vs| vs.size > 1 }
  if duplicate_plural_names.present?
    message = duplicate_plural_names.map{|name, pairs| "The models #{pairs.map(&:key).join(', ')} have a duplicate plural name #{name}"}.join("\n")
    raise DunlopValidationError.new(message)
  end
end