class Cuprum::Collections::Basic::Repository

A repository represents a group of Basic collections.

Public Class Methods

new(data: {}) click to toggle source

@param data [Hash<String, Object>] Seed data to use when building

collections.
Calls superclass method Cuprum::Collections::Repository::new
# File lib/cuprum/collections/basic/repository.rb, line 12
def initialize(data: {})
  super()

  @data = data
end

Public Instance Methods

build(collection_name:, data: nil, **options) click to toggle source

Adds a new collection with the given name to the repository.

@param collection_name [String] The name of the new collection. @param data [Hash<String, Object>] The inital data for the collection. If

not specified, defaults to the data used to initialize the repository.

@param options [Hash] Additional options to pass to Collection.new

@return [Cuprum::Collections::Basic::Collection] the created collection.

@see Cuprum::Collections::Basic::Collection#initialize.

# File lib/cuprum/collections/basic/repository.rb, line 28
def build(collection_name:, data: nil, **options)
  validate_collection_name!(collection_name)
  validate_data!(data)

  collection = Cuprum::Collections::Basic.new(
    collection_name: collection_name,
    data:            data || @data.fetch(collection_name.to_s, []),
    **options
  )

  add(collection)

  collection
end

Private Instance Methods

valid_collection?(collection) click to toggle source
# File lib/cuprum/collections/basic/repository.rb, line 45
def valid_collection?(collection)
  collection.is_a?(Cuprum::Collections::Basic::Collection)
end
validate_collection_name!(name) click to toggle source
# File lib/cuprum/collections/basic/repository.rb, line 49
def validate_collection_name!(name)
  raise ArgumentError, "collection name can't be blank" if name.nil?

  unless name.is_a?(String) || name.is_a?(Symbol)
    raise ArgumentError, 'collection name must be a String or Symbol'
  end

  return unless name.empty?

  raise ArgumentError, "collection name can't be blank"
end
validate_data!(data) click to toggle source
# File lib/cuprum/collections/basic/repository.rb, line 61
def validate_data!(data)
  return if data.nil? || data.is_a?(Array)

  raise ArgumentError, 'data must be an Array of Hashes'
end