3
# Like attr_writer, but the writer method validates the
4
# setting against the given block.
6
# NOTE: This is not (presently) a common core extension and is not
7
# loaded automatically when using <code>require 'facets'</code>.
11
def attr_validator(*symbols, &validator)
13
symbols.each do |symbol|
14
define_method "#{symbol}=" do |val|
15
unless validator.call(val)
16
raise ArgumentError, "Invalid value provided for #{symbol}"
18
instance_variable_set("@#{symbol}", val)
20
made << "#{symbol}=".to_sym
25
# Create aliases for validators.
27
# NOTE: This is not (presently) a common core extension and is not
28
# loaded automatically when using <code>require 'facets'</code>.
30
def alias_validator(*args)
34
#alias_method(name, orig)
35
alias_method("#{name}=", "#{orig}=")