module KnifeAttribute::Get

Public Class Methods

included(base) click to toggle source
# File lib/knife-attribute/get.rb, line 3
def self.included(base)
  base.class_eval do
    include KnifeAttribute::CommonOptions

    def run
      check_arguments

      if config[:attribute_type]
        get_attribute(entity.send(attribute_type_map[config[:attribute_type].to_sym]))
      elsif entity.respond_to?(:construct_attributes)
        get_attribute(entity.construct_attributes)
      else
        get_attribute(entity.send(attribute_type_map[default_attribute_type]))
      end
    end

    private
      def check_arguments
        if entity_name.nil?
          show_usage
          ui.fatal("You must specify a #{entity_type.to_s} name")
          exit 1
        end

        if attribute.nil?
          show_usage
          ui.fatal('You must specify an attribute')
          exit 1
        end

        check_type
      end

      def get_attribute(target)
        result = ui.presenter.extract_nested_value(target, attribute)
        output(format_for_display(result))
      end
  end
end

Public Instance Methods

check_arguments() click to toggle source
# File lib/knife-attribute/get.rb, line 20
def check_arguments
  if entity_name.nil?
    show_usage
    ui.fatal("You must specify a #{entity_type.to_s} name")
    exit 1
  end

  if attribute.nil?
    show_usage
    ui.fatal('You must specify an attribute')
    exit 1
  end

  check_type
end
get_attribute(target) click to toggle source
# File lib/knife-attribute/get.rb, line 36
def get_attribute(target)
  result = ui.presenter.extract_nested_value(target, attribute)
  output(format_for_display(result))
end
run() click to toggle source
# File lib/knife-attribute/get.rb, line 7
def run
  check_arguments

  if config[:attribute_type]
    get_attribute(entity.send(attribute_type_map[config[:attribute_type].to_sym]))
  elsif entity.respond_to?(:construct_attributes)
    get_attribute(entity.construct_attributes)
  else
    get_attribute(entity.send(attribute_type_map[default_attribute_type]))
  end
end