Sha256: 7f286c7685795670d8fe1d39983bcf8ec2befb88d3c8b5b85c0aec53de6021d6

Contents?: true

Size: 1.8 KB

Versions: 10

Compression:

Stored size: 1.8 KB

Contents

module Hyperloop
  class Operation
    class Railway

      def validations
        self.class.validations
      end

      def add_validation_error(i, e)
        @operation.add_error("param validation #{i+1}", :validation_error, e.to_s)
      end

      class << self
        def validations
          @validations ||= []
        end

        def add_validation(*args, &block)
          block = args[0] if args[0]
          validations << block
        end

        def add_error(param, symbol, message, *args, &block)
          add_validation do
            begin
              add_error(param, symbol, message) if instance_eval(&block)
            rescue Exit => e
              raise e unless e.state == :failed
              add_error(param, symbol, message)
              raise Exit.new(:abort_from_add_error, e.result)
            end
          end
        end
      end

      def process_validations
        validations.each_with_index do |validator, i|
          begin
            validator = @operation.method(validator) if validator.is_a? Symbol
            next if @operation.instance_exec(&validator)
            add_validation_error(i, "param validation #{i+1} failed")
          rescue Exit => e
            case e.state
            when :success
              add_validation_error(i, "illegal use of succeed! in validation")
            when :failed
              add_validation_error(i, "param validation #{i+1} aborted")
            end
            @state = :failed
            return # break does not work in Opal
          rescue AccessViolation => e
            add_validation_error(i, e)
            @state = :failed
            @last_result = e
            return # break does not work in Opal
          rescue Exception => e
            add_validation_error(i, e)
          end
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
hyper-operation-0.5.9 lib/hyper-operation/railway/validations.rb
hyper-operation-0.5.8 lib/hyper-operation/railway/validations.rb
hyper-operation-0.5.7 lib/hyper-operation/railway/validations.rb
hyper-operation-0.5.6 lib/hyper-operation/railway/validations.rb
hyper-operation-0.5.5 lib/hyper-operation/railway/validations.rb
hyper-operation-0.5.4 lib/hyper-operation/railway/validations.rb
hyper-operation-0.5.3 lib/hyper-operation/railway/validations.rb
hyper-operation-0.5.2 lib/hyper-operation/railway/validations.rb
hyper-operation-0.5.1 lib/hyper-operation/railway/validations.rb
hyper-operation-0.5.0 lib/hyper-operation/railway/validations.rb