validates_uri_format_of 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +21 -2
  2. data/init.rb +1 -1
  3. data/lib/validates_uri_format_of.rb +31 -3
  4. metadata +12 -5
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # validates\_uri\_format\_of
1
+ # validates_uri_format_of
2
2
 
3
3
  Rails plugin that provides a `validates_uri_format_of` method to `ActiveRecord` models.
4
4
  You can require or disallow certain components of a URI.
@@ -22,7 +22,26 @@ After installing the plugin, it's used like
22
22
 
23
23
  ## Features
24
24
 
25
- Take a look at the lib/ and test/ directories.
25
+ * `:schemes` allowed URI schemes. Default: `['http','https']`
26
+ * `:require_scheme` – Default: `true`
27
+ * `:require_host` – Default: `true`
28
+ * `:require_path` – Default: `true`
29
+ * `:require_fqdn` – enforce a fully qualified domain name? Default: `nil`
30
+ * `:with_port` – Default: `nil`
31
+ * `:with_query` – `true` requires a query string (e.g. '?query'), `false` does not allow a query string. Default: `nil` (no check)
32
+ * `:with_fragment` – `true` requires a fragment string (e.g. '#fragment'), `false` does not allow a fragment string. Default: `nil` (no check)
33
+ * `:with_auth` – `true` requires authentication info (e.g. 'http://user:pass@example.com/'), `false` does not allow a fragment string. Default: `nil` (no check)
34
+ * `:on` – Specifies when this validation is active (default is `:save`, other options `:create`, `:update`).
35
+ * `:if` – Specifies a method, proc or string to call to determine if the validation should
36
+ occur (e.g. `:if => :allow_validation`, or `:if => Proc.new { |user| user.signup_step > 2 }`).
37
+ The method, proc or string should return or evaluate to a true or false value.
38
+ * `:unless` – Specifies a method, proc or string to call to determine if the validation should
39
+ not occur (e.g. `:unless => :skip_validation`, or `:unless => Proc.new { |user| user.signup_step <= 2 }`).
40
+ The method, proc or string should return or evaluate to a true or false value.
41
+
42
+
43
+
44
+ Do also a look at the lib/ and test/ directories.
26
45
 
27
46
  ## Credits and license
28
47
 
data/init.rb CHANGED
@@ -1 +1 @@
1
- require 'lib/validates_uri_format_of'
1
+ require 'validates_uri_format_of'
@@ -43,6 +43,28 @@ module ValidatesUriFormatOf
43
43
  :on => :save
44
44
  }
45
45
 
46
+ # Merken, welche Optionen überhaupt bereitgestellt werden.
47
+ # Benötigen wir, um Exceptions zu schmeißen, falls unbekannte
48
+ # Optionen übergeben werden.
49
+ KNOWN_OPTIONS = {
50
+ # Eigene Optionen -- Wird weiter unten in einer Schleife ergänzt
51
+ :schemes => true,
52
+ :require_fqdn => true,
53
+ :message => true,
54
+
55
+ # Optionen von validates_each
56
+ :on => true,
57
+ :allow_nil => true,
58
+ :allow_blank => true,
59
+ :if => true,
60
+ :unless => true
61
+ }
62
+ for type_key, type_hash in ValidatesUriFormatOf::COMPONENTS
63
+ for opt_key, opt_value in type_hash
64
+ KNOWN_OPTIONS[:"#{type_key}_#{opt_key}"] = true
65
+ end
66
+ end
67
+
46
68
 
47
69
  def validates_uri_format_of(*attr_names)
48
70
  configuration = {
@@ -50,12 +72,18 @@ module ValidatesUriFormatOf
50
72
  }.merge(DEFAULT_CONFIGURATION)
51
73
 
52
74
  configuration.update(attr_names.extract_options!)
53
- #validates_each(attr_names, configuration) do |record, attr_name, value|
54
- validates_each(attr_names) do |record, attr_name, value|
75
+
76
+ for opt in configuration.keys
77
+ raise ArgumentError, "unknown option :#{opt}" unless KNOWN_OPTIONS[opt]
78
+ end
79
+
80
+ # Übergebene Parameter rauspicken, die wir direkt validates_each übergeben können.
81
+ validates_each_configuration = Hash[configuration.select{|k,v| [:on, :allow_nil, :allow_blank, :if, :unless].include?(k) }]
82
+ validates_each(attr_names, validates_each_configuration) do |record, attr_name, value|
55
83
  begin
56
84
  raise(URI::InvalidURIError, "cannot be a #{value.class}") unless [NilClass, String].include?(value.class)
57
85
 
58
- for meth in [:nil, :empty]
86
+ for meth in [:nil, :empty, :blank]
59
87
  if value.send("#{meth}?")
60
88
  if configuration[:"allow_#{meth}"]
61
89
  next
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validates_uri_format_of
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
4
+ hash: 23
5
+ prerelease:
5
6
  segments:
6
7
  - 0
7
- - 1
8
- - 1
9
- version: 0.1.1
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
10
11
  platform: ruby
11
12
  authors:
12
13
  - Julian Kornberger
@@ -22,9 +23,11 @@ dependencies:
22
23
  name: activerecord
23
24
  prerelease: false
24
25
  requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
25
27
  requirements:
26
28
  - - ">="
27
29
  - !ruby/object:Gem::Version
30
+ hash: 5
28
31
  segments:
29
32
  - 2
30
33
  - 3
@@ -56,23 +59,27 @@ rdoc_options:
56
59
  require_paths:
57
60
  - lib
58
61
  required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
59
63
  requirements:
60
64
  - - ">="
61
65
  - !ruby/object:Gem::Version
66
+ hash: 3
62
67
  segments:
63
68
  - 0
64
69
  version: "0"
65
70
  required_rubygems_version: !ruby/object:Gem::Requirement
71
+ none: false
66
72
  requirements:
67
73
  - - ">="
68
74
  - !ruby/object:Gem::Version
75
+ hash: 3
69
76
  segments:
70
77
  - 0
71
78
  version: "0"
72
79
  requirements:
73
80
  - :"rmagick libqt4-ruby libqt4-webkit"
74
81
  rubyforge_project:
75
- rubygems_version: 1.3.6
82
+ rubygems_version: 1.4.1
76
83
  signing_key:
77
84
  specification_version: 3
78
85
  summary: URL Validation for ActiveRecord models.