Class: LiveValidations::AdapterBase::ValidationHook

Inherits:
Object
  • Object
show all
Defined in:
lib/client_validations/adapter_base/validation_hook.rb

Overview

A ValidationHook is this plugins representation of a validation, and is created with the adapter DSL through the Validations::AdapterBase.validates method.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ ValidationHook

Returns a new instance of ValidationHook.



8
9
10
11
# File 'lib/client_validations/adapter_base/validation_hook.rb', line 8

def initialize(&block)
  @proc = block
  @data = {}
end

Instance Attribute Details

#adapter_instanceObject (readonly)

Returns the value of attribute adapter_instance.



6
7
8
# File 'lib/client_validations/adapter_base/validation_hook.rb', line 6

def adapter_instance
  @adapter_instance
end

#callbackObject (readonly)

Returns the value of attribute callback.



6
7
8
# File 'lib/client_validations/adapter_base/validation_hook.rb', line 6

def callback
  @callback
end

#dataObject (readonly)

Returns the value of attribute data.



6
7
8
# File 'lib/client_validations/adapter_base/validation_hook.rb', line 6

def data
  @data
end

#prefixObject (readonly)

Returns the value of attribute prefix.



6
7
8
# File 'lib/client_validations/adapter_base/validation_hook.rb', line 6

def prefix
  @prefix
end

Instance Method Details

#[](key) ⇒ Object



13
14
15
# File 'lib/client_validations/adapter_base/validation_hook.rb', line 13

def [](key)
  data[key]
end

#[]=(key, value) ⇒ Object



17
18
19
# File 'lib/client_validations/adapter_base/validation_hook.rb', line 17

def []=(key, value)
  data[key] = value
end

#handwritten_message_for(attribute) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/client_validations/adapter_base/validation_hook.rb', line 46

def handwritten_message_for(attribute)
  return unless callback.options[:message]
  
  I18n.backend.send(:interpolate, I18n.locale, callback.options[:message], {
    :model => adapter_instance.active_record_instance.class.human_name,
    :attribute => attribute
  })
end

#message_for(attribute, key, options = {}) ⇒ Object

Returns either the :message specified, or the default I18n error message.



42
43
44
# File 'lib/client_validations/adapter_base/validation_hook.rb', line 42

def message_for(attribute, key,options={})
  handwritten_message_for(attribute) || I18n.translate(key, {:scope => 'activerecord.errors.messages'}.merge(options))
end

#regexObject



55
56
57
# File 'lib/client_validations/adapter_base/validation_hook.rb', line 55

def regex
  callback.options[:live_validator] || callback.options[:with]
end

#run_validation(adapter_instance, callback) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/client_validations/adapter_base/validation_hook.rb', line 21

def run_validation(adapter_instance, callback)
  @adapter_instance = adapter_instance
  @callback = callback
  @prefix = @adapter_instance.prefix
  
  # The @proc is called once for each of the attributes in the @callback,
  # passing the attribute to the proc much like validates_each.
  @callback.options[:attributes].each {|attribute| @proc.call(self, attribute) }
  

  @data.each do |key, value|
    case value
    when Hash
      recursively_merge_hashes(@adapter_instance[key], value)
    when Array
      @adapter_instance[key] += value
    end
  end
end