Class: Conjur::Graph::Edge

Inherits:
Object
  • Object
show all
Defined in:
lib/conjur/graph.rb

Overview

Represents a directed Edge between a parent role and a child role.

In this context, the parent role is a member of the child role. For example, the admin role is a parent of every role, either directly or indirectly, because it is added as a member to all roles it creates.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, child, admin_option = nil) ⇒ Edge

Create a directed edge with a parent and child

Parameters:

  • parent (Conjur::Role)

    the parent or source of this edge

  • child (Conjur::Role)

    the child or destination of this edge



224
225
226
227
228
# File 'lib/conjur/graph.rb', line 224

def initialize parent, child, admin_option = nil
  @parent = parent
  @child = child
  @admin_option = admin_option
end

Instance Attribute Details

#admin_optionObject (readonly) Also known as: admin_option?

Was the role granted with admin_option? May be nil if unknown (e.g. if the server doesn't return it).



217
218
219
# File 'lib/conjur/graph.rb', line 217

def admin_option
  @admin_option
end

#childConjur::Role (readonly)

Return the child of this edge. The #parent role is a member of the #child role.

Returns:



213
214
215
# File 'lib/conjur/graph.rb', line 213

def child
  @child
end

#parentConjur::Role (readonly)

Return the parent of this edge. The #parent role is a member of the #child role.

Returns:



209
210
211
# File 'lib/conjur/graph.rb', line 209

def parent
  @parent
end

Instance Method Details

#as_json(short = false) ⇒ Array, Hash

Return a value suitable for JSON serialization.

The short parameter determines whether to return a ["parent", "child"] Array or a Hash like {"parent" => "parent-role", "child" => "child-role"}.

Parameters:

  • short (Boolean) (defaults to: false)

    return an Array when true, otherwise return a Hash.

Returns:

  • (Array, Hash)

    value suitable for JSON serialization



246
247
248
# File 'lib/conjur/graph.rb', line 246

def as_json short = false
  short ? to_a : to_h
end

#to_aArray<String>

Return this edge as an Array like ["parent", "child"]

Returns:

  • (Array<String>)

    the edge as an Array



263
264
265
# File 'lib/conjur/graph.rb', line 263

def to_a
  [@parent, @child].tap {|a| a.push(@admin_option) unless @admin_option.nil?}
end

#to_hHash

Return this edge as a Hash like => "...", "child" => "...".

Note that the keys in the hash are strings.

Returns:

  • (Hash)

    a Hash representing this edge



255
256
257
258
# File 'lib/conjur/graph.rb', line 255

def to_h
  # return string keys to make testing less brittle
  {'parent' => @parent, 'child' => @child}.tap {|h| h['admin_option'] = @admin_option unless @admin_option.nil?}
end

#to_json(short = false) ⇒ String

Serialize this edge as JSON.

Parameters:

  • short (Boolean) (defaults to: false)

    when true, serialize the edge as an Array instead of a Hash

Returns:

  • (String)

    the JSON serialized edge

See Also:



235
236
237
# File 'lib/conjur/graph.rb', line 235

def to_json short = false
  as_json(short).to_json
end