Sha256: 2b236114a321daee8524e64a1b9edca186d19c508122a24b8046d3ada38fb81a

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

require_relative 'persistence'

module ArisControl
  class Bookkeeper
    attr_reader :persistence, :users

    def initialize(persistence = default_persistence)
      @persistence = persistence
      @users = persistence.load_users
    end

    def list
      users
    end

    def add(name, opts = {})
      user     = users[name] || {}
      ssh_key  = opts[:ssh_key]
      env_vars = opts[:env_vars]

      _opts = Hash.new.tap do |h|
        h[:email]    = opts[:email] || user.fetch(:email)
        h[:ssh_key]  = ssh_key || user[:ssh_key] || ''
        h[:env_vars] = with_upcased_keys(env_vars) if env_vars
      end

      users[name] = user.merge(_opts)
      persistence.store_users(users)
    end

    def delete(name)
      users.delete(name)
      persistence.store_users(users)
    end

    def default_persistence
      ArisControl::Persistence.new
    end

    private

    def with_upcased_keys(hash)
      hash or return
      hash.each_with_object({}) {|(k,v),h| h[k.to_s.upcase] = v }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
aris-control-2.0.2 lib/aris-control/bookkeeper.rb