Sha256: ee632bb0d61abebb254ee99763203ee73a74236ae5bf20925e1636ae1b4c019c

Contents?: true

Size: 1.61 KB

Versions: 27

Compression:

Stored size: 1.61 KB

Contents

module CF
  module Populators
    module PopulatorMethods
      def self.included(klass)
        klass.class_eval do
          define_method(:type) do
            klass.name.split("::").last.downcase.to_sym
          end
        end
      end

      def populate_and_save!
        obj = get_object
        info[type] = obj.guid
        save_target_info(info)
        invalidate_client

        obj
      end

      private

      def get_object
        previous_object = client.send(type, (info[type])) if info[type]

        if input.has?(type)
          if respond_to?(:finder_argument, true)
            object = input[type, finder_argument]
          else
            object = input[type]
          end

          with_progress("Switching to #{type} #{c(object.name, :name)}") {}
        elsif info[type]
          object = previous_object if valid?(previous_object)
        end

        object ||= prompt_user

        if (previous_object != object) && respond_to?(:changed, true)
          changed
        end

        object
      end

      def prompt_user
        object_choices = choices

        if object_choices.empty?
          raise CF::UserFriendlyError.new(
            "There are no #{type}s. You may want to create one with #{c("create-#{type}", :good)}."
          )
        elsif object_choices.size == 1 && !input.interactive?(type)
          object_choices.first
        else
          ask(type.to_s.capitalize, :choices => object_choices.sort_by(&:name), :display => proc(&:name)).tap do |object|
            with_progress("Switching to #{type} #{c(object.name, :name)}") {}
          end
        end
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
cf-2.1.0 lib/cf/cli/populators/populator_methods.rb
cf-2.0.1 lib/cf/cli/populators/populator_methods.rb
cf-2.0.0 lib/cf/cli/populators/populator_methods.rb
cf-1.1.4 lib/cf/cli/populators/populator_methods.rb
cf-1.1.3.rc1 lib/cf/cli/populators/populator_methods.rb
cf-1.1.2 lib/cf/cli/populators/populator_methods.rb
cf-1.1.2.rc2 lib/cf/cli/populators/populator_methods.rb
cf-1.1.2.rc1 lib/cf/cli/populators/populator_methods.rb
cf-1.1.1 lib/cf/cli/populators/populator_methods.rb
cf-1.1.0 lib/cf/cli/populators/populator_methods.rb
cf-1.0.1.rc1 lib/cf/cli/populators/populator_methods.rb
cf-1.0.0 lib/cf/cli/populators/populator_methods.rb
cf-0.6.1.rc110 lib/cf/cli/populators/populator_methods.rb
cf-0.6.1.rc19 lib/cf/cli/populators/populator_methods.rb
cf-0.6.1.rc18 lib/cf/cli/populators/populator_methods.rb
cf-0.6.1.rc17 lib/cf/cli/populators/populator_methods.rb
cf-0.6.1.rc16 lib/cf/cli/populators/populator_methods.rb
cf-0.6.1.rc15 lib/cf/cli/populators/populator_methods.rb
cf-0.6.1.rc14 lib/cf/cli/populators/populator_methods.rb
cf-0.6.1.rc13 lib/cf/cli/populators/populator_methods.rb