Sha256: 7464cdd08036a5c3313c1bb9e31132b3964906a2dc7e2b7565f3aba85b0fb337
Contents?: true
Size: 1.1 KB
Versions: 1
Compression:
Stored size: 1.1 KB
Contents
# frozen_string_literal: true require_relative 'active_model_service/version' require 'active_model' module ActiveModelService class Error < StandardError; end class Call include ActiveModel::Validations include ActiveModel::AttributeAssignment class ValueError < StandardError; end attr_reader :result, :valid def _call @result = call if send(:run_validations!) rescue Error => e puts e.message end def valid?(_ = nil) @valid = errors.empty? end def self.call(attributes = {}) new.tap do |instance| attributes.each do |k, v| raise Error, "Attribute #{k} is a reserve word!" if %i[result error valid call].include?(k.to_sym) unless instance.methods.include?(k.to_sym) raise Error, "Attribute is not defined! Add `attr :#{k}` in #{instance.class}" end instance.instance_variable_set("@#{k}".to_sym, v) end instance._call end end # Add error to :base once def add_error(message) errors.add(:base, message) raise Error, message end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
active_model_service-0.1.0 | lib/active_model_service.rb |