waw 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENCE.rdoc +25 -0
- data/README.rdoc +32 -0
- data/bin/waw +32 -0
- data/bin/waw-profile +26 -0
- data/bin/waw-start +26 -0
- data/bin/wspec +33 -0
- data/layouts/empty/Rakefile +14 -0
- data/layouts/empty/config.ru +5 -0
- data/layouts/empty/config/commons.cfg +31 -0
- data/layouts/empty/config/devel.cfg +21 -0
- data/layouts/empty/ignore +7 -0
- data/layouts/empty/logs/dontforgetme +0 -0
- data/layouts/empty/resources/messages.rs +1 -0
- data/layouts/empty/test/wspec/site_respond.wspec +3 -0
- data/layouts/empty/test/wspec/test_all.rb +13 -0
- data/layouts/empty/waw.deploy +1 -0
- data/layouts/empty/waw.routing +6 -0
- data/layouts/static/dependencies +1 -0
- data/layouts/static/public/.wawaccess +18 -0
- data/layouts/static/public/css/style.css +0 -0
- data/layouts/static/public/images/dontforgetme +0 -0
- data/layouts/static/public/js/project.js +0 -0
- data/layouts/static/public/pages/.wawaccess +30 -0
- data/layouts/static/public/pages/404.wtpl +1 -0
- data/layouts/static/public/pages/index.wtpl +5 -0
- data/layouts/static/public/templates/.wawaccess +9 -0
- data/layouts/static/public/templates/layout.wtpl +17 -0
- data/layouts/static/test/wspec/static_pages_are_served.wspec +21 -0
- data/layouts/static/waw.routing +8 -0
- data/lib/waw.rb +99 -0
- data/lib/waw/commands/command.rb +115 -0
- data/lib/waw/commands/profile_command.rb +66 -0
- data/lib/waw/commands/start_command.rb +59 -0
- data/lib/waw/config.rb +110 -0
- data/lib/waw/controller.rb +25 -0
- data/lib/waw/controllers/action/action.rb +91 -0
- data/lib/waw/controllers/action/action_utils.rb +30 -0
- data/lib/waw/controllers/action/js_generation.rb +116 -0
- data/lib/waw/controllers/action_controller.rb +133 -0
- data/lib/waw/controllers/error/backtrace.rb +54 -0
- data/lib/waw/controllers/error_handler.rb +62 -0
- data/lib/waw/controllers/json_controller.rb +31 -0
- data/lib/waw/controllers/no_cache.rb +22 -0
- data/lib/waw/controllers/static/match.rb +80 -0
- data/lib/waw/controllers/static/waw_access.rb +235 -0
- data/lib/waw/controllers/static/waw_access_dsl.rb +48 -0
- data/lib/waw/controllers/static_controller.rb +37 -0
- data/lib/waw/default_config.cfg +14 -0
- data/lib/waw/environment_utils.rb +57 -0
- data/lib/waw/errors.rb +4 -0
- data/lib/waw/ext.rb +3 -0
- data/lib/waw/ext/core.rb +4 -0
- data/lib/waw/ext/core/hash.rb +47 -0
- data/lib/waw/ext/core/logger.rb +10 -0
- data/lib/waw/ext/core/module.rb +20 -0
- data/lib/waw/ext/core/object.rb +29 -0
- data/lib/waw/ext/rack.rb +19 -0
- data/lib/waw/ext/rack/builder.rb +43 -0
- data/lib/waw/ext/rack/delegator.rb +51 -0
- data/lib/waw/ext/rack/urlmap.rb +55 -0
- data/lib/waw/ext/wlang.rb +1 -0
- data/lib/waw/ext/wlang/hosted_language.rb +21 -0
- data/lib/waw/fullstate.rb +8 -0
- data/lib/waw/fullstate/on_class.rb +37 -0
- data/lib/waw/fullstate/on_instance.rb +27 -0
- data/lib/waw/fullstate/variable.rb +36 -0
- data/lib/waw/kern.rb +6 -0
- data/lib/waw/kern/app.rb +48 -0
- data/lib/waw/kern/empty/waw.deploy +0 -0
- data/lib/waw/kern/empty/waw.routing +1 -0
- data/lib/waw/kern/freezed_state.rb +32 -0
- data/lib/waw/kern/hooks.rb +53 -0
- data/lib/waw/kern/lifecycle.rb +248 -0
- data/lib/waw/kern/living_state.rb +87 -0
- data/lib/waw/kern/utils.rb +27 -0
- data/lib/waw/resource_collection.rb +100 -0
- data/lib/waw/restart.rb +32 -0
- data/lib/waw/routing.rb +43 -0
- data/lib/waw/routing/action_routing.rb +78 -0
- data/lib/waw/routing/dsl.rb +45 -0
- data/lib/waw/routing/feedback.rb +23 -0
- data/lib/waw/routing/form_validation_feedback.rb +36 -0
- data/lib/waw/routing/javascript.rb +17 -0
- data/lib/waw/routing/redirect.rb +26 -0
- data/lib/waw/routing/refresh.rb +17 -0
- data/lib/waw/routing/routing_rule.rb +16 -0
- data/lib/waw/scope_utils.rb +69 -0
- data/lib/waw/session.rb +51 -0
- data/lib/waw/testing.rb +1 -0
- data/lib/waw/tools/mail.rb +4 -0
- data/lib/waw/tools/mail/mail.rb +119 -0
- data/lib/waw/tools/mail/mail_agent.rb +123 -0
- data/lib/waw/tools/mail/mailbox.rb +62 -0
- data/lib/waw/tools/mail/template.rb +38 -0
- data/lib/waw/utils/dsl_helper.rb +116 -0
- data/lib/waw/validation.rb +175 -0
- data/lib/waw/validation/and_validator.rb +27 -0
- data/lib/waw/validation/array_validations.rb +38 -0
- data/lib/waw/validation/boolean_validator.rb +32 -0
- data/lib/waw/validation/comparison_validations.rb +45 -0
- data/lib/waw/validation/date_validator.rb +31 -0
- data/lib/waw/validation/default_validator.rb +30 -0
- data/lib/waw/validation/dsl_ruby_extensions.rb +11 -0
- data/lib/waw/validation/errors.rb +17 -0
- data/lib/waw/validation/ext.rb +3 -0
- data/lib/waw/validation/file_validator.rb +30 -0
- data/lib/waw/validation/float_validator.rb +19 -0
- data/lib/waw/validation/helpers.rb +67 -0
- data/lib/waw/validation/integer_validator.rb +16 -0
- data/lib/waw/validation/isin_validator.rb +24 -0
- data/lib/waw/validation/mandatory_validator.rb +17 -0
- data/lib/waw/validation/missing_validator.rb +17 -0
- data/lib/waw/validation/not_validator.rb +20 -0
- data/lib/waw/validation/or_validator.rb +34 -0
- data/lib/waw/validation/regexp_validator.rb +29 -0
- data/lib/waw/validation/same_validator.rb +16 -0
- data/lib/waw/validation/signature.rb +157 -0
- data/lib/waw/validation/size_validations.rb +44 -0
- data/lib/waw/validation/string_validator.rb +15 -0
- data/lib/waw/validation/validator.rb +48 -0
- data/lib/waw/wawgen.rb +2 -0
- data/lib/waw/wawgen/create.rb +166 -0
- data/lib/waw/wawgen/project.rb +25 -0
- data/lib/waw/wspec.rb +5 -0
- data/lib/waw/wspec/browser.rb +240 -0
- data/lib/waw/wspec/dsl.rb +201 -0
- data/lib/waw/wspec/html_analysis.rb +136 -0
- data/lib/waw/wspec/html_analysis/tag.rb +56 -0
- data/lib/waw/wspec/runner.rb +70 -0
- data/lib/waw/wspec/scenario.rb +35 -0
- data/lib/waw/wspec/suite.rb +54 -0
- data/test/bricks/error_handler/config/test.cfg +2 -0
- data/test/bricks/error_handler/logs/webapp.log +1411 -0
- data/test/bricks/error_handler/test/error_handler.wspec +16 -0
- data/test/bricks/error_handler/waw.deploy +1 -0
- data/test/bricks/error_handler/waw.routing +27 -0
- data/test/integration/waw_create_integration_test.rb +24 -0
- data/test/spec/assumptions_spec.rb +30 -0
- data/test/spec/controllers/action_controller_spec.rb +14 -0
- data/test/spec/controllers/static/waw_access_spec.rb +112 -0
- data/test/spec/environment_utils_spec.rb +15 -0
- data/test/spec/ext/core/hash_spec.rb +58 -0
- data/test/spec/fixtures.rb +41 -0
- data/test/spec/fixtures/action/config/default.cfg +2 -0
- data/test/spec/fixtures/action/lib/action_controller_test.rb +12 -0
- data/test/spec/fixtures/action/waw.deploy +1 -0
- data/test/spec/fixtures/action/waw.routing +6 -0
- data/test/spec/fixtures/empty/waw.deploy +0 -0
- data/test/spec/fixtures/empty/waw.routing +0 -0
- data/test/spec/fullstate/on_class_spec.rb +59 -0
- data/test/spec/fullstate/on_instance_spec.rb +59 -0
- data/test/spec/fullstate/session_spec.rb +43 -0
- data/test/spec/fullstate/variable_spec.rb +55 -0
- data/test/spec/resource_collection_spec.rb +50 -0
- data/test/spec/test_all.rb +9 -0
- data/test/spec/tools/mail/mail_agent_spec.rb +116 -0
- data/test/spec/tools/mail/mail_spec.rb +56 -0
- data/test/spec/tools/mail/mailbox_spec.rb +57 -0
- data/test/spec/tools/mail/template_spec.rb +47 -0
- data/test/spec/validation/array_validation_spec.rb +63 -0
- data/test/spec/validation/array_validator_spec.rb +17 -0
- data/test/spec/validation/date_validation_spec.rb +35 -0
- data/test/spec/validation/default_validation_spec.rb +37 -0
- data/test/spec/validation/disjuctive_validation_spec.rb +33 -0
- data/test/spec/validation/errors_spec.rb +37 -0
- data/test/spec/validation/file_validator_spec.rb +34 -0
- data/test/spec/validation/mail_validation_spec.rb +51 -0
- data/test/spec/validation/missing_validation_spec.rb +43 -0
- data/test/spec/validation/same_validation_spec.rb +24 -0
- data/test/spec/validation/signature_intuition_spec.rb +37 -0
- data/test/spec/validation/signature_spec.rb +164 -0
- data/test/spec/validation/validation_spec.rb +28 -0
- data/test/spec/wspec/html_analysis/tag_spec.rb +38 -0
- data/test/spec/wspec/html_analysis_spec.rb +170 -0
- data/test/unit/test_all.rb +8 -0
- data/test/unit/waw/app_test.rb +126 -0
- data/test/unit/waw/app_test/config/commons.cfg +2 -0
- data/test/unit/waw/app_test/config/devel.cfg +1 -0
- data/test/unit/waw/config_test.rb +54 -0
- data/test/unit/waw/controllers/action_controller_test.rb +76 -0
- data/test/unit/waw/controllers/action_test.rb +35 -0
- data/test/unit/waw/controllers/example_action_controller_test.rb +24 -0
- data/test/unit/waw/controllers/multiple_action_controller_test.rb +78 -0
- data/test/unit/waw/controllers/static/example/css/example.css +1 -0
- data/test/unit/waw/controllers/static/example/index.html +1 -0
- data/test/unit/waw/controllers/static/example/js/example.js +1 -0
- data/test/unit/waw/controllers/static/example/pages/hello.wtpl +1 -0
- data/test/unit/waw/controllers/static/waw_access_test.rb +76 -0
- data/test/unit/waw/ext/rack_test.rb +74 -0
- data/test/unit/waw/resource_collection_test.rb +49 -0
- data/test/unit/waw/resources.txt +4 -0
- data/test/unit/waw/routing/routing_test.rb +26 -0
- data/test/unit/waw/utils/dsl_helper_test.rb +79 -0
- data/test/unit/waw/utils/dsl_helper_test_extensions1.rb +4 -0
- data/test/unit/waw/validation/signature_test.rb +193 -0
- data/test/unit/waw/validation_test.rb +319 -0
- data/test/unit/waw/wspec/html_analysis_test.html +81 -0
- data/test/unit/waw/wspec/html_analysis_test.rb +26 -0
- metadata +272 -0
@@ -0,0 +1,38 @@
|
|
1
|
+
module Waw
|
2
|
+
module Tools
|
3
|
+
class MailAgent
|
4
|
+
# Allows creating WLang templates for mails easily
|
5
|
+
class Template < Mail
|
6
|
+
|
7
|
+
# The wlang dialect
|
8
|
+
attr_writer :dialect
|
9
|
+
|
10
|
+
# Returns the wlang dialect to use
|
11
|
+
def dialect
|
12
|
+
return @dialect if @dialect
|
13
|
+
case content_type
|
14
|
+
when 'text/html'
|
15
|
+
'wlang/xhtml'
|
16
|
+
when 'text/plain'
|
17
|
+
'wlang/active-string'
|
18
|
+
else
|
19
|
+
Waw.logger.warn("Unable to get wlang dialect from content-type #{content_type}")
|
20
|
+
Waw.logger.warn("Using wlang/active-string by default")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# Instantiates this template using a given wlang context.
|
25
|
+
# The date of the resulting mail is set to Time.now
|
26
|
+
def instantiate(context = {})
|
27
|
+
mail, args = self.dup, context.unsymbolize_keys
|
28
|
+
mail.subject = mail.subject.wlang(args, dialect)
|
29
|
+
mail.body = mail.body.wlang(args, dialect)
|
30
|
+
mail.date = Time.now
|
31
|
+
mail
|
32
|
+
end
|
33
|
+
alias :to_mail :instantiate
|
34
|
+
|
35
|
+
end
|
36
|
+
end # class MailAgent
|
37
|
+
end # module Tools
|
38
|
+
end # module Waw
|
@@ -0,0 +1,116 @@
|
|
1
|
+
#
|
2
|
+
# This file is extracted from the CRUC library (http://code.chefbe.net)
|
3
|
+
# which is distributed under the licence below. Please keep this header
|
4
|
+
# when using this class. Send an email to blambeau at gmail dot com if
|
5
|
+
# you need special usage rights.
|
6
|
+
#
|
7
|
+
# The MIT License
|
8
|
+
# Copyright (c) 2009 Bernard Lambeau and the University of Louvain
|
9
|
+
# (Universite Catholique de Louvain, Louvain-la-Neuve, Belgium)
|
10
|
+
#
|
11
|
+
|
12
|
+
#
|
13
|
+
# Provides a simple but powerful way to create non intrusive DSLs.
|
14
|
+
# Typical use cases are described below.
|
15
|
+
#
|
16
|
+
# === Punctual DSL usage
|
17
|
+
# A given user block uses your DSL punctually. As the later installs
|
18
|
+
# potentially intrusive methods in some Ruby core classes, you would
|
19
|
+
# like your DSL to be installed, user block executed and Ruby methods
|
20
|
+
# restored immediately after.
|
21
|
+
#
|
22
|
+
# DSLHelper.new(String => [:upcase], Symbol => [:/, :&, :|]) do
|
23
|
+
# load 'mydsl.rb' # install your DSL first
|
24
|
+
# yield if block_given? # yield user block
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# After the DSLHelper new invocation, all Ruby methods are correctly
|
28
|
+
# restored.
|
29
|
+
#
|
30
|
+
# === Middle-case DSL usage
|
31
|
+
# Your DSL must be installed punctually but you cannot encapsulate its
|
32
|
+
# usage in a single DSLHelper.new invocation. Calling save and restore
|
33
|
+
# methods is the alternative. However, their invocation MUST respect
|
34
|
+
# (save restore)* invocation regexp otherwise a RuntimeError is raised.
|
35
|
+
#
|
36
|
+
# helper = DSLHelper.new(String => [:upcase], Symbol => [:/, :&, :|])
|
37
|
+
# helper.save # save Ruby methods that you plan to override
|
38
|
+
# load 'mydsl.rb' # install your DSL
|
39
|
+
# [...] # do anything you want, here of somewhere
|
40
|
+
# helper.restore # restore Ruby methods when finished
|
41
|
+
#
|
42
|
+
# === Long-term DSL usage
|
43
|
+
# Your DSL (or more generically, ruby extensions you've written) is used during
|
44
|
+
# the whole execution of the program. You hope that it is not intrusive (you only
|
45
|
+
# add methods that doesn't already exist to some Ruby classes). You would like to
|
46
|
+
# check this hypothesis (because Ruby evolves, your code may be used in conjunction
|
47
|
+
# with third party libraries that install their own methods, etc.)
|
48
|
+
#
|
49
|
+
# if DSLHelper.is_intrusive?(String => [:upcase], Symbol => [:/, :&, :|])
|
50
|
+
# STDERR << "WARNING: this library seems to override Ruby existing methods"
|
51
|
+
# end
|
52
|
+
# load 'ruby_extensions.rb'
|
53
|
+
#
|
54
|
+
class DSLHelper
|
55
|
+
|
56
|
+
# Creates a DSLHelper instance. _hash_ must be a Hash instance
|
57
|
+
# mapping modules to array of symbols. If a block is given, the
|
58
|
+
# Ruby state is immediately saved, block is yield and state is
|
59
|
+
# restored after that.
|
60
|
+
def initialize(hash)
|
61
|
+
@definition = hash
|
62
|
+
@saved = nil
|
63
|
+
if block_given?
|
64
|
+
save
|
65
|
+
yield
|
66
|
+
restore
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# Checks if a set of methods that your DSL is planning to install
|
71
|
+
# makes it an intrusive DSL (overriding existing Ruby or third
|
72
|
+
# party methods)
|
73
|
+
def self.is_intrusive?(hash)
|
74
|
+
hash.each_pair do |mod, methods|
|
75
|
+
methods.each do |method|
|
76
|
+
return true if mod.method_defined?(method)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
false
|
80
|
+
end
|
81
|
+
|
82
|
+
# Finds a method inside a Module, or returns nil.
|
83
|
+
def self.find_instance_method(mod, method)
|
84
|
+
mod.instance_method(method)
|
85
|
+
rescue NameError
|
86
|
+
nil
|
87
|
+
end
|
88
|
+
|
89
|
+
# Saves all methods of the definition hash in an internal data
|
90
|
+
# structure. This method raises an error is a save is already
|
91
|
+
# pending.
|
92
|
+
def save
|
93
|
+
raise "save already pending" unless @saved.nil?
|
94
|
+
@saved = {}
|
95
|
+
@definition.each_pair do |mod, methods|
|
96
|
+
@saved[mod] = methods.collect {|m| DSLHelper.find_instance_method(mod,m)}
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
# Restores all methods previously saved. This method raises an
|
101
|
+
# error if no save call has been made before.
|
102
|
+
def restore
|
103
|
+
raise "save has not been called previously." if @saved.nil?
|
104
|
+
@definition.each_pair do |mod, methods|
|
105
|
+
methods.zip(@saved[mod]).each do |name, saved|
|
106
|
+
if saved.nil?
|
107
|
+
mod.send(:remove_method, name) if mod.method_defined?(name)
|
108
|
+
elsif
|
109
|
+
mod.send(:define_method, name, saved)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
@saved = nil
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
@@ -0,0 +1,175 @@
|
|
1
|
+
require 'waw/validation/errors'
|
2
|
+
require 'waw/validation/helpers'
|
3
|
+
require 'waw/validation/validator'
|
4
|
+
require 'waw/validation/not_validator'
|
5
|
+
require 'waw/validation/and_validator'
|
6
|
+
require 'waw/validation/or_validator'
|
7
|
+
|
8
|
+
require 'waw/validation/missing_validator'
|
9
|
+
require 'waw/validation/mandatory_validator'
|
10
|
+
require 'waw/validation/default_validator'
|
11
|
+
require 'waw/validation/same_validator'
|
12
|
+
require 'waw/validation/isin_validator'
|
13
|
+
|
14
|
+
require 'date'
|
15
|
+
require 'waw/validation/boolean_validator'
|
16
|
+
require 'waw/validation/string_validator'
|
17
|
+
require 'waw/validation/integer_validator'
|
18
|
+
require 'waw/validation/float_validator'
|
19
|
+
require 'waw/validation/date_validator'
|
20
|
+
require 'waw/validation/regexp_validator'
|
21
|
+
|
22
|
+
require 'waw/validation/comparison_validations'
|
23
|
+
require 'waw/validation/size_validations'
|
24
|
+
require 'waw/validation/array_validations'
|
25
|
+
require 'waw/validation/file_validator'
|
26
|
+
require 'waw/validation/ext'
|
27
|
+
require 'waw/validation/signature'
|
28
|
+
module Waw
|
29
|
+
#
|
30
|
+
# Provides a reusable architecture for parameter validation
|
31
|
+
#
|
32
|
+
module Validation
|
33
|
+
extend ::Waw::Validation::Helpers
|
34
|
+
|
35
|
+
# Validators, by name
|
36
|
+
@@validators = {}
|
37
|
+
|
38
|
+
# Ruby classes => validators
|
39
|
+
@@ruby_classes_to_validators = {}
|
40
|
+
|
41
|
+
# Other modules that include me
|
42
|
+
@@who_includes_me = []
|
43
|
+
|
44
|
+
# Allows this module to be included as a normal one, hiding
|
45
|
+
# introspection mechanisms to install validators
|
46
|
+
class << self
|
47
|
+
|
48
|
+
# When this module is included in _who_ we install all validation
|
49
|
+
# methods dynamically
|
50
|
+
def included(who)
|
51
|
+
@@who_includes_me << who unless @@who_includes_me.include?(who)
|
52
|
+
@@validators.each_pair do |name, validator|
|
53
|
+
who.module_eval <<-EOF
|
54
|
+
def #{name}(*args, &block)
|
55
|
+
::Waw::Validation.send(:#{name}, *args, &block)
|
56
|
+
end
|
57
|
+
EOF
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end # Metaprogramming hack for module inclusion
|
62
|
+
|
63
|
+
|
64
|
+
# Helpers for extensions
|
65
|
+
class << self
|
66
|
+
|
67
|
+
# Builds a signature with a given block as definition
|
68
|
+
def signature(&block)
|
69
|
+
Signature.new(&block)
|
70
|
+
end
|
71
|
+
|
72
|
+
# Adds a ruby class => validator mapping
|
73
|
+
def ruby_class_to_validator(ruby_class, validator)
|
74
|
+
@@ruby_classes_to_validators[ruby_class] = validator
|
75
|
+
end
|
76
|
+
|
77
|
+
# Returns a validator to use for a given ruby class
|
78
|
+
def validator_for_ruby_class(ruby_class, raise_if_not_found=true)
|
79
|
+
val = @@ruby_classes_to_validators[ruby_class]
|
80
|
+
return val unless val.nil?
|
81
|
+
raise "Unable to find a validator for #{ruby_class}" if raise_if_not_found
|
82
|
+
end
|
83
|
+
|
84
|
+
# Automatically builds the validator and installs a method returning
|
85
|
+
# it.
|
86
|
+
def validator(name = nil, *args, &block)
|
87
|
+
# Build the validator
|
88
|
+
validator = args[0]
|
89
|
+
if validator.nil? and block
|
90
|
+
# first case, block creation
|
91
|
+
validator = Validator.new(&block)
|
92
|
+
elsif ::Waw::Validation::Validator===validator and block.nil?
|
93
|
+
# first case: explicit validator, nothing to do
|
94
|
+
elsif validator.is_a?(::Class) and block.nil?
|
95
|
+
# second case: class, for defered creation
|
96
|
+
validator = to_validator(validator) unless validator.ancestors.include?(::Waw::Validation::Validator)
|
97
|
+
elsif ::Module===validator and block.nil?
|
98
|
+
# third case: module of validation rules, nothing to do
|
99
|
+
elsif block.nil?
|
100
|
+
# fifth case: auto conversion
|
101
|
+
validator = to_validator(validator)
|
102
|
+
else
|
103
|
+
raise WawError, "Invalid Waw::Validation.validator call with #{name}, #{args.inspect}, #{block.inspect}"
|
104
|
+
end
|
105
|
+
|
106
|
+
# Install it if a name is provided
|
107
|
+
return validator if name.nil?
|
108
|
+
@@validators[name] = validator
|
109
|
+
|
110
|
+
# Installs the class method that returns the validator
|
111
|
+
instance_eval <<-EOF
|
112
|
+
class << self
|
113
|
+
def #{name}(*args, &block)
|
114
|
+
val = @@validators[:#{name}]
|
115
|
+
val.is_a?(::Class) ? val.new(*args, &block) : val
|
116
|
+
end
|
117
|
+
end
|
118
|
+
EOF
|
119
|
+
|
120
|
+
# Install the class methods on all modules that include me
|
121
|
+
@@who_includes_me.each do |mod|
|
122
|
+
mod.module_eval <<-EOF
|
123
|
+
def #{name}(*args, &block)
|
124
|
+
::Waw::Validation.send(:#{name}, *args, &block)
|
125
|
+
end
|
126
|
+
EOF
|
127
|
+
end
|
128
|
+
|
129
|
+
validator
|
130
|
+
end
|
131
|
+
|
132
|
+
# User-friendly message for missing validators
|
133
|
+
def method_missing(name, *args, &block)
|
134
|
+
raise WawError, "Unknown validator #{name}", caller
|
135
|
+
end
|
136
|
+
|
137
|
+
end # Helpers for extensions
|
138
|
+
|
139
|
+
# General validators typically used for parameter validation
|
140
|
+
validator :always_accept, validator{|*args| true}
|
141
|
+
validator :missing, ::Waw::Validation::MissingValidator.new
|
142
|
+
validator :mandatory, ::Waw::Validation::MandatoryValidator.new
|
143
|
+
validator :default, ::Waw::Validation::DefaultValidator
|
144
|
+
validator :same, ::Waw::Validation::SameValidator.new
|
145
|
+
validator :equal, ::Waw::Validation::SameValidator.new
|
146
|
+
validator :isin, ::Waw::Validation::IsInValidator
|
147
|
+
|
148
|
+
# Type-based validators
|
149
|
+
validator :boolean, ::Waw::Validation::BooleanValidator.new
|
150
|
+
validator :string, ::Waw::Validation::StringValidator.new
|
151
|
+
validator :integer, ::Waw::Validation::IntegerValidator.new
|
152
|
+
validator :float, ::Waw::Validation::FloatValidator.new
|
153
|
+
validator :date, ::Waw::Validation::DateValidator.new
|
154
|
+
ruby_class_to_validator(::Boolean, boolean)
|
155
|
+
ruby_class_to_validator(::String, string)
|
156
|
+
ruby_class_to_validator(::Integer, integer)
|
157
|
+
ruby_class_to_validator(::Float, float)
|
158
|
+
ruby_class_to_validator(::Date, date)
|
159
|
+
|
160
|
+
# Regexp-based validators
|
161
|
+
validator :mail, /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]?@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/
|
162
|
+
validator :weburl, /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix
|
163
|
+
|
164
|
+
# Validators from other modules
|
165
|
+
Array = ::Waw::Validation::ArrayValidations
|
166
|
+
validator :array, ::Waw::Validation::ArrayValidations
|
167
|
+
validator :size, ::Waw::Validation::SizeValidations
|
168
|
+
validator :is, ::Waw::Validation::ComparisonValidations
|
169
|
+
|
170
|
+
# Validators about files and directories
|
171
|
+
validator :directory, validator{|*args| args.all?{|a| ::File.directory?(a)}}
|
172
|
+
validator :file, ::Waw::Validation::FileValidator
|
173
|
+
|
174
|
+
end # module Validation
|
175
|
+
end # module Waw
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Waw
|
2
|
+
module Validation
|
3
|
+
class AndValidator < Validator
|
4
|
+
|
5
|
+
# Empty constructor that overrides the top one
|
6
|
+
def initialize(*validators)
|
7
|
+
@validators = validators
|
8
|
+
end
|
9
|
+
|
10
|
+
# Calls the block installed at initialization time
|
11
|
+
def validate(*values)
|
12
|
+
@validators.all?{|validator| validator.validate(*values)}
|
13
|
+
end
|
14
|
+
|
15
|
+
# Converts and validate
|
16
|
+
def convert_and_validate(*values)
|
17
|
+
initials = values
|
18
|
+
@validators.each do |validator|
|
19
|
+
ok, values = validator.convert_and_validate(*values)
|
20
|
+
return [ok, initials] unless ok
|
21
|
+
end
|
22
|
+
[true, values]
|
23
|
+
end
|
24
|
+
|
25
|
+
end # class AndValidator
|
26
|
+
end # module Validation
|
27
|
+
end # module Waw
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Waw
|
2
|
+
module Validation
|
3
|
+
module ArrayValidations
|
4
|
+
class ArrayValidator < Validator
|
5
|
+
|
6
|
+
# Creates a validator instance with a delegator
|
7
|
+
def initialize(subvalidator)
|
8
|
+
@subvalidator = Waw::Validation.to_validator(subvalidator)
|
9
|
+
end
|
10
|
+
|
11
|
+
# Validation method
|
12
|
+
def validate(*values)
|
13
|
+
values.all?{|val| val.is_a?(::Array) and @subvalidator.validate(*val) }
|
14
|
+
end
|
15
|
+
|
16
|
+
# Convert and validate method
|
17
|
+
def convert_and_validate(*values)
|
18
|
+
my_converted = []
|
19
|
+
values.each do |val|
|
20
|
+
return [false, values] unless ::Array===val
|
21
|
+
ok, converted = @subvalidator.convert_and_validate(*val)
|
22
|
+
return [false, values] unless ok
|
23
|
+
my_converted << converted.compact
|
24
|
+
end
|
25
|
+
[true, my_converted]
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
module Methods
|
30
|
+
# Checks that all values of the array are validated by a subvalidator
|
31
|
+
def [](subvalidator)
|
32
|
+
ArrayValidator.new(subvalidator)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
extend Methods
|
36
|
+
end # module ArrayValidations
|
37
|
+
end # module Validation
|
38
|
+
end # module Waw
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Waw
|
2
|
+
module Validation
|
3
|
+
class BooleanValidator < Validator
|
4
|
+
|
5
|
+
def is_boolean?(value)
|
6
|
+
(value==true) || (value==false)
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_boolean(value)
|
10
|
+
return value if validate(value)
|
11
|
+
case value.to_s.strip
|
12
|
+
when 'true'
|
13
|
+
true
|
14
|
+
when 'false'
|
15
|
+
false
|
16
|
+
else
|
17
|
+
nil
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def validate(*values)
|
22
|
+
values.all?{|val| is_boolean?(val)}
|
23
|
+
end
|
24
|
+
|
25
|
+
def convert_and_validate(*values)
|
26
|
+
converted = values.collect{|v| to_boolean(v)}
|
27
|
+
validate(*converted) ? [true, converted] : [false, values]
|
28
|
+
end
|
29
|
+
|
30
|
+
end # class BooleanValidator
|
31
|
+
end # module Validation
|
32
|
+
end # module Waw
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Waw
|
2
|
+
module Validation
|
3
|
+
module ComparisonValidations
|
4
|
+
module Methods
|
5
|
+
|
6
|
+
# Builds a validator that verifies that the length is greater than
|
7
|
+
# a specified value
|
8
|
+
def >(value)
|
9
|
+
Validator.new {|*vals| Validation.argument_safe{ vals.all?{|val| val.respond_to?(:>) and val > value} }}
|
10
|
+
end
|
11
|
+
|
12
|
+
# Builds a validator that verifies that the length is greater-or-equal to
|
13
|
+
# a specified value
|
14
|
+
def >=(value)
|
15
|
+
Validator.new {|*vals| Validation.argument_safe{ vals.all?{|val| val.respond_to?(:>=) and val >= value} }}
|
16
|
+
end
|
17
|
+
|
18
|
+
# Builds a validator that verifies that the length is less than
|
19
|
+
# a specified value
|
20
|
+
def <(value)
|
21
|
+
Validator.new {|*vals| Validation.argument_safe{ vals.all?{|val| val.respond_to?(:<) and val < value} }}
|
22
|
+
end
|
23
|
+
|
24
|
+
# Builds a validator that verifies that the length is less-or-equal to
|
25
|
+
# a specified value
|
26
|
+
def <=(value)
|
27
|
+
Validator.new {|*vals| Validation.argument_safe{ vals.all?{|val| val.respond_to?(:<=) and val <= value} }}
|
28
|
+
end
|
29
|
+
|
30
|
+
# Builds a validator that verifies that the length is equal to a
|
31
|
+
# specified value
|
32
|
+
def ==(value)
|
33
|
+
Validator.new {|*vals| Validation.argument_safe{ vals.all?{|val| val.respond_to?(:==) and val == value} }}
|
34
|
+
end
|
35
|
+
|
36
|
+
# Same as Waw::Validation.isin
|
37
|
+
def in(*values)
|
38
|
+
Waw::Validation.isin(*values)
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
extend Methods
|
43
|
+
end # module ComparisonValidations
|
44
|
+
end # module Validation
|
45
|
+
end # module Waw
|