~ubuntu-branches/ubuntu/trusty/ruby-facets/trusty

« back to all changes in this revision

Viewing changes to lib/core-uncommon/facets/module/attr_validator.rb

  • Committer: Package Import Robot
  • Author(s): Marc Dequènes (Duck)
  • Date: 2013-06-21 14:41:38 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20130621144138-nb3t82yl5gl8p7a6
Tags: 2.9.3-1
* New upstream release:
  - rakefile is gone, switched back to setuprb method
  - recreated documention build rules and adapted to new paths
* Updated copyright file.
* Increased Standards-Version to 3.9.4 (no changes).
* Removed transitional libfacets-* package and obsolete relations.
* Use system JQuery library instead of embedded one for documentation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
class Module
2
 
 
3
 
  # Like attr_writer, but the writer method validates the
4
 
  # setting against the given block.
5
 
  #
6
 
  # NOTE: This is not (presently) a common core extension and is not
7
 
  # loaded automatically when using <code>require 'facets'</code>.
8
 
  #
9
 
  # CREDIT: ?
10
 
 
11
 
  def attr_validator(*symbols, &validator)
12
 
    made = []
13
 
    symbols.each do |symbol|
14
 
      define_method "#{symbol}=" do |val|
15
 
        unless validator.call(val)
16
 
          raise ArgumentError, "Invalid value provided for #{symbol}"
17
 
        end
18
 
        instance_variable_set("@#{symbol}", val)
19
 
      end
20
 
      made << "#{symbol}=".to_sym
21
 
    end
22
 
    made
23
 
  end
24
 
 
25
 
  # Create aliases for validators.
26
 
  #
27
 
  # NOTE: This is not (presently) a common core extension and is not
28
 
  # loaded automatically when using <code>require 'facets'</code>.
29
 
 
30
 
  def alias_validator(*args)
31
 
    orig = args.last
32
 
    args = args - [orig]
33
 
    args.each do |name|
34
 
      #alias_method(name, orig)
35
 
      alias_method("#{name}=", "#{orig}=")
36
 
    end
37
 
  end
38
 
 
39
 
end
40