Class: LcpRuby::Metadata::ValidationDefinition

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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 = normalize_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_refObject (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

#messageObject (readonly)

Returns the value of attribute message.



10
11
12
# File 'lib/lcp_ruby/metadata/validation_definition.rb', line 10

def message
  @message
end

#operatorObject (readonly)

Returns the value of attribute operator.



10
11
12
# File 'lib/lcp_ruby/metadata/validation_definition.rb', line 10

def operator
  @operator
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/lcp_ruby/metadata/validation_definition.rb', line 10

def options
  @options
end

#service_keyObject (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_fieldObject (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

#typeObject (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_classObject (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_conditionObject (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

Returns:

  • (Boolean)


32
33
34
# File 'lib/lcp_ruby/metadata/validation_definition.rb', line 32

def comparison?
  type == "comparison"
end

#custom?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/lcp_ruby/metadata/validation_definition.rb', line 28

def custom?
  type == "custom"
end

#service?Boolean

Returns:

  • (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 = options[:scope].presence || options[:fields].presence
  return nil if raw.nil?

  scope = Array(raw).map(&:to_sym)
  scope -= [ field_name.to_sym ] if options[:scope].blank?
  scope.empty? ? nil : scope
end