Class: Ward::Context
- Inherits:
-
Object
- Object
- Ward::Context
- Defined in:
- lib/ward/context.rb
Overview
A class which represents “somewhere” from which a value can be retrieved for validation.
A context initialized with a :length
attribute assumes that the value for validation can be retrieved by calling length
on the target object.
Instance Attribute Summary collapse
-
#attribute ⇒ Symbol
readonly
Returns the name of the attribute to be validated.
-
#natural_name ⇒ String
readonly
Returns the ‘natural name’ of the attribute.
Instance Method Summary collapse
-
#initialize(attribute, *context_args, &context_block) ⇒ Context
constructor
Creates a new validator instance.
-
#value(target) ⇒ Object
Returns the value of the context for the given
target
object.
Constructor Details
#initialize(attribute, *context_args, &context_block) ⇒ Context
Creates a new validator instance.
40 41 42 43 44 45 46 |
# File 'lib/ward/context.rb', line 40 def initialize(attribute, *context_args, &context_block) @attribute = attribute.to_sym @context_args, @context_block = context_args, context_block @natural_name = ActiveSupport::Inflector.humanize(@attribute.to_s).downcase end |
Instance Attribute Details
#attribute ⇒ Symbol (readonly)
Returns the name of the attribute to be validated.
14 15 16 |
# File 'lib/ward/context.rb', line 14 def attribute @attribute end |
#natural_name ⇒ String (readonly)
Returns the ‘natural name’ of the attribute.
This name is used when generating error messages, since you probably don’t want you end users to be presented with (occasionally) obscure attribute names.
29 30 31 |
# File 'lib/ward/context.rb', line 29 def natural_name @natural_name end |
Instance Method Details
#value(target) ⇒ Object
Returns the value of the context for the given target
object.
65 66 67 |
# File 'lib/ward/context.rb', line 65 def value(target) target.__send__(@attribute, *@context_args, &@context_block) end |