Class: Mongoid::SleepingKingStudios::Concern::Metadata

Inherits:
Hash
  • Object
show all
Defined in:
lib/mongoid/sleeping_king_studios/concern/metadata.rb

Overview

Stores information about a given concern or relation. By default, stored in the Document class's ::relations attribute. Concerns may subclass Metadata to add further keys and/or functionality.

Since:

  • 0.6.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, properties = {}) ⇒ Metadata

Returns a new instance of Metadata

Parameters:

  • name (Symbol, String)

    The name of the concern or relation.

  • properties (Hash) (defaults to: {})

    The properties of the concern or relation.

Since:

  • 0.6.0


16
17
18
19
20
21
# File 'lib/mongoid/sleeping_king_studios/concern/metadata.rb', line 16

def initialize name, properties = {}
  @name       = name
  @properties = properties.dup

  merge! properties if Hash === properties
end

Instance Attribute Details

#nameSymbol (readonly)

Returns The name of the concern or relation.

Returns:

  • (Symbol)

    The name of the concern or relation.

Since:

  • 0.6.0


24
25
26
# File 'lib/mongoid/sleeping_king_studios/concern/metadata.rb', line 24

def name
  @name
end

#propertiesHash (readonly)

Returns The unmodified properties hash that was passed into the constructor.

Returns:

  • (Hash)

    The unmodified properties hash that was passed into the constructor.

Since:

  • 0.6.0


28
29
30
# File 'lib/mongoid/sleeping_king_studios/concern/metadata.rb', line 28

def properties
  @properties
end

Instance Method Details

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

Creates a metadata instance for a subgroup of the metadata, such as a generated relation or for an optional parameter.

Parameters:

  • name (Symbol)

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

  • properties (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


40
41
42
43
# File 'lib/mongoid/sleeping_king_studios/concern/metadata.rb', line 40

def characterize name, properties, type = nil
  type ||= Mongoid::SleepingKingStudios::Concern::Metadata
  type.new name, properties
end

#relation_keyString

The key used to store the metadata inside the class's ::relations attribute. By default, adds the prefix 'sleeping_king_studios::' to the name, but can be set via the properties hash.

Returns:

  • (String)

    The key used to store the metadata.

Since:

  • 0.6.0


50
51
52
# File 'lib/mongoid/sleeping_king_studios/concern/metadata.rb', line 50

def relation_key
  self[:relation_key] || name.to_s
end

#relation_key?Boolean

Returns True if a custom relation key is defined, otherwise false.

Returns:

  • (Boolean)

    True if a custom relation key is defined, otherwise false.

Since:

  • 0.6.0


56
57
58
# File 'lib/mongoid/sleeping_king_studios/concern/metadata.rb', line 56

def relation_key?
  !!self[:relation_key]
end