Module: Shamu::Entities::EntityPath

Included in:
Auditing::Transaction
Defined in:
lib/shamu/entities/entity_path.rb

Overview

An entity path descrives 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.

Class Method Summary collapse

Class 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]"


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

def compose_entity_path( entities )
  return unless entities.present?

  entities.each_with_object( "" ) do |entity, path|
    path << "/" if path.present?
    path << compose_single_entity( entity )
  end
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" ]
         #    ]


52
53
54
55
56
57
58
59
60
# File 'lib/shamu/entities/entity_path.rb', line 52

def decompose_entity_path( path )
  return unless path.present?

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

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