module AyeCommander::Limitable::ClassMethods

Limitable is a module which functionality is completely defined at class level.

Constants

LIMITERS

Public Instance Methods

limiters() click to toggle source

Contains all the limiters

# File lib/aye_commander/limitable.rb, line 11
def limiters
  @limiters ||= Hash.new([])
end
readers() click to toggle source

Helper method that tells the result class which methods to create as readers the first time it is created.

# File lib/aye_commander/limitable.rb, line 51
def readers
  [:status] | uses
end
uses(*args) click to toggle source

Helps the command define methods to not use method missing on every instance.

The original idea was to encourage to use uses for a small performance boost when running their commands since the methods would be created at load time. This idea has been since scrapped since it would make the commands look very convoluted and the performance hit is probably neglegible since the methods themselves are defined after the first method missing.

The functionality however still remains as limited call this method internally

# File lib/aye_commander/limitable.rb, line 27
def uses(*args)
  uses = limiters[:uses]
  return uses if args.empty?

  missing = args - uses
  attr_accessor(*missing) if missing.any?

  limiters[:uses] |= args
end
validate_arguments(args, skip_validations: false) click to toggle source

Validates the limiter arguments

# File lib/aye_commander/limitable.rb, line 56
def validate_arguments(args, skip_validations: false)
  if validate_required_arguments?(skip_validations)
    validate_required_arguments(args)
  end

  return unless validate_received_arguments?(skip_validations)
  validate_received_arguments(args)
end
validate_received_arguments(args) click to toggle source

Validates the received arguments Received arguments are the ones that your command is able to receive. Any other argument not defined by this would be considered an error.

# File lib/aye_commander/limitable.rb, line 88
def validate_received_arguments(args)
  extras = args.keys - (receives | requires)
  raise UnexpectedReceivedArgumentError, extras if extras.any?
end
validate_received_arguments?(skip_validations) click to toggle source

Dont validate if asked to skip or receives is empty

# File lib/aye_commander/limitable.rb, line 72
def validate_received_arguments?(skip_validations)
  return if [true, :receives].include?(skip_validations)
  receives.any?
end
validate_required_arguments(args) click to toggle source

Validates the required arguments Required arguments are ones that your commander absolutely needs to be able to run properly.

# File lib/aye_commander/limitable.rb, line 80
def validate_required_arguments(args)
  missing = requires - args.keys
  raise MissingRequiredArgumentError, missing if missing.any?
end
validate_required_arguments?(skip_validations) click to toggle source

Dont validate if asked to skip or requires is empty

# File lib/aye_commander/limitable.rb, line 66
def validate_required_arguments?(skip_validations)
  return if [true, :requires].include?(skip_validations)
  requires.any?
end