From 9e9ce113cdac8ce290afeb611bffa00ef0282f23 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Tue, 9 Oct 2012 14:33:12 -0400 Subject: [PATCH] refactor the most complex thing in the app, in preparation of more cleaning --- features/ruby_gemfile.feature | 1 + lib/penchant.rb | 5 ++++ lib/penchant/custom_property.rb | 20 +++++++++++++ lib/penchant/file_processor.rb | 36 ++---------------------- lib/penchant/penchant_file.rb | 4 +-- lib/penchant/properties.rb | 14 +++++++++ lib/penchant/property.rb | 0 lib/penchant/property_stack.rb | 26 +++++++++++++++++ lib/penchant/property_stack_builder.rb | 23 +++++++++++++++ lib/penchant/property_stack_processor.rb | 26 +++++++++++++++++ 10 files changed, 119 insertions(+), 36 deletions(-) create mode 100644 lib/penchant/custom_property.rb create mode 100644 lib/penchant/properties.rb create mode 100644 lib/penchant/property.rb create mode 100644 lib/penchant/property_stack.rb create mode 100644 lib/penchant/property_stack_builder.rb create mode 100644 lib/penchant/property_stack_processor.rb diff --git a/features/ruby_gemfile.feature b/features/ruby_gemfile.feature index 96173d1..d63188e 100644 --- a/features/ruby_gemfile.feature +++ b/features/ruby_gemfile.feature @@ -263,6 +263,7 @@ Feature: Gemfiles gem "two", {:require=>nil} """ + @wip Scenario: Set the opposite environment in the environment defaults Given I have the file "Gemfile.penchant" with the content: """ diff --git a/lib/penchant.rb b/lib/penchant.rb index fa1c7b6..63aa66a 100644 --- a/lib/penchant.rb +++ b/lib/penchant.rb @@ -7,4 +7,9 @@ module Penchant autoload :FileProcessor, 'penchant/file_processor' autoload :PenchantFile, 'penchant/penchant_file' autoload :Defaults, 'penchant/defaults' + autoload :Properties, 'penchant/properties' + autoload :CustomProperty, 'penchant/custom_property' + autoload :PropertyStack, 'penchant/property_stack' + autoload :PropertyStackBuilder, 'penchant/property_stack_builder' + autoload :PropertyStackProcessor, 'penchant/property_stack_processor' end diff --git a/lib/penchant/custom_property.rb b/lib/penchant/custom_property.rb new file mode 100644 index 0000000..b6e398e --- /dev/null +++ b/lib/penchant/custom_property.rb @@ -0,0 +1,20 @@ +module Penchant + class CustomProperty + def initialize(value) + @value = value + end + + def process(values) + if @value.respond_to?(:call) + @value.call(*values).to_a + else + @value.collect do |k, v| + v = v.dup.gsub(%r{\$(\d+)}) { |m| values[m.to_i - 1 ] } + + [ k, v ] + end + end + end + end +end + diff --git a/lib/penchant/file_processor.rb b/lib/penchant/file_processor.rb index 945bc7c..6a42916 100644 --- a/lib/penchant/file_processor.rb +++ b/lib/penchant/file_processor.rb @@ -21,7 +21,7 @@ module Penchant @available_environments = [] @defined_git_repos = [] @defaults = Defaults.new - @properties = {} + @properties = PropertyStackBuilder.new(@defaults) @_current_env_defaults = {} end @@ -131,9 +131,7 @@ module Penchant end def process_options(gem_name, template = {}) - properties = {} - - property_stack = template.to_a + properties = Properties.new(template) original_properties = process_option_stack(gem_name, property_stack) @@ -148,36 +146,6 @@ module Penchant Hash[properties.sort] end - def process_option_stack(gem_name, stack) - property_stack = stack.dup - properties = {} - - while !property_stack.empty? - key, value = property_stack.shift - - if property = @properties[key] - values = [ value ].flatten - - if property.respond_to?(:call) - property.call(*values).each do |k, v| - property_stack.push([ k, v ]) - end - else - property.each do |k, v| - v = v.dup.gsub(%r{\$(\d+)}) { |m| values[m.to_i - 1 ] } - property_stack.push([ k, v ]) - end - end - else - value = value % gem_name if value.respond_to?(:%) - - properties[key] = value - end - end - - properties - end - def _defaults_for(gem_name) result = @_current_env_defaults result.merge(@defaults[gem_name] || {}) diff --git a/lib/penchant/penchant_file.rb b/lib/penchant/penchant_file.rb index 9a76785..0a1bfbd 100644 --- a/lib/penchant/penchant_file.rb +++ b/lib/penchant/penchant_file.rb @@ -14,7 +14,7 @@ module Penchant version = args.first - options = process_options(gem_name.first, template) + options = @properties.create_stack_for(template, @_strip_pathing_options).process_for_gem(gem_name.first, @_current_env_defaults) args = [ gem_name.first ] args << version if version @@ -36,7 +36,7 @@ module Penchant gems, template = split_args(args) gems.flatten.each do |gem_name| - options = process_options(gem_name, template) + options = @properties.create_stack_for(template, @_strip_pathing_options).process_for_gem(gem_name) args = [ gem_name ] args << options if !options.empty? diff --git a/lib/penchant/properties.rb b/lib/penchant/properties.rb new file mode 100644 index 0000000..940bbdd --- /dev/null +++ b/lib/penchant/properties.rb @@ -0,0 +1,14 @@ +module Penchant + class Properties + def initialize(template) + @properties = {} + + @template = template.to_a + end + + def process + + end + end +end + diff --git a/lib/penchant/property.rb b/lib/penchant/property.rb new file mode 100644 index 0000000..e69de29 diff --git a/lib/penchant/property_stack.rb b/lib/penchant/property_stack.rb new file mode 100644 index 0000000..c97d702 --- /dev/null +++ b/lib/penchant/property_stack.rb @@ -0,0 +1,26 @@ +module Penchant + class PropertyStack + def initialize(builder, property_stack, strip_pathing_options) + @builder, @property_stack, @strip_pathing_options = builder, property_stack.dup, strip_pathing_options + end + + def processor + @processor ||= PropertyStackProcessor.new(@builder) + end + + def process_for_gem(gem_name, additional_env = {}) + properties = processor.process(gem_name, @property_stack) + + if @strip_pathing_options + [ :git, :branch, :path ].each { |key| properties.delete(key) } + end + + properties = processor.process(gem_name, @builder.defaults[gem_name].merge(additional_env)).merge(properties) + + properties.delete(:opposite) + + Hash[properties.sort] + end + end +end + diff --git a/lib/penchant/property_stack_builder.rb b/lib/penchant/property_stack_builder.rb new file mode 100644 index 0000000..7a0a3d2 --- /dev/null +++ b/lib/penchant/property_stack_builder.rb @@ -0,0 +1,23 @@ +module Penchant + class PropertyStackBuilder + attr_reader :defaults + + def initialize(defaults) + @defaults = defaults + + @custom_properties = {} + end + + def []=(key, value) + @custom_properties[key] = CustomProperty.new(value) + end + + def [](key) + @custom_properties[key] + end + + def create_stack_for(stack, strip_pathing_options = false) + PropertyStack.new(self, stack, strip_pathing_options) + end + end +end diff --git a/lib/penchant/property_stack_processor.rb b/lib/penchant/property_stack_processor.rb new file mode 100644 index 0000000..5bcae4b --- /dev/null +++ b/lib/penchant/property_stack_processor.rb @@ -0,0 +1,26 @@ +module Penchant + class PropertyStackProcessor + def initialize(builder) + @builder = builder + end + + def process(gem_name, stack) + properties = {} + property_stack = stack.dup.to_a + + while !property_stack.empty? + key, value = property_stack.shift + + if property = @builder[key] + property_stack += property.process([ value ].flatten) + else + value = value % gem_name if value.respond_to?(:%) + + properties[key] = value + end + end + + properties + end + end +end