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
#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
#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 |
#element ⇒ Object (readonly)
Returns the value of attribute element.
3 4 5 |
# File 'lib/active_form/validators/base.rb', line 3 def element @element end |
#msg ⇒ Object
Returns the value of attribute msg.
4 5 6 |
# File 'lib/active_form/validators/base.rb', line 4 def msg @msg end |
Class Method Details
.create(definition_name, &block) ⇒ Object
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 |
.default_message=(str) ⇒ Object Also known as: default_message
95 96 97 |
# File 'lib/active_form/validators/base.rb', line 95 def (str) self.default_msg = str end |
.inherited(derivative) ⇒ Object
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 |
.javascript_definition(identifier) ⇒ Object
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 |
.javascript_validation(msg = 'validation failed', jscode = '') ⇒ Object
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 |
.loadable_type ⇒ Object
120 121 122 |
# File 'lib/active_form/validators/base.rb', line 120 def loadable_type self.name.to_s.demodulize.underscore.to_sym end |
.message ⇒ Object
91 92 93 |
# File 'lib/active_form/validators/base.rb', line 91 def self.default_msg ||= '%s: validation failure' end |
Instance Method Details
#advice ⇒ Object
28 29 30 |
# File 'lib/active_form/validators/base.rb', line 28 def advice { code => ActiveForm::Error.new(element, , code, ) } end |
#collection_length ⇒ Object
69 70 71 |
# File 'lib/active_form/validators/base.rb', line 69 def collection_length [*self.value].delete_if { |v| v.blank? }.length end |
#identifier ⇒ Object
20 21 22 |
# File 'lib/active_form/validators/base.rb', line 20 def identifier self.code == 'required' ? code : "validate-#{code}" end |
#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 |
#javascript_definition ⇒ Object
24 25 26 |
# File 'lib/active_form/validators/base.rb', line 24 def javascript_definition self.class.javascript_definition(identifier) end |
#js_validation(validation_msg, code) ⇒ Object
77 78 |
# File 'lib/active_form/validators/base.rb', line 77 def js_validation(validation_msg, code) end |
#label ⇒ Object
56 57 58 |
# File 'lib/active_form/validators/base.rb', line 56 def label element.label end |
#message ⇒ Object
44 45 46 |
# File 'lib/active_form/validators/base.rb', line 44 def self.msg || self.class. end |
#message=(str) ⇒ Object
48 49 50 |
# File 'lib/active_form/validators/base.rb', line 48 def (str) self.msg = str end |
#message_params ⇒ Object
52 53 54 |
# File 'lib/active_form/validators/base.rb', line 52 def [] end |
#register_element(elem) ⇒ Object
36 37 38 |
# File 'lib/active_form/validators/base.rb', line 36 def register_element(elem) @element = elem end |
#setup ⇒ Object
17 18 |
# File 'lib/active_form/validators/base.rb', line 17 def setup end |
#validate ⇒ Object
32 33 34 |
# File 'lib/active_form/validators/base.rb', line 32 def validate element.errors << advice[code] end |
#value ⇒ Object
60 61 62 |
# File 'lib/active_form/validators/base.rb', line 60 def value element.element_value end |
#value_length ⇒ Object
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 |