Module: DeployInfo::State

Extended by:
State
Included in:
State
Defined in:
lib/deploy-info/state.rb

Overview

> This is the State controller. It manages State information

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#stateObject

> State Operations <= #



24
25
26
# File 'lib/deploy-info/state.rb', line 24

def state
  @state
end

Instance Method Details

#add_state(app, user, params) ⇒ Object

> Add Node to the State



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/deploy-info/state.rb', line 52

def add_state(app, user, params) # rubocop: disable MethodLength, AbcSize
  # => Create an App-State Object
  (n = {}) && (n[:name] = app)
  n[:created] = DateTime.now
  n[:creator] = user
  # => Parse our Field Values
  %w(type).each do |opt|
    n[opt.to_sym] = params[opt] if params[opt]
  end
  # => Parse our Booleans
  %w(protected).each do |opt|
    n[opt.to_sym] = true if params[opt] && %w(true 1).any? { |x| params[opt].to_s.casecmp(x).zero? }
  end
  # => Build the Updated State
  update_state(n)
  # => Return the Added App
  find_state(node)
end

#delete_state(app) ⇒ Object

> Remove App from the State



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/deploy-info/state.rb', line 72

def delete_state(app)
  # => Find the App
  existing = find_state(app)
  return 'App not present in state' unless existing
  # => Delete the App from State
  state.delete(existing)
  # => Write Out the Updated State
  write_state
  # => Return the Deleted App
  existing
end

#find_state(app) ⇒ Object



30
31
32
# File 'lib/deploy-info/state.rb', line 30

def find_state(app)
  state.detect { |h| h[:name].casecmp(app).zero? }
end

#update_state(hash) ⇒ Object

rubocop: disable AbcSize



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/deploy-info/state.rb', line 34

def update_state(hash) # rubocop: disable AbcSize
  # => Check if App Already Exists
  existing = find_state(hash[:name])
  if existing # => Update the Existing App
    state.delete(existing)
    audit_string = [DateTime.now, hash[:creator]].join(' - ')
    existing[:last_modified] = existing[:last_modified].is_a?(Array) ? existing[:last_modified].take(5).unshift(audit_string) : [audit_string]
    hash = existing
  end

  # => Update the State
  state.push(hash)

  # => Write Out the Updated State
  write_state
end

#write_stateObject



84
85
86
87
88
89
90
# File 'lib/deploy-info/state.rb', line 84

def write_state
  # => Sort & Unique State
  state.sort_by! { |h| h[:name].downcase }.uniq!

  # => Write Out the Updated State
  Util.write_json_config(Config.state_file, state)
end