Module: Mongoid::SleepingKingStudios::Concern

Included in:
HasTree, HasTree::CacheAncestry, Orderable, Sluggable
Defined in:
lib/mongoid/sleeping_king_studios/concern.rb,
lib/mongoid/sleeping_king_studios/concern/metadata.rb

Overview

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

Defined Under Namespace

Classes: Metadata

Instance Method Summary collapse

Instance Method Details

#characterize(name, properties, type = Metadata) ⇒ Metadata

Creates a metadata instance for the relation.

Parameters:

  • name (Symbol)

    The name of the relation. Must be unique for the base type within the sleeping_king_studios namespace.

  • options (Hash)

    The options for the relation.

  • type (Class) (defaults to: Metadata)

    The type of the generated metadata.

Returns:

  • (Metadata)

    The generated metadata.

Since:

  • 0.6.0



22
23
24
25
# 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) ⇒ Object

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.

Parameters:

  • base (Class)

    The base class into which the concern is mixed in.

  • name (Symbol)

    The name of the relation. Must be unique for the base type within the sleeping_king_studios namespace.

  • metadata (Metadata)

    The metadata to be stored.

Since:

  • 0.6.0



36
37
38
# File 'lib/mongoid/sleeping_king_studios/concern.rb', line 36

def relate base, name, 
  base.relations_sleeping_king_studios.update .relation_key => 
end

#valid_optionsArray<Symbol>

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

Returns:

  • (Array<Symbol>)

    The list of valid options.

Since:

  • 0.6.0



43
44
45
46
47
# File 'lib/mongoid/sleeping_king_studios/concern.rb', line 43

def valid_options
  %i(

  ) # end array
end

#validate_options(name, options) ⇒ Object

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

Parameters:

  • name (Symbol)

    The name of the relation.

  • options (Hash)

    The options for the relation.

Raises:

  • (Mongoid::Errors::InvalidOptions)

    If any of the options provided are invalid.

Since:

  • 0.6.0



57
58
59
60
61
62
63
64
65
66
67
# 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