module DeployInfo::State

> This is the State controller. It manages State information

Attributes

state[RW]

> State Operations <= #

Public Instance Methods

add_state(app, user, params) click to toggle source

> Add Node to the State

# 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) click to toggle source

> Remove App from the State

# 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) click to toggle source
# File lib/deploy-info/state.rb, line 30
def find_state(app)
  state.detect { |h| h[:name].casecmp(app).zero? }
end
update_state(hash) click to toggle source
# 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_state() click to toggle source
# 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