Module: Dry::Mutations::Extensions::Command

Includes:
Dry::Monads::Either::Mixin
Defined in:
lib/dry/mutations/extensions/command.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#validationObject (readonly)

Returns the value of attribute validation.



35
36
37
# File 'lib/dry/mutations/extensions/command.rb', line 35

def validation
  @validation
end

Class Method Details

.prepended(base) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dry/mutations/extensions/command.rb', line 7

def self.prepended base
  fail ArgumentError, "Can not prepend #{self.class} to #{base.class}: base class must be a ::Mutations::Command descendant." unless base < ::Mutations::Command
  base.extend(DSL::Module) unless base.ancestors.include?(DSL::Module)
  base.extend(Module.new do
    def call(*args)
      new(*args).call
    end

    def to_proc
      ->(*args) { new(*args).call }
    end

    if base.name && !::Kernel.methods.include?(base_name = base.name.split('::').last.to_sym)
      ::Kernel.class_eval <<-FACTORY, __FILE__, __LINE__ + 1
        def #{base_name}(*args)
          #{base}.call(*args)
        end
      FACTORY
    end
  end)

  base.singleton_class.prepend(Module.new do
    def respond_to_missing?(method_name, include_private = false)
      [:call, :to_proc].include?(method_name) || super
    end
  end)
end

Instance Method Details

#add_error(key, kind, message = nil, dry_message = nil) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/dry/mutations/extensions/command.rb', line 83

def add_error(key, kind, message = nil, dry_message = nil)
  fail ArgumentError.new("Invalid kind #{kind}") unless kind.is_a?(Symbol)

  path = key.to_s.split('.')
  # ["#<struct Dry::Validation::Message
  #            predicate=:int?,
  #            path=[:maturity_set, :maturity_days_set, :days],
  #            text=\"must be an integer\",
  #            options={:args=>[], :rule=>:days, :each=>false}>"
  dry_message ||= ::Dry::Validation::Message.new(kind, *path.map(&:to_sym), message, rule: :♻)
  atom = Errors::ErrorAtom.new(key, kind, dry_message, message: message)

  last = path.pop
  (@errors ||= ::Mutations::ErrorHash.new).tap do |errs|
    path.inject(errs) do |cur_errors, part|
      cur_errors[part.to_sym] ||= ::Mutations::ErrorHash.new
    end[last] = atom
  end
end

#callObject

Functional helpers



65
66
67
# File 'lib/dry/mutations/extensions/command.rb', line 65

def call
  run.either
end

#executeObject



77
78
79
80
81
# File 'lib/dry/mutations/extensions/command.rb', line 77

def execute
  super
rescue => e
  add_error(:♻, :runtime_exception, e.message)
end

#initialize(*args) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/dry/mutations/extensions/command.rb', line 37

def initialize(*args)
  @raw_inputs = Utils.RawInputs(*args)

  @validation_result = schema.(@raw_inputs)

  @inputs = Utils.Hash @validation_result.output

  # dry: {:name=>["size cannot be greater than 10"],
  #       :properties=>{:first_arg=>["must be a string", "is in invalid format"]},
  #       :second_arg=>{:second_sub_arg=>["must be one of: 42"]},
  #       :amount=>["must be one of: 42"]}}
  # mut: {:name=>#<Mutations::ErrorAtom:0x00000009534e50 @key=:name, @symbol=:max_length, @message=nil, @index=nil>,
  #       :properties=>{
  #           :second_arg=>{:second_sub_arg=>#<Mutations::ErrorAtom:0x000000095344a0 @key=:second_sub_arg, @symbol=:in, @message=nil, @index=nil>}
  #       :amount=>#<Mutations::ErrorAtom:0x00000009534068 @key=:amount, @symbol=:in, @message=nil, @index=nil>}

  @errors = Errors::ErrorAtom.patch_message_set(
    Errors::ErrorCompiler.new(schema).(@validation_result.to_ast.last)
  )

  # Run a custom validation method if supplied:
  validate unless has_errors?
end

#messagesObject



103
104
105
# File 'lib/dry/mutations/extensions/command.rb', line 103

def messages
  @messages ||= @errors && @errors.values.map(&:dry_message)
end

#validation_outcome(result = nil) ⇒ Object

Overrides



73
74
75
# File 'lib/dry/mutations/extensions/command.rb', line 73

def validation_outcome(result = nil)
  ::Dry::Mutations::Extensions::Outcome(super)
end