Class: ActiveForm::Validator::Base
- Defined in:
- lib/active_form/validators/base.rb
Instance Attribute Summary collapse
-
#code ⇒ Object
(also: #name)
Returns the value of attribute code.
-
#element ⇒ Object
readonly
Returns the value of attribute element.
-
#msg ⇒ Object
Returns the value of attribute msg.
Class Method Summary collapse
- .create(definition_name, &block) ⇒ Object
- .default_message=(str) ⇒ Object (also: default_message)
- .inherited(derivative) ⇒ Object
- .javascript_definition(identifier) ⇒ Object
- .javascript_validation(msg = 'validation failed', jscode = '') ⇒ Object
- .loadable_type ⇒ Object
- .message ⇒ Object
Instance Method Summary collapse
- #advice ⇒ Object
- #collection_length ⇒ Object
- #identifier ⇒ Object
-
#initialize(*args) {|_self| ... } ⇒ Base
constructor
A new instance of Base.
- #is_element?(arg) ⇒ Boolean
- #javascript_definition ⇒ Object
- #js_validation(validation_msg, code) ⇒ Object
- #label ⇒ Object
- #message ⇒ Object
- #message=(str) ⇒ Object
- #message_params ⇒ Object
- #register_element(elem) ⇒ Object
- #setup ⇒ Object
- #validate ⇒ Object
- #value ⇒ Object
- #value_length ⇒ Object
Constructor Details
permalink #initialize(*args) {|_self| ... } ⇒ Base
Returns a new instance of Base.
6 7 8 9 10 11 12 13 |
# File 'lib/active_form/validators/base.rb', line 6 def initialize(*args, &block) setup props = args.last.is_a?(Hash) ? args.pop : {} register_element(args.shift) if is_element?(args.first) self.code = self.class.to_s.demodulize.underscore props.each { |prop, value| send("#{prop}=", value) if respond_to?("#{prop}=") } yield self if block_given? end |
Instance Attribute Details
permalink #code ⇒ Object Also known as: name
Returns the value of attribute code.
4 5 6 |
# File 'lib/active_form/validators/base.rb', line 4 def code @code end |
Class Method Details
permalink .create(definition_name, &block) ⇒ Object
[View source]
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/active_form/validators/base.rb', line 107 def create(definition_name, &block) class_name = name_to_class_name(definition_name) if !ActiveForm::Validator.const_defined?(class_name) ActiveForm::Validator.const_set(class_name, Class.new(self)) if klass = ActiveForm::Validator.const_get(class_name) klass.module_eval(&block) if block_given? ActiveForm::Validator::register(klass) return klass end end nil end |
permalink .default_message=(str) ⇒ Object Also known as: default_message
[View source]
95 96 97 |
# File 'lib/active_form/validators/base.rb', line 95 def (str) self.default_msg = str end |
permalink .inherited(derivative) ⇒ Object
[View source]
100 101 102 103 104 105 |
# File 'lib/active_form/validators/base.rb', line 100 def inherited(derivative) ActiveForm::Validator::register(derivative) if derivative.kind_of?(ActiveForm::Validator::Base) derivative.class_inheritable_accessor :default_msg derivative.class_inheritable_accessor :javascript_validation_code super end |
permalink .javascript_definition(identifier) ⇒ Object
[View source]
86 87 88 89 |
# File 'lib/active_form/validators/base.rb', line 86 def javascript_definition(identifier) return nil if self.javascript_validation_code.blank? %|Validation.add('#{identifier}', "#{self.javascript_validation_code[:msg]}", function (v) {\n #{self.javascript_validation_code[:jscode]}\n});| end |
permalink .javascript_validation(msg = 'validation failed', jscode = '') ⇒ Object
[View source]
82 83 84 |
# File 'lib/active_form/validators/base.rb', line 82 def javascript_validation(msg = 'validation failed', jscode = '') self.javascript_validation_code = { :msg => msg, :jscode => yield(jscode) } end |
permalink .loadable_type ⇒ Object
[View source]
120 121 122 |
# File 'lib/active_form/validators/base.rb', line 120 def loadable_type self.name.to_s.demodulize.underscore.to_sym end |
permalink .message ⇒ Object
[View source]
91 92 93 |
# File 'lib/active_form/validators/base.rb', line 91 def self.default_msg ||= '%s: validation failure' end |
Instance Method Details
permalink #advice ⇒ Object
[View source]
28 29 30 |
# File 'lib/active_form/validators/base.rb', line 28 def advice { code => ActiveForm::Error.new(element, , code, ) } end |
permalink #collection_length ⇒ Object
[View source]
69 70 71 |
# File 'lib/active_form/validators/base.rb', line 69 def collection_length [*self.value].delete_if { |v| v.blank? }.length end |
permalink #identifier ⇒ Object
[View source]
20 21 22 |
# File 'lib/active_form/validators/base.rb', line 20 def identifier self.code == 'required' ? code : "validate-#{code}" end |
permalink #is_element?(arg) ⇒ Boolean
73 74 75 |
# File 'lib/active_form/validators/base.rb', line 73 def is_element?(arg) arg.respond_to?(:element?) && arg.element? end |
permalink #javascript_definition ⇒ Object
[View source]
24 25 26 |
# File 'lib/active_form/validators/base.rb', line 24 def javascript_definition self.class.javascript_definition(identifier) end |
permalink #js_validation(validation_msg, code) ⇒ Object
[View source]
77 78 |
# File 'lib/active_form/validators/base.rb', line 77 def js_validation(validation_msg, code) end |
permalink #label ⇒ Object
[View source]
56 57 58 |
# File 'lib/active_form/validators/base.rb', line 56 def label element.label end |
permalink #message ⇒ Object
[View source]
44 45 46 |
# File 'lib/active_form/validators/base.rb', line 44 def self.msg || self.class. end |
permalink #message=(str) ⇒ Object
[View source]
48 49 50 |
# File 'lib/active_form/validators/base.rb', line 48 def (str) self.msg = str end |
permalink #message_params ⇒ Object
[View source]
52 53 54 |
# File 'lib/active_form/validators/base.rb', line 52 def [] end |
permalink #register_element(elem) ⇒ Object
[View source]
36 37 38 |
# File 'lib/active_form/validators/base.rb', line 36 def register_element(elem) @element = elem end |
permalink #setup ⇒ Object
[View source]
17 18 |
# File 'lib/active_form/validators/base.rb', line 17 def setup end |
permalink #validate ⇒ Object
[View source]
32 33 34 |
# File 'lib/active_form/validators/base.rb', line 32 def validate element.errors << advice[code] end |
permalink #value ⇒ Object
[View source]
60 61 62 |
# File 'lib/active_form/validators/base.rb', line 60 def value element.element_value end |
permalink #value_length ⇒ Object
[View source]
64 65 66 67 |
# File 'lib/active_form/validators/base.rb', line 64 def value_length str = self.value.to_s charlength = str.respond_to?(:chars) ? str.chars.length : str.length end |