Sha256: 27197da5bfea5f14032d2be3372ea3be42175c6909a61aff8c1406d961219a65
Contents?: true
Size: 1.75 KB
Versions: 2
Compression:
Stored size: 1.75 KB
Contents
# frozen_string_literal: true module Lite module Command module Internals module Attributes def self.included(base) base.extend ClassMethods end module ClassMethods def required(*attributes, from: :context, **options) delegates(*attributes, from:) validates_each(*attributes, **options) do |command, method_name, _attr_value| next if command.errors.added?(from, :undefined) || command.errors.added?(method_name, :required) if !command.respond_to?(from, true) command.errors.add(from, :undefined, message: "is an undefined argument") elsif !command.send(from).respond_to?(method_name, true) command.errors.add(method_name, :required, message: "is a required argument") end end end def optional(*attributes, from: :context, **_options) delegates(*attributes, from:) end private def delegates(*attributes, from: :context) attributes.each do |method_name| define_method(method_name) do return unless respond_to?(from, true) Utils.cmd_try(send(from), method_name) end end end end def read_attribute_for_validation(method_name) Utils.cmd_try(self, method_name) rescue NameError # Do nothing, fallback to :undefined error end private def validate_context_attributes run_hooks(:before_validation) invalid!(errors.full_messages.join(". "), metadata: errors.messages) unless valid? run_hooks(:after_validation) end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
lite-command-3.3.3 | lib/lite/command/internals/attributes.rb |
lite-command-3.3.2 | lib/lite/command/internals/attributes.rb |