Sha256: 67f98317a79cddef6dbfed8e15c4c6ab1b1f0308045ecb50c3680c1274be6508
Contents?: true
Size: 1.55 KB
Versions: 3
Compression:
Stored size: 1.55 KB
Contents
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 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 attr_accessor #{attribute_names.map(&:inspect).join(", ")} def self.model_name ::ActiveModel::Name.new(self, nil, %|#{name || "Anonymous"}|) 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
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
fluxo-0.2.1 | lib/fluxo/active_model_extension.rb |
fluxo-0.2.0 | lib/fluxo/active_model_extension.rb |
fluxo-0.1.0 | lib/fluxo/active_model_extension.rb |