Module: Zen::Validation
- Included in:
- Settings::Setting, Settings::SettingsGroup, Language, Language::Translation, Package, Theme
- Defined in:
- lib/zen/validation.rb
Overview
The Validation module is a very basic validation framework that's used by various internal modules/classes such as Zen::Plugin and Zen::Package.
Usage
Using the module is pretty simple. Include it, specify the validation rules in a method and call it. All official modules and classes use a method called "validate" but you're free to name it whatever you want. A basic example looks like the following:
class Something
include Zen::Validation
attr_accessor :name
def validate
validates_presence(:name)
end
end
Instance Method Summary (collapse)
-
- (Object) validates_filepath(attribute)
Checks if the specified attribute contains a valid filepath.
-
- (Object) validates_format(attribute, regexp = nil)
Checks if the given attributes match the specified regular expressions.
-
- (Object) validates_length(attribute, options)
Checks if the length of a string matches the given length.
-
- (Object) validates_presence(attributes)
Checks if the specified attributes exist and aren't set to nil.
Instance Method Details
- (Object) validates_filepath(attribute)
Checks if the specified attribute contains a valid filepath.
135 136 137 138 139 140 141 142 143 144 |
# File 'lib/zen/validation.rb', line 135 def validates_filepath(attribute) path = send(attribute) if !File.exist?(path) raise( ValidationError, "The path #{path} in \"#{attribute}\" doesn't exist." ) end end |
- (Object) validates_format(attribute, regexp = nil)
Checks if the given attributes match the specified regular expressions. When a hash is specified the keys should be the names of the attributes to validate and the values the regular expressions to use.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/zen/validation.rb', line 106 def validates_format(attribute, regexp = nil) if attribute.class != Hash attribute = {attribute => regexp} end # Try to match all attributes attribute.each do |attr, regexp| val = send(attr) match = val =~ regexp if !match raise( ValidationError, "The attribute \"#{attr}\" doesn't match #{regexp}" ) end end end |
- (Object) validates_length(attribute, options)
Checks if the length of a string matches the given length. You can specify a minimum length, a maximum one as well as both.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/zen/validation.rb', line 66 def validates_length(attribute, ) value = send(attribute) if !value.respond_to?(:length) raise( ValidationError, "The length of \"#{attribute}\" can't be checked as the method " + \ "\"length\" doesn't exist." ) end # Time to validate the length length = value.length if .key?(:min) and length < [:min] raise(ValidationError, "The attribute \"#{attribute}\" is too short.") end if .key?(:max) and length > [:max] raise(ValidationError, "The attribute \"#{attribute}\" is too long.") end end |
- (Object) validates_presence(attributes)
Checks if the specified attributes exist and aren't set to nil.
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/zen/validation.rb', line 38 def validates_presence(attributes) if attributes.class != Array attributes = [attributes] end attributes.each do |f| if !respond_to?(f) or send(f).nil? raise(ValidationError, "The attribute \"#{f}\" doesn't exist.") end end end |