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
-
#limiters ⇒ Object
Contains all the limiters.
-
#readers ⇒ Object
Helper method that tells the result class which methods to create as readers the first time it is created.
-
#uses(*args) ⇒ Object
Helps the command define methods to not use method missing on every instance.
-
#validate_arguments(args, skip_validations: false) ⇒ Object
Validates the limiter arguments.
-
#validate_received_arguments(args) ⇒ Object
Validates the received arguments Received arguments are the ones that your command is able to receive.
-
#validate_required_arguments(args) ⇒ Object
Validates the required arguments Required arguments are ones that your commander absolutely needs to be able to run properly.
Instance Method Details
#limiters ⇒ Object
Contains all the limiters
11 12 13 |
# File 'lib/aye_commander/limitable.rb', line 11 def limiters @limiters ||= Hash.new([]) end |
#readers ⇒ Object
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 64 |
# File 'lib/aye_commander/limitable.rb', line 56 def validate_arguments(args, skip_validations: false) unless [true, :requires].include?(skip_validations) || requires.empty? validate_required_arguments(args) end unless [true, :receives].include?(skip_validations) || receives.empty? validate_received_arguments(args) end 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.
77 78 79 80 |
# File 'lib/aye_commander/limitable.rb', line 77 def validate_received_arguments(args) extras = args.keys - (receives | requires) raise UnexpectedReceivedArgumentError, extras if extras.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.
69 70 71 72 |
# File 'lib/aye_commander/limitable.rb', line 69 def validate_required_arguments(args) missing = requires - args.keys raise MissingRequiredArgumentError, missing if missing.any? end |