Class: ActiveForm::Validator::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/active_form/validators/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) {|_self| ... } ⇒ Base

Returns a new instance of Base.

Yields:

  • (_self)

Yield Parameters:



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

#codeObject 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

#elementObject (readonly)

Returns the value of attribute element.



3
4
5
# File 'lib/active_form/validators/base.rb', line 3

def element
  @element
end

#msgObject

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 default_message=(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_typeObject



120
121
122
# File 'lib/active_form/validators/base.rb', line 120

def loadable_type
  self.name.to_s.demodulize.underscore.to_sym
end

.messageObject



91
92
93
# File 'lib/active_form/validators/base.rb', line 91

def message
  self.default_msg ||= '%s: validation failure'
end

Instance Method Details

#adviceObject



28
29
30
# File 'lib/active_form/validators/base.rb', line 28

def advice
  { code => ActiveForm::Error.new(element, message, code, message_params) }
end

#collection_lengthObject



69
70
71
# File 'lib/active_form/validators/base.rb', line 69

def collection_length
  [*self.value].delete_if { |v| v.blank? }.length
end

#identifierObject



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

Returns:

  • (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_definitionObject



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

#labelObject



56
57
58
# File 'lib/active_form/validators/base.rb', line 56

def label
  element.label
end

#messageObject



44
45
46
# File 'lib/active_form/validators/base.rb', line 44

def message
  self.msg || self.class.message
end

#message=(str) ⇒ Object



48
49
50
# File 'lib/active_form/validators/base.rb', line 48

def message=(str)
  self.msg = str
end

#message_paramsObject



52
53
54
# File 'lib/active_form/validators/base.rb', line 52

def message_params
  []
end

#register_element(elem) ⇒ Object



36
37
38
# File 'lib/active_form/validators/base.rb', line 36

def register_element(elem)
  @element = elem
end

#setupObject



17
18
# File 'lib/active_form/validators/base.rb', line 17

def setup
end

#validateObject



32
33
34
# File 'lib/active_form/validators/base.rb', line 32

def validate 
  element.errors << advice[code]
end

#valueObject



60
61
62
# File 'lib/active_form/validators/base.rb', line 60

def value
  element.element_value
end

#value_lengthObject



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