Class: Hybag::Validator

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/hybag/validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bag) ⇒ Validator

Returns a new instance of Validator.



8
9
10
# File 'lib/hybag/validator.rb', line 8

def initialize(bag)
  @bag = bag
end

Instance Attribute Details

#bagObject (readonly)

Returns the value of attribute bag.



6
7
8
# File 'lib/hybag/validator.rb', line 6

def bag
  @bag
end

Instance Method Details

#bag_validObject



18
19
20
# File 'lib/hybag/validator.rb', line 18

def bag_valid
  errors.add(:bag, "is not valid for bagging.") unless bag.baggable?
end

#require_persistedObject



22
23
24
# File 'lib/hybag/validator.rb', line 22

def require_persisted
  errors.add(:bag, "is not persisted.") unless bag.persisted?
end

#validate!Object



12
13
14
15
16
# File 'lib/hybag/validator.rb', line 12

def validate!
  unless self.valid?
    raise Hybag::InvalidBaggable.new(bag)
  end
end