Sha256: 9a079df7a5328f592563e700a874b109ee2b85d5a1119c300b3afd25f7e0122b

Contents?: true

Size: 1.24 KB

Versions: 4

Compression:

Stored size: 1.24 KB

Contents

module Glman
  module Commands
    module Configs
      class AliasesConfig
        class AliasesConfigurationError < StandardError; end

        include InitRequired

        attr_required :config_manager

        DEFAULT = {}

        def add(hash={})
          email       = hash[:email]
          alias_name  = hash[:alias_name]
          hash = alias_name.nil? ? symbolize_keys(hash) : { alias_name.to_sym => email }
          aliases = get
          aliases = aliases.merge(hash)
          set(aliases)
        end

        def delete(alias_name)
          aliases = config_manager.get[:aliases] || {}
          aliases.delete_if{ |k, _| k==alias_name.to_s }
          set(aliases)
        end

        def set(hash={})
          raise AliasesConfigurationError.new('incorrect aliases data') unless hash.kind_of? Hash
          hash = nil if hash.empty?
          config_manager.set(aliases: hash)
        end

        def get
          (config_manager.get || {})[:aliases] || DEFAULT
        end

        def clear
          set(DEFAULT)
        end

        private

        def symbolize_keys(hash)
          hash.inject({}) do |result, (key, value)|
            result[key.to_sym] = value
            result
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
glman-0.1.3 lib/glman/commands/configs/aliases_config.rb
glman-0.1.2 lib/glman/commands/configs/aliases_config.rb
glman-0.1.1 lib/glman/commands/configs/aliases_config.rb
glman-0.1.0 lib/glman/commands/configs/aliases_config.rb