module Mongoid::SleepingKingStudios::Concern

Base class for concerns with shared behavior, such as creating metadata objects from an options hash and storing that metadata in the Document class’s ::relations attribute.

@since 0.6.0

Public Instance Methods

characterize(name, options, type = nil) click to toggle source

@overload characterize name, properties, type = Metadata

Creates a metadata instance for the relation.

@param [Symbol] name The name of the relation. Must be unique for the
  base type within the sleeping_king_studios namespace.
@param [Hash] options The options for the relation.
@param [Class] type The type of the generated metadata.

@return [Metadata] The generated metadata.
# File lib/mongoid/sleeping_king_studios/concern.rb, line 22
def characterize name, options, type = nil
  type ||= Mongoid::SleepingKingStudios::Concern::Metadata
  type.new name, options
end
relate(base, name, metadata) click to toggle source

Stores the metadata in the class’s relations object. To avoid automatic Mongoid behavior on relations, adds a sleeping_king_studios accessor to the relations hash by mixing in the Relations module. Then, saves the metadata using the metadata#relation_key as the key.

@param [Class] base The base class into which the concern is mixed in. @param [Symbol] name The name of the relation. Must be unique for the

base type within the sleeping_king_studios namespace.

@param [Metadata] metadata The metadata to be stored.

# File lib/mongoid/sleeping_king_studios/concern.rb, line 36
def relate base, name, metadata
  base.relations_sleeping_king_studios.update metadata.relation_key => metadata
end
valid_options() click to toggle source

Returns a list of options that are valid for this concern.

@return [Array<Symbol>] The list of valid options.

# File lib/mongoid/sleeping_king_studios/concern.rb, line 43
def valid_options
  %i(

  ) # end array
end
validate_options(name, options) click to toggle source

Evaluates the provided options and raises an error if any of the options are invalid, based on the list from valid_options.

@param [Symbol] name The name of the relation. @param [Hash] options The options for the relation.

@raise [Mongoid::Errors::InvalidOptions] If any of the options provided

are invalid.
# File lib/mongoid/sleeping_king_studios/concern.rb, line 57
def validate_options name, options
  options.keys.each do |key|
    if !valid_options.include?(key)
      raise Mongoid::Errors::InvalidOptions.new(
        name,
        key,
        valid_options
      ) # end InvalidOptions
    end # if
  end # each
end