factor out some defaults handling

This commit is contained in:
John Bintz 2012-10-09 11:24:12 -04:00
parent 7af1e933b2
commit 152067f925
4 changed files with 26 additions and 13 deletions

View File

@ -6,4 +6,5 @@ module Penchant
autoload :Env, 'penchant/env' autoload :Env, 'penchant/env'
autoload :FileProcessor, 'penchant/file_processor' autoload :FileProcessor, 'penchant/file_processor'
autoload :PenchantFile, 'penchant/penchant_file' autoload :PenchantFile, 'penchant/penchant_file'
autoload :Defaults, 'penchant/defaults'
end end

11
lib/penchant/defaults.rb Normal file
View File

@ -0,0 +1,11 @@
module Penchant
class Defaults
def initialize
@defaults = {}
end
def [](key)
@defaults[key.to_s] ||= {}
end
end
end

View File

@ -20,7 +20,7 @@ module Penchant
@data = data @data = data
@available_environments = [] @available_environments = []
@defined_git_repos = [] @defined_git_repos = []
@defaults = {} @defaults = Defaults.new
@properties = {} @properties = {}
@_current_env_defaults = {} @_current_env_defaults = {}
@ -37,6 +37,10 @@ module Penchant
@output.join("\n") @output.join("\n")
end end
def <<(string)
@output << string
end
def env(*args) def env(*args)
options = {} options = {}
options = args.pop if args.last.kind_of?(::Hash) options = args.pop if args.last.kind_of?(::Hash)
@ -71,11 +75,8 @@ module Penchant
end end
def opposites(left, right) def opposites(left, right)
@defaults[Env.new(left).to_s] ||= {} @defaults[Env.new(left)][:opposite] = right
@defaults[Env.new(left).to_s][:opposite] = right @defaults[Env.new(right)][:opposite] = left
@defaults[Env.new(right).to_s] ||= {}
@defaults[Env.new(right).to_s][:opposite] = left
end end
def for_environment?(envs) def for_environment?(envs)
@ -104,7 +105,7 @@ module Penchant
end end
def ruby(version) def ruby(version)
@output << %{ruby "#{version}"} self << %{ruby "#{version}"}
end end
protected protected
@ -180,7 +181,7 @@ module Penchant
def _defaults_for(gem_name) def _defaults_for(gem_name)
result = @_current_env_defaults result = @_current_env_defaults
result.merge(@defaults[gem_name.to_s] || {}) result.merge(@defaults[gem_name] || {})
end end
def current_os def current_os

View File

@ -25,7 +25,7 @@ module Penchant
args << options if !options.empty? args << options if !options.empty?
@output << %{gem #{args_to_string(args)}} self << %{gem #{args_to_string(args)}}
end end
def gemspec def gemspec
@ -46,16 +46,16 @@ module Penchant
end end
def group(*args, &block) def group(*args, &block)
@output << "" self << ""
@output << %{group #{args_to_string(args)} do} self << %{group #{args_to_string(args)} do}
call_and_indent_output(block) call_and_indent_output(block)
@output << %{end} self << %{end}
end end
def source(*args) def source(*args)
@output << %{source #{args_to_string(args)}} self << %{source #{args_to_string(args)}}
end end
end end
end end