Module: Shamu::Entities::EntityPath

Extended by:
EntityPath
Included in:
Auditing::Transaction, EntityPath
Defined in:
lib/shamu/entities/entity_path.rb

Overview

An entity path describes one or more levels of parent/child relationships that can be used to navigate from the root entity to a target entity.

Entity paths can be used to identify polymorphic relationships between entities managed by difference services.

Instance Method Summary collapse

Instance Method Details

#compose_entity_path(entities) ⇒ String

Composes an array of entities describing the path from the root entity to the leaf into a string.

Examples:

path = compose_entity_path([
          [ "User", "45" ],
          [ "Calendar", "567" ],
          [ "Event", "1" ]
        ])
path # => "User[45]/Calendar[567]/Event[1]"

path = compose_entity_path( user )
path # => "User[45]"

Parameters:

Returns:

  • (String)


28
29
30
31
32
33
34
# File 'lib/shamu/entities/entity_path.rb', line 28

def compose_entity_path( entities )
  return unless entities.present?

  entities.map do |entity|
    compose_single_entity( entity )
  end.join( "/" )
end

#decompose_entity_path(path) ⇒ Array<Array<String,String>>

Decompose an entity path into an array of arrays of entity classes with their ids.

Examples:

entities = decompose_entity_path( "User[45]/Calendar[567]/Event[1]" )
entities # => [
         #      [ "User", "45" ],
         #      [ "Calendar", "567" ],
         #      [ "Event", "1" ]
         #    ]

Parameters:

  • path (String)

    the composed entity path.

Returns:

  • (Array<Array<String,String>>)

    the entities with their ids.



49
50
51
52
53
54
55
56
57
# File 'lib/shamu/entities/entity_path.rb', line 49

def decompose_entity_path( path )
  return unless path.present?

  path.split( "/" ).map do |node|
    entity, id = node.split "["

    [ entity, id[ 0..-2 ] ]
  end
end