module Options
  module Helpers
    module Extendable

      # In Options classes (like StackTemplateOptions) method list_options
      # is defined dynamically, that's why if we'd like to extend their functionality
      # we can't use _super_, we should use aliasing.
      # So I created this helper to hide and DRY syntatic noise.
      #
      # USAGE EXAMPLE:
      # extend_options_method :list_options do |options|
      #   if args[2] == "provider" and args[3]
      #     options[:given_provider] = args[3]
      #   elsif args[2]
      #     options[:given_provider] = args[2]
      #   end
      # end
      #
      # here 'options' is a result of default list_options method.

      def extend_options_method(method_name, &block)
        old_method_name = "old_#{method_name}"
        alias_method old_method_name, method_name

        define_method(method_name) do
          options_hash = send(old_method_name)
          instance_exec(options_hash, &block)
          options_hash
        end
      end

    end
  end
end