Module: Modspec::ChildContainer::ClassMethods

Defined in:
lib/modspec/child_container.rb

Instance Method Summary collapse

Instance Method Details

#validates_children(collection_name, empty_label:, child_label:) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/modspec/child_container.rb', line 10

def validates_children(collection_name, empty_label:, child_label:)
  define_method(:validate_children_presence) do
    children = send(collection_name)
    if children.nil? || children.empty?
      ["#{empty_label} #{identifier} has no child #{child_label}"]
    else
      []
    end
  end

  define_method(:validate_children_identifier_prefix) do
    children = send(collection_name)
    return [] unless children

    expected_prefix = "#{identifier}/"
    children.filter_map do |child|
      unless child.identifier.to_s.start_with?(expected_prefix)
        msg = "#{child_label} #{child.identifier} "
        msg += "does not share the expected prefix #{expected_prefix}"
        msg
      end
    end
  end
end