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

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.

Parameters:

  • block (Proc)

    a block to run



44
45
46
# File 'lib/amp-front/dispatch/commands/validations.rb', line 44

def after(&block)
  after_blocks << block
end

#after_blocksObject



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.

Parameters:

  • block (Proc)

    a block to run



35
36
37
# File 'lib/amp-front/dispatch/commands/validations.rb', line 35

def before(&block)
  before_blocks << block
end

#before_blocksObject



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(options)
  key_used = VALID_LENGTH_KEYS.find {|key| options.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.

Parameters:

  • value (Integer)

    a numeric value

  • key_used (Symbol)

    the key used for comparison. Either :within, :in, :maximum, :minimum, and :is.

  • constraint (Integer, #include?)

    a value to compare against

Returns:

  • (Boolean)


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(options={})
  key_used = extract_length_key options
  validates_block(options) do |opts, args|
    value = args.size
    numeric_comparison(value, key_used, options[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(options={}, &block)
  before do |opts, args|
    if options[:if] || options[:unless]
      checker = options[:if] || options[:unless]
      result = if checker.is_a?(Proc)
                 checker.call(opts, args)
               else
                 new.send(checker, opts, args)
               end
      result = !result if options[: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)
  options = syms.last.is_a?(Hash) ? syms.pop : {}
  validates_block(options) 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
  options = syms.last
  validates_each(*syms) {|opts, attr, value| options[: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.

Parameters:

  • attr (Symbol)

    the attribute to validate

  • options (Hash) (defaults to: {})

    A set of options that specifies the constraint. Must have one of the following: :in, :within, :maximum, :minimum, :is.



110
111
112
113
114
115
116
# File 'lib/amp-front/dispatch/commands/validations.rb', line 110

def validates_length_of(attr, options={})
  key_used = extract_length_key options
  validates_block(options) do |opts, args|
    value = opts[attr].size
    numeric_comparison(value, key_used, options[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