Class: Puppet::Relationship

Inherits:
Object show all
Includes:
Network::FormatSupport
Defined in:
lib/puppet/relationship.rb

Overview

This is Puppet’s class for modeling edges in its configuration graph. It used to be a subclass of GRATR::Edge, but that class has weird hash overrides that dramatically slow down the graphing.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Network::FormatSupport

included, #mime, #render, #support_format?, #to_json, #to_msgpack, #to_pson

Constructor Details

#initialize(source, target, options = {}) ⇒ Relationship

Returns a new instance of Relationship.



36
37
38
39
40
41
42
43
44
45
# File 'lib/puppet/relationship.rb', line 36

def initialize(source, target, options = {})
  @source, @target = source, target

  options = (options || {}).inject({}) { |h,a| h[a[0].to_sym] = a[1]; h }
  [:callback, :event].each do |option|
    if value = options[option]
      send(option.to_s + "=", value)
    end
  end
end

Instance Attribute Details

#callbackObject



12
13
14
# File 'lib/puppet/relationship.rb', line 12

def callback
  @callback
end

#eventObject



14
15
16
# File 'lib/puppet/relationship.rb', line 14

def event
  @event
end

#sourceObject



12
13
14
# File 'lib/puppet/relationship.rb', line 12

def source
  @source
end

#targetObject



12
13
14
# File 'lib/puppet/relationship.rb', line 12

def target
  @target
end

Class Method Details

.from_data_hash(data) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/puppet/relationship.rb', line 16

def self.from_data_hash(data)
  source = data["source"]
  target = data["target"]

  args = {}
  if event = data["event"]
    args[:event] = event
  end
  if callback = data["callback"]
    args[:callback] = callback
  end

  new(source, target, args)
end

Instance Method Details

#inspectObject



70
71
72
# File 'lib/puppet/relationship.rb', line 70

def inspect
  "{ #{source} => #{target} }"
end

#labelObject



59
60
61
62
63
64
# File 'lib/puppet/relationship.rb', line 59

def label
  result = {}
  result[:callback] = callback if callback
  result[:event] = event if event
  result
end

#match?(event) ⇒ Boolean

Does the passed event match our event? This is where the meaning of :NONE comes from.

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
# File 'lib/puppet/relationship.rb', line 49

def match?(event)
  if self.event.nil? or event == :NONE or self.event == :NONE
    return false
  elsif self.event == :ALL_EVENTS or event == self.event
    return true
  else
    return false
  end
end

#refObject



66
67
68
# File 'lib/puppet/relationship.rb', line 66

def ref
  "#{source} => #{target}"
end

#to_data_hashObject



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/puppet/relationship.rb', line 74

def to_data_hash
  data = {
    'source' => source.to_s,
    'target' => target.to_s
  }

  ["event", "callback"].each do |attr|
    next unless value = send(attr)
    data[attr] = value
  end
  data
end

#to_sObject



87
88
89
# File 'lib/puppet/relationship.rb', line 87

def to_s
  ref
end