Class: PuppetX::Relay::Agent::Model::State

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_x/relay/agent/model/state.rb

Defined Under Namespace

Classes: InvalidTransitionError, MissingStatusError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ State

Returns a new instance of State.

Parameters:

  • opts (Hash)

Raises:



44
45
46
47
48
# File 'lib/puppet_x/relay/agent/model/state.rb', line 44

def initialize(opts)
  raise MissingStatusError unless opts.key? 'status'

  opts.each { |key, value| instance_variable_set("@#{key}", value) }
end

Instance Attribute Details

#job_idString (readonly)

Returns:

  • (String)


35
36
37
# File 'lib/puppet_x/relay/agent/model/state.rb', line 35

def job_id
  @job_id
end

#next_update_beforeTime (readonly)

Returns:

  • (Time)


38
39
40
# File 'lib/puppet_x/relay/agent/model/state.rb', line 38

def next_update_before
  @next_update_before
end

#outcomeString (readonly)

Returns:

  • (String)


29
30
31
# File 'lib/puppet_x/relay/agent/model/state.rb', line 29

def outcome
  @outcome
end

#run_resultsString (readonly)

Returns:

  • (String)


32
33
34
# File 'lib/puppet_x/relay/agent/model/state.rb', line 32

def run_results
  @run_results
end

#status:pending, ... (readonly)

Returns:

  • (:pending, :in_progress, :complete)


26
27
28
# File 'lib/puppet_x/relay/agent/model/state.rb', line 26

def status
  @status
end

#updated_atTime (readonly)

Returns:

  • (Time)


41
42
43
# File 'lib/puppet_x/relay/agent/model/state.rb', line 41

def updated_at
  @updated_at
end

Class Method Details

.from_h(hsh) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/puppet_x/relay/agent/model/state.rb', line 14

def from_h(hsh)
  hsh = hsh.dup

  hsh['status'] = hsh['status'].tr('-', '_').to_sym
  hsh['next_update_before'] = Time.iso8601(hsh['next_update_before']) if hsh.key? 'next_update_before'
  hsh['updated_at'] = Time.iso8601(hsh['updated_at']) if hsh.key? 'updated_at'

  new(hsh)
end

Instance Method Details

#to_complete(outcome: nil, run_results: nil) ⇒ self

Parameters:

  • outcome (String) (defaults to: nil)
  • run_results (String) (defaults to: nil)

Returns:

  • (self)

Raises:



53
54
55
56
57
58
59
60
61
62
# File 'lib/puppet_x/relay/agent/model/state.rb', line 53

def to_complete(outcome: nil, run_results: nil)
  raise InvalidTransitionError, "Cannot transition status to complete from #{status}" unless [:pending, :in_progress].include? status

  upd = dup
  upd.instance_variable_set(:@status, :complete)
  upd.instance_variable_set(:@outcome, outcome)
  upd.instance_variable_set(:@run_results, run_results)
  upd.instance_variable_set(:@next_update_before, nil)
  upd
end

#to_hHash

Returns:

  • (Hash)


78
79
80
81
82
83
84
85
86
87
# File 'lib/puppet_x/relay/agent/model/state.rb', line 78

def to_h
  {
    'status' => status.to_s.tr('_', '-'),
    'outcome' => outcome,
    'run_results' => run_results,
    'job_id' => job_id,
    'next_update_before' => (next_update_before.utc.iso8601 unless next_update_before.nil?),
    'updated_at' => (updated_at.utc.iso8601 unless updated_at.nil?),
  }.reject { |_k, v| v.nil? }
end

#to_in_progress(next_update_before, job_id: nil) ⇒ self

Parameters:

  • next_update_before (Time)
  • job_id (String) (defaults to: nil)

Returns:

  • (self)

Raises:



67
68
69
70
71
72
73
74
75
# File 'lib/puppet_x/relay/agent/model/state.rb', line 67

def to_in_progress(next_update_before, job_id: nil)
  raise InvalidTransitionError, "Cannot transition status to in-progress from #{status}" unless [:pending, :in_progress].include? status

  upd = dup
  upd.instance_variable_set(:@status, :in_progress)
  upd.instance_variable_set(:@job_id, job_id) if job_id
  upd.instance_variable_set(:@next_update_before, next_update_before)
  upd
end

#to_json(*args) ⇒ String

Returns:

  • (String)


90
91
92
# File 'lib/puppet_x/relay/agent/model/state.rb', line 90

def to_json(*args)
  to_h.to_json(*args)
end