Class: LiveValidations::AdapterBase::ValidationHook
- Inherits:
-
Object
- Object
- LiveValidations::AdapterBase::ValidationHook
- 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
-
#adapter_instance ⇒ Object
readonly
Returns the value of attribute adapter_instance.
-
#callback ⇒ Object
readonly
Returns the value of attribute callback.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #handwritten_message_for(attribute) ⇒ Object
-
#initialize(&block) ⇒ ValidationHook
constructor
A new instance of ValidationHook.
-
#message_for(attribute, key, options = {}) ⇒ Object
Returns either the :message specified, or the default I18n error message.
- #regex ⇒ Object
- #run_validation(adapter_instance, callback) ⇒ Object
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_instance ⇒ Object (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 |
#callback ⇒ Object (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 |
#data ⇒ Object (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 |
#prefix ⇒ Object (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 (attribute) return unless callback.[:message] I18n.backend.send(:interpolate, I18n.locale, callback.[: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 (attribute, key,={}) (attribute) || I18n.translate(key, {:scope => 'activerecord.errors.messages'}.merge()) end |
#regex ⇒ Object
55 56 57 |
# File 'lib/client_validations/adapter_base/validation_hook.rb', line 55 def regex callback.[:live_validator] || callback.[: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.[: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 |