Module: Amp::Command::Validations::ClassMethods
- Defined in:
- lib/amp-front/dispatch/commands/validations.rb
Constant Summary collapse
- VALID_LENGTH_KEYS =
[:in, :within, :maximum, :minimum, :is]
Instance Method Summary collapse
-
#after(&block) ⇒ Object
Specifies one or more “after” callbacks that are run before the command executes.
- #after_blocks ⇒ Object
-
#before(&block) ⇒ Object
Specifies one or more “before” callbacks that are run before the command executes.
- #before_blocks ⇒ Object
- #extract_length_key(options) ⇒ Object
-
#numeric_comparison(value, key_used, constraint) ⇒ Boolean
Compares a value to a given constraint and returns whether it matches it or not.
-
#validates_argument_count(options = {}) ⇒ Object
Validates the number of arguments provided with the :in, :within, :maximum, :minimum, and :is relationships.
-
#validates_block(options = {}, &block) ⇒ Object
Validates that the given block runs and returns true.
-
#validates_each(*syms, &block) ⇒ Object
Validates each the given options by running their values through the block.
-
#validates_inclusion_of(*syms) ⇒ Object
Validates that the value of the given symbols is in the provided object.
-
#validates_length_of(attr, options = {}) ⇒ Object
(also: #validates_size_of)
Validates the length of the string provided as an argument matches the provided constraints on length.
-
#validates_presence_of(*syms) ⇒ Object
Validates that the given options were provided by the user.
Instance Method Details
#after(&block) ⇒ Object
Specifies one or more “after” callbacks that are run before the command executes. If any of them return non-truthy, then execution halts.
44 45 46 |
# File 'lib/amp-front/dispatch/commands/validations.rb', line 44 def after(&block) after_blocks << block end |
#after_blocks ⇒ Object
26 27 28 |
# File 'lib/amp-front/dispatch/commands/validations.rb', line 26 def after_blocks @after_blocks ||= [] end |
#before(&block) ⇒ Object
Specifies one or more “before” callbacks that are run before the command executes. If any of them return non-truthy, then execution halts.
35 36 37 |
# File 'lib/amp-front/dispatch/commands/validations.rb', line 35 def before(&block) before_blocks << block end |
#before_blocks ⇒ Object
22 23 24 |
# File 'lib/amp-front/dispatch/commands/validations.rb', line 22 def before_blocks @before_blocks ||= [] end |
#extract_length_key(options) ⇒ Object
95 96 97 98 99 100 101 102 |
# File 'lib/amp-front/dispatch/commands/validations.rb', line 95 def extract_length_key() key_used = VALID_LENGTH_KEYS.find {|key| .include?(key)} if key_used.nil? raise ArgumentError.new("One of #{VALID_LENGTH_KEYS.inspect} must be " + "provided to validates_length_of") end key_used end |
#numeric_comparison(value, key_used, constraint) ⇒ Boolean
Compares a value to a given constraint and returns whether it matches it or not.
143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/amp-front/dispatch/commands/validations.rb', line 143 def numeric_comparison(value, key_used, constraint) if key_used == :in || key_used == :within constraint.include?(value) elsif key_used == :is constraint == value elsif key_used == :minimum constraint <= value elsif key_used == :maximum constraint >= value end end |
#validates_argument_count(options = {}) ⇒ Object
Validates the number of arguments provided with the :in, :within, :maximum, :minimum, and :is relationships. To validate that a user provided at least 2 arguments in a copy command:
command "copy" do
validates_argument_count :minimum => 2
end
127 128 129 130 131 132 133 |
# File 'lib/amp-front/dispatch/commands/validations.rb', line 127 def validates_argument_count(={}) key_used = extract_length_key validates_block() do |opts, args| value = args.size numeric_comparison(value, key_used, [key_used]) end end |
#validates_block(options = {}, &block) ⇒ Object
Validates that the given block runs and returns true. TODO(adgar): Document how :if, :unless work
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/amp-front/dispatch/commands/validations.rb', line 50 def validates_block(={}, &block) before do |opts, args| if [:if] || [:unless] checker = [:if] || [:unless] result = if checker.is_a?(Proc) checker.call(opts, args) else new.send(checker, opts, args) end result = !result if [:unless] next true unless result end block.call(opts, args) end end |
#validates_each(*syms, &block) ⇒ Object
Validates each the given options by running their values through the block.
68 69 70 71 72 73 |
# File 'lib/amp-front/dispatch/commands/validations.rb', line 68 def validates_each(*syms, &block) = syms.last.is_a?(Hash) ? syms.pop : {} validates_block() do |opts, args| syms.all? {|sym| block.call(opts, sym, opts[sym])} end end |
#validates_inclusion_of(*syms) ⇒ Object
Validates that the value of the given symbols is in the provided object. Takes an options hash that must contain the :in key:
validates_inclusion_of :age, :favorite_number, :in => 13..18
Also supports :if/:unless like the rails counterparts.
86 87 88 89 90 91 92 |
# File 'lib/amp-front/dispatch/commands/validations.rb', line 86 def validates_inclusion_of(*syms) unless syms.last.is_a?(Hash) raise ArgumentError.new('validates_inclusion_of takes an options hash') end = syms.last validates_each(*syms) {|opts, attr, value| [:in].include?(value)} end |
#validates_length_of(attr, options = {}) ⇒ Object Also known as: validates_size_of
Validates the length of the string provided as an argument matches the provided constraints on length.
110 111 112 113 114 115 116 |
# File 'lib/amp-front/dispatch/commands/validations.rb', line 110 def validates_length_of(attr, ={}) key_used = extract_length_key validates_block() do |opts, args| value = opts[attr].size numeric_comparison(value, key_used, [key_used]) end end |
#validates_presence_of(*syms) ⇒ Object
Validates that the given options were provided by the user.
76 77 78 |
# File 'lib/amp-front/dispatch/commands/validations.rb', line 76 def validates_presence_of(*syms) validates_each(*syms) {|opts, attr, value| opts["#{attr}_given".to_sym]} end |