Sha256: 8bd6c0cc93fb9b067c26a5293d100109ff21cf698d8344971de35d37b8cc9aa8

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

require 'ostruct'

module Fluxo
  module ActiveModelExtension
    module ClassMethods
      def validations(&block)
        @validations_proxy ||= build_validations_proxy!
        return unless block_given?

        begin
          @validations_proxy.class_eval(&block)
        rescue => e
          raise ValidationDefinitionError, <<~ERROR
            Invalid validations for #{self.class.name}.

            #{e.message}
          ERROR
        end
      end

      private

      def build_validations_proxy!
        validator = Class.new(OpenStruct) do
          include ActiveModel::Validations

          def self.validate!(operation_instance, **attrs)
            validator = new

            attrs.each do |name, value|
              validator.public_send(:"#{name}=", value)
            end

            if validator.valid?
              operation_instance.Void()
            else
              operation_instance.Failure(:validation) { validator.errors }
            end
          end

          def valid?
            super
            errors.empty?
          end
        end

        validator.class_eval <<-RUBY, __FILE__, __LINE__ + 1
          def self.name
            "#{name || 'Fluxo::Operation'}::Validations"
          end

          def self.to_s
            name
          end
        RUBY

        validator
      end
    end

    module InstanceMethods
    end

    def self.included(klass)
      klass.extend(ClassMethods)
      klass.send(:include, InstanceMethods)
    end
  end

  class Operation
    include Fluxo::ActiveModelExtension
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fluxo-0.3.0 lib/fluxo/active_model_extension.rb