Class: LcpRuby::Metadata::ValidationDefinition
- Inherits:
-
Object
- Object
- LcpRuby::Metadata::ValidationDefinition
- Defined in:
- lib/lcp_ruby/metadata/validation_definition.rb
Constant Summary collapse
- VALID_TYPES =
%w[ presence length numericality format inclusion exclusion uniqueness confirmation custom comparison service array_length array_inclusion array_uniqueness ].freeze
Instance Attribute Summary collapse
-
#field_ref ⇒ Object
readonly
Returns the value of attribute field_ref.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#operator ⇒ Object
readonly
Returns the value of attribute operator.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#service_key ⇒ Object
readonly
Returns the value of attribute service_key.
-
#target_field ⇒ Object
readonly
Returns the value of attribute target_field.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#validator_class ⇒ Object
readonly
Returns the value of attribute validator_class.
-
#when_condition ⇒ Object
readonly
Returns the value of attribute when_condition.
Instance Method Summary collapse
- #comparison? ⇒ Boolean
- #custom? ⇒ Boolean
-
#initialize(attrs = {}) ⇒ ValidationDefinition
constructor
A new instance of ValidationDefinition.
- #service? ⇒ Boolean
-
#uniqueness_scope(field_name) ⇒ Object
:fields is a DSL alias for :scope that lists ALL columns of the compound key (including the validated one), so the validated field is subtracted to match Rails’ scope: semantics.
Constructor Details
#initialize(attrs = {}) ⇒ ValidationDefinition
Returns a new instance of ValidationDefinition.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/lcp_ruby/metadata/validation_definition.rb', line 13 def initialize(attrs = {}) attrs = attrs.transform_keys(&:to_s) if attrs.is_a?(Hash) @type = (attrs["type"] || attrs[:type]).to_s @options = (attrs["options"] || attrs[:options] || {}) @validator_class = attrs["validator_class"] || attrs[:validator_class] @when_condition = attrs["when"] @field_ref = (attrs["field_ref"] || attrs[:field_ref])&.to_s @operator = (attrs["operator"] || attrs[:operator])&.to_s @message = attrs["message"] || attrs[:message] @service_key = (attrs["service"] || attrs[:service])&.to_s @target_field = (attrs["field"] || attrs[:field])&.to_s&.presence validate! end |
Instance Attribute Details
#field_ref ⇒ Object (readonly)
Returns the value of attribute field_ref.
10 11 12 |
# File 'lib/lcp_ruby/metadata/validation_definition.rb', line 10 def field_ref @field_ref end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
10 11 12 |
# File 'lib/lcp_ruby/metadata/validation_definition.rb', line 10 def @message end |
#operator ⇒ Object (readonly)
Returns the value of attribute operator.
10 11 12 |
# File 'lib/lcp_ruby/metadata/validation_definition.rb', line 10 def operator @operator end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
10 11 12 |
# File 'lib/lcp_ruby/metadata/validation_definition.rb', line 10 def @options end |
#service_key ⇒ Object (readonly)
Returns the value of attribute service_key.
10 11 12 |
# File 'lib/lcp_ruby/metadata/validation_definition.rb', line 10 def service_key @service_key end |
#target_field ⇒ Object (readonly)
Returns the value of attribute target_field.
10 11 12 |
# File 'lib/lcp_ruby/metadata/validation_definition.rb', line 10 def target_field @target_field end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
10 11 12 |
# File 'lib/lcp_ruby/metadata/validation_definition.rb', line 10 def type @type end |
#validator_class ⇒ Object (readonly)
Returns the value of attribute validator_class.
10 11 12 |
# File 'lib/lcp_ruby/metadata/validation_definition.rb', line 10 def validator_class @validator_class end |
#when_condition ⇒ Object (readonly)
Returns the value of attribute when_condition.
10 11 12 |
# File 'lib/lcp_ruby/metadata/validation_definition.rb', line 10 def when_condition @when_condition end |
Instance Method Details
#comparison? ⇒ Boolean
32 33 34 |
# File 'lib/lcp_ruby/metadata/validation_definition.rb', line 32 def comparison? type == "comparison" end |
#custom? ⇒ Boolean
28 29 30 |
# File 'lib/lcp_ruby/metadata/validation_definition.rb', line 28 def custom? type == "custom" end |
#service? ⇒ Boolean
36 37 38 |
# File 'lib/lcp_ruby/metadata/validation_definition.rb', line 36 def service? type == "service" end |
#uniqueness_scope(field_name) ⇒ Object
:fields is a DSL alias for :scope that lists ALL columns of the compound key (including the validated one), so the validated field is subtracted to match Rails’ scope: semantics.
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/lcp_ruby/metadata/validation_definition.rb', line 43 def uniqueness_scope(field_name) return nil unless type == "uniqueness" raw = [:scope].presence || [:fields].presence return nil if raw.nil? scope = Array(raw).map(&:to_sym) scope -= [ field_name.to_sym ] if [:scope].blank? scope.empty? ? nil : scope end |