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-0.6.1.rc12 lib/cf/cli/populators/populator_methods.rb
cf-0.6.1.rc11 lib/cf/cli/populators/populator_methods.rb
cf-0.6.1.rc10 lib/cf/cli/populators/populator_methods.rb
cf-0.6.1.rc9 lib/cf/cli/populators/populator_methods.rb
cf-0.6.1.rc8 lib/cf/cli/populators/populator_methods.rb
cf-0.6.1.rc7 lib/cf/cli/populators/populator_methods.rb
cf-0.6.1.rc6 lib/cf/cli/populators/populator_methods.rb