Class: Kitchen::Collection

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/kitchen/collection.rb

Overview

Delegate class which adds the ability to find single and multiple objects by their #name in an Array. Hey, it’s better than monkey-patching Array, right?

Author:

Instance Method Summary collapse

Instance Method Details

#as_namesArray<String>

Returns an Array of names from the collection as strings.

Returns:

  • (Array<String>)

    array of name strings



48
49
50
# File 'lib/kitchen/collection.rb', line 48

def as_names
  __getobj__.map(&:name)
end

#get(name) ⇒ Object

Returns a single object by its name, or nil if none are found.

Parameters:

  • name (String)

    name of object

Returns:

  • (Object)

    first match by name, or nil if none are found



31
32
33
# File 'lib/kitchen/collection.rb', line 31

def get(name)
  __getobj__.find { |i| i.name == name }
end

#get_all(regexp) ⇒ Kitchen::Config::Collection<Object>

Returns a Collection of all objects whose #name is matched by the regular expression.

Parameters:

  • regexp (Regexp)

    a regular expression pattern

Returns:

  • (Kitchen::Config::Collection<Object>)

    a new collection of matched objects



41
42
43
# File 'lib/kitchen/collection.rb', line 41

def get_all(regexp)
  Kitchen::Collection.new(__getobj__.select { |i| i.name =~ regexp })
end