Module: CustomValidations
- Defined in:
- lib/vex/active_record/custom_validations.rb
Defined Under Namespace
Modules: Etest
Class Method Summary collapse
-
.invalid_parsed?(value, opts, &block) ⇒ Boolean
custom pared.
- .invalid_url?(url, opts = {}) ⇒ Boolean
Instance Method Summary collapse
- #validates_float(*args) ⇒ Object
- #validates_integer(*args) ⇒ Object
- #validates_parsed(*args, &block) ⇒ Object
- #validates_texta(*args) ⇒ Object
- #validates_url(*args) ⇒ Object
Class Method Details
.invalid_parsed?(value, opts, &block) ⇒ Boolean
custom pared.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/vex/active_record/custom_validations.rb', line 39 def self.invalid_parsed?(value, opts, &block) return if value.nil? && opts[:allow_nil] begin value = yield(value) rescue return $!.to_s end return "must be in #{opts[:in].inspect}" if opts[:in] && !opts[:in].include?(value) return "Must be greater than #{opts[:gt].inspect}" if opts[:gt] && !(value > opts[:gt]) return "Must be >= #{opts[:ge].inspect}" if opts[:ge] && !(value >= opts[:ge]) return "Must be less than #{opts[:gt].inspect}" if opts[:lt] && !(value < opts[:lt]) return "Must be <= #{opts[:ge].inspect}" if opts[:le] && !(value <= opts[:le]) end |
.invalid_url?(url, opts = {}) ⇒ Boolean
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/vex/active_record/custom_validations.rb', line 15 def self.invalid_url?(url, opts = {}) return if url.respond_to?(:blank?) && url.blank? && opts[:allow_nil] uri = url.is_a?(URI) ? url : URI.parse(url) if !uri.scheme return "Missing URI protocol" end schemes = [ opts[:scheme] || %w(http https) ].flatten.compact return if schemes.include?(:any) || schemes.empty? if !schemes.include?(uri.scheme) return "Unsupported protocol #{uri.scheme.inspect}, must be one of #{schemes.join(", ")}" end rescue URI::InvalidURIError return $!.to_s end |
Instance Method Details
#validates_float(*args) ⇒ Object
79 80 81 |
# File 'lib/vex/active_record/custom_validations.rb', line 79 def validates_float(*args) validates_parsed(*args) { |value| Float(value) } end |
#validates_integer(*args) ⇒ Object
75 76 77 |
# File 'lib/vex/active_record/custom_validations.rb', line 75 def validates_integer(*args) validates_parsed(*args) { |value| Integer(value) } end |
#validates_parsed(*args, &block) ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/vex/active_record/custom_validations.rb', line 55 def validates_parsed(*args, &block) opts = args. validates_each(args, opts) do |r, attr, value| msg = CustomValidations.invalid_parsed?(value, opts, &block) r.errors.add attr, msg if msg !msg end end |
#validates_texta(*args) ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/vex/active_record/custom_validations.rb', line 65 def validates_texta(*args) validates_parsed(*args) { |value| begin value.texta rescue Texta::Error raise "I do not understand: '#{$!.to_s}'" end } end |
#validates_url(*args) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 |
# File 'lib/vex/active_record/custom_validations.rb', line 2 def validates_url(*args) opts = { :scheme => %w(http https) }.update(args.) validates_each(args, opts) do |r, attr, value| next unless msg = CustomValidations.invalid_url?(value, opts) r.errors.add(attr, msg) false end end |