Class: Spree::Validators::Set

Inherits:
Array
  • Object
show all
Defined in:
lib/spree/validators.rb

Overview

One named set of validator classes. Subclasses Array so the historical Spree.validators.addresses — which was a plain array of classes, and which hosts may already read, map or push onto — keeps behaving exactly as it did; register/unregister are the documented API on top.

Instance Method Summary collapse

Constructor Details

#initialize(entries = []) ⇒ Set

Returns a new instance of Set.



23
24
25
26
# File 'lib/spree/validators.rb', line 23

def initialize(entries = [])
  super()
  replace(entries)
end

Instance Method Details

#register(validator) ⇒ Class

Returns the registered validator.

Parameters:

  • validator (Class, String) —

    class responding to the ActiveModel::Validator contract

Returns:

  • (Class) —

    the registered validator



31
32
33
34
35
# File 'lib/spree/validators.rb', line 31

def register(validator)
  resolved = resolve(validator)
  push(resolved) unless include?(resolved)
  resolved
end

#replace(entries) ⇒ Object



43
44
45
# File 'lib/spree/validators.rb', line 43

def replace(entries)
  super(Array(entries).map { |entry| resolve(entry) })
end

#unregister(validator) ⇒ Class?

Returns the removed validator, nil when not registered.

Parameters:

  • validator (Class, String)

Returns:

  • (Class, nil) —

    the removed validator, nil when not registered



39
40
41
# File 'lib/spree/validators.rb', line 39

def unregister(validator)
  delete(resolve(validator))
end