Module: Chimps::Utils::ActsOnResource

Included in:
Commands::Create, Commands::Destroy, Commands::Download, Commands::List, Commands::Search, Commands::Show, Commands::Update
Defined in:
lib/chimps-cli/utils/acts_on_resource.rb

Overview

Defines methods which can be included by a Chimps::Command to let it interpret an optional first argument as the name of a resource to act on.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



9
10
11
# File 'lib/chimps-cli/utils/acts_on_resource.rb', line 9

def self.included klass
  klass.extend(ClassMethods)
end

Instance Method Details

#first_arg_is_resource_type?Boolean

Returns:

  • (Boolean)


46
47
48
49
# File 'lib/chimps-cli/utils/acts_on_resource.rb', line 46

def first_arg_is_resource_type?
  return false if config.argv.size < 2
  self.class.allowed_resources.include?(self.class.normalize_resource_name(config.argv[1]))
end

#has_resource_identifier?Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
# File 'lib/chimps-cli/utils/acts_on_resource.rb', line 64

def has_resource_identifier?
  if first_arg_is_resource_type?
    config.argv.size >= 2 && (! config.argv[1].empty?) && config.argv[2]
  else
    config.argv.size >= 1 && (! config.argv.first.empty?) && config.argv[1]
  end
end

#plural_resourceObject



55
56
57
58
59
60
61
62
# File 'lib/chimps-cli/utils/acts_on_resource.rb', line 55

def plural_resource
  # i miss you DHH
  if resource_type[-1].chr == 'y'
    resource_type[1..-1] + 'ies'
  else
    resource_type + 's'
  end
end

#resource_identifierObject

Raises:



72
73
74
75
# File 'lib/chimps-cli/utils/acts_on_resource.rb', line 72

def resource_identifier
  raise CLIError.new(self.class::USAGE) unless has_resource_identifier?
  first_arg_is_resource_type? ? config.argv[2] : config.argv[1]
end

#resource_pathObject



85
86
87
# File 'lib/chimps-cli/utils/acts_on_resource.rb', line 85

def resource_path
  "#{plural_resource}/#{resource_identifier}"
end

#resource_typeObject



51
52
53
# File 'lib/chimps-cli/utils/acts_on_resource.rb', line 51

def resource_type
  (first_arg_is_resource_type? && self.class.normalize_resource_name(config.argv[1])) || self.class.default_resource_type
end

#resources_pathObject



77
78
79
80
81
82
83
# File 'lib/chimps-cli/utils/acts_on_resource.rb', line 77

def resources_path
  if config[:my]
    "/my/#{plural_resource}"
  else
    "/#{plural_resource}"
  end
end