Module: AyeCommander::Limitable::ClassMethods

Included in:
Command::ClassMethods
Defined in:
lib/aye_commander/limitable.rb

Overview

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

Constant Summary collapse

LIMITERS =
%i[receives requires returns].freeze

Instance Method Summary collapse

Instance Method Details

#limitersObject

Contains all the limiters



11
12
13
# File 'lib/aye_commander/limitable.rb', line 11

def limiters
  @limiters ||= Hash.new([])
end

#readersObject

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



51
52
53
# File 'lib/aye_commander/limitable.rb', line 51

def readers
  [:status] | uses
end

#uses(*args) ⇒ Object

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



27
28
29
30
31
32
33
34
35
# 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) ⇒ Object

Validates the limiter arguments



56
57
58
59
60
61
62
63
# 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) ⇒ Object

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.



88
89
90
91
# 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) ⇒ Boolean

Dont validate if asked to skip or receives is empty

Returns:

  • (Boolean)


72
73
74
75
# 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) ⇒ Object

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



80
81
82
83
# 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) ⇒ Boolean

Dont validate if asked to skip or requires is empty

Returns:

  • (Boolean)


66
67
68
69
# File 'lib/aye_commander/limitable.rb', line 66

def validate_required_arguments?(skip_validations)
  return if [true, :requires].include?(skip_validations)
  requires.any?
end