From 152067f9253e32c8b34c417a4fc2d42a7f0cd3cf Mon Sep 17 00:00:00 2001 From: John Bintz Date: Tue, 9 Oct 2012 11:24:12 -0400 Subject: [PATCH] factor out some defaults handling --- lib/penchant.rb | 1 + lib/penchant/defaults.rb | 11 +++++++++++ lib/penchant/file_processor.rb | 17 +++++++++-------- lib/penchant/penchant_file.rb | 10 +++++----- 4 files changed, 26 insertions(+), 13 deletions(-) create mode 100644 lib/penchant/defaults.rb diff --git a/lib/penchant.rb b/lib/penchant.rb index d72d22a..fa1c7b6 100644 --- a/lib/penchant.rb +++ b/lib/penchant.rb @@ -6,4 +6,5 @@ module Penchant autoload :Env, 'penchant/env' autoload :FileProcessor, 'penchant/file_processor' autoload :PenchantFile, 'penchant/penchant_file' + autoload :Defaults, 'penchant/defaults' end diff --git a/lib/penchant/defaults.rb b/lib/penchant/defaults.rb new file mode 100644 index 0000000..3635100 --- /dev/null +++ b/lib/penchant/defaults.rb @@ -0,0 +1,11 @@ +module Penchant + class Defaults + def initialize + @defaults = {} + end + + def [](key) + @defaults[key.to_s] ||= {} + end + end +end diff --git a/lib/penchant/file_processor.rb b/lib/penchant/file_processor.rb index 263724c..271f026 100644 --- a/lib/penchant/file_processor.rb +++ b/lib/penchant/file_processor.rb @@ -20,7 +20,7 @@ module Penchant @data = data @available_environments = [] @defined_git_repos = [] - @defaults = {} + @defaults = Defaults.new @properties = {} @_current_env_defaults = {} @@ -37,6 +37,10 @@ module Penchant @output.join("\n") end + def <<(string) + @output << string + end + def env(*args) options = {} options = args.pop if args.last.kind_of?(::Hash) @@ -71,11 +75,8 @@ module Penchant end def opposites(left, right) - @defaults[Env.new(left).to_s] ||= {} - @defaults[Env.new(left).to_s][:opposite] = right - - @defaults[Env.new(right).to_s] ||= {} - @defaults[Env.new(right).to_s][:opposite] = left + @defaults[Env.new(left)][:opposite] = right + @defaults[Env.new(right)][:opposite] = left end def for_environment?(envs) @@ -104,7 +105,7 @@ module Penchant end def ruby(version) - @output << %{ruby "#{version}"} + self << %{ruby "#{version}"} end protected @@ -180,7 +181,7 @@ module Penchant def _defaults_for(gem_name) result = @_current_env_defaults - result.merge(@defaults[gem_name.to_s] || {}) + result.merge(@defaults[gem_name] || {}) end def current_os diff --git a/lib/penchant/penchant_file.rb b/lib/penchant/penchant_file.rb index b310d7b..9a76785 100644 --- a/lib/penchant/penchant_file.rb +++ b/lib/penchant/penchant_file.rb @@ -25,7 +25,7 @@ module Penchant args << options if !options.empty? - @output << %{gem #{args_to_string(args)}} + self << %{gem #{args_to_string(args)}} end def gemspec @@ -46,16 +46,16 @@ module Penchant end def group(*args, &block) - @output << "" - @output << %{group #{args_to_string(args)} do} + self << "" + self << %{group #{args_to_string(args)} do} call_and_indent_output(block) - @output << %{end} + self << %{end} end def source(*args) - @output << %{source #{args_to_string(args)}} + self << %{source #{args_to_string(args)}} end end end