Sha256: 0a2c77303a3b7141adb820ae920d032150425a4f8ce44d61fb680c268e929d96

Contents?: true

Size: 1.08 KB

Versions: 3

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module MissionKontrolRelay
  class ValidationMappingService
    def initialize(model)
      @model = model
    end

    def build
      {
        model: @model.name,
        validations: model_validations
      }
    end

    private

    def model_validations
      validations = []

      @model._validate_callbacks.to_a.each do |validation|
        validations << validation_attributes(validation)
      end

      validations
    end

    def validation_attributes(validation)
      {
        key: remove_klass_from_key(validation),
        type: validation.instance_variable_get(:@key).try(:kind),
        kind: validation.kind,
        name: validation.name,
        attributes: validation.try(:filter).try(:attributes),
        if: validation.instance_variable_get(:@if),
        unless: validation.instance_variable_get(:@unless)
      }
    end

    def remove_klass_from_key(validation)
      key = validation.instance_variable_get(:@key)
      if key.instance_variable_get(:@klass)
        key.instance_variable_set(:@klass, nil)
      end

      key
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mission_kontrol_relay-0.1.2 app/services/mission_kontrol_relay/validation_mapping_service.rb
mission_kontrol_relay-0.1.1 app/services/mission_kontrol_relay/validation_mapping_service.rb
mission_kontrol_relay-0.1.0 app/services/mission_kontrol_relay/validation_mapping_service.rb