lib/capistrano/env/config.rb in capistrano-env-0.3.3 vs lib/capistrano/env/config.rb in capistrano-env-1.0.0

- old
+ new

@@ -1,65 +1,48 @@ -require 'capistrano/env/formatter' +require 'capistrano/env/builder' module Capistrano module Env class Config - attr_accessor :formatter - attr_accessor :filemode + include Capistrano::Env::Builder + attr_accessor :filemode, :filename + attr_reader :envs def initialize - @values = {} - @keys = [] + @envs = {} + @filemode = '0640' + @filename = '.env' + yield(self) if block_given? end - def formatter - self.formatter = :dotenv unless @formatter - @formatter + def inspect + envs end - def filemode - self.filemode = '0640' unless @filemode - @filemode - end - - attr_writer :formatter - attr_writer :filemode - def add(name_or_regexp, val = nil, &block) if val && name_or_regexp.is_a?(String) - @values[name_or_regexp] = val + set_env(name_or_regexp, val) else - @keys << [name_or_regexp, block] + find_envs(name_or_regexp).each do |key, value| + set_env(key, value, &block) + end end end - def formatter_class - @formatter_class ||= begin - require "capistrano/env/formatter/#{formatter}_formatter" - Capistrano::Env::Formatter.const_get "#{formatter.capitalize}Formatter" - end - end + private - 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) + def set_env(key, value) + key = yield(key) if block_given? + @envs[key] = value end - def capenv_file - formatter_class.filename - end - - def capenv_content - formatter_class.format(envs) - end - - def capenv_filemode - filemode + def find_envs(key) + case key + when Regexp + ENV.select { |x| x =~ key } + else + ENV.select { |x| x == key } + end end end end end