Sha256: 874f725d8e5635ebd3613b7413b2db1a3469bb796815931d8cbc83344a579d66

Contents?: true

Size: 1.3 KB

Versions: 3

Compression:

Stored size: 1.3 KB

Contents

require 'capistrano/env/formatter'

module Capistrano
  module Env
    class Config
      attr_accessor :formatter

      def initialize
        @values = {}
        @keys = []
      end

      def formatter
        self.formatter = :dotenv unless @formatter
        @formatter
      end

      attr_writer :formatter

      def add(name_or_regexp, val = nil, &block)
        if val && name_or_regexp.is_a?(String)
          @values[name_or_regexp] = val
        else
          @keys << [name_or_regexp, block]
        end
      end

      def formatter_class
        @formatter_class ||= begin
                               require "capistrano/env/formatter/#{formatter}_formatter"
                               Capistrano::Env::Formatter.const_get "#{formatter.capitalize}Formatter"
                             end
      end

      def envs
        result = {}
        @keys.each do |key, block|
          key_values = key.is_a?(Regexp) ? ENV.select { |x| x =~ key } : ENV.select { |x| x == key }
          key_values = Hash[key_values.map { |k, v| [block.call(k), v] }] if block
          result.merge!(key_values)
        end
        result.merge(@values)
      end

      def capenv_file
        formatter_class.filename
      end

      def capenv_content
        formatter_class.format(envs)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
capistrano-env-0.3.2 lib/capistrano/env/config.rb
capistrano-env-0.3.1 lib/capistrano/env/config.rb
capistrano-env-0.3.0 lib/capistrano/env/config.rb