Class: HammerCLI::Validator::BaseConstraint

Inherits:
Object
  • Object
show all
Defined in:
lib/hammer_cli/validator.rb

Direct Known Subclasses

AllConstraint, AnyConstraint, OneOfConstraint

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, to_check) ⇒ BaseConstraint

Returns a new instance of BaseConstraint.



13
14
15
16
17
18
19
20
21
# File 'lib/hammer_cli/validator.rb', line 13

def initialize(options, to_check)
  @to_check = to_check
  @rejected_msg = ""
  @required_msg = ""

  @options = options.inject({}) do |hash, opt|
    hash.update({opt.attribute.attribute_name => opt})
  end
end

Instance Attribute Details

#rejected_msgObject (readonly)

Returns the value of attribute rejected_msg.



11
12
13
# File 'lib/hammer_cli/validator.rb', line 11

def rejected_msg
  @rejected_msg
end

#required_msgObject (readonly)

Returns the value of attribute required_msg.



11
12
13
# File 'lib/hammer_cli/validator.rb', line 11

def required_msg
  @required_msg
end

Instance Method Details

#exist?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


33
34
35
# File 'lib/hammer_cli/validator.rb', line 33

def exist?
  raise NotImplementedError
end

#rejected(args = {}) ⇒ Object

Raises:



23
24
25
26
# File 'lib/hammer_cli/validator.rb', line 23

def rejected(args={})
  msg = args[:msg] || rejected_msg % option_switches.join(", ")
  raise ValidationError.new(msg) if exist?
end

#required(args = {}) ⇒ Object

Raises:



28
29
30
31
# File 'lib/hammer_cli/validator.rb', line 28

def required(args={})
  msg = args[:msg] || required_msg % option_switches.join(", ")
  raise ValidationError.new(msg) unless exist?
end