Class: Decidim::Command

Inherits:
Object
  • Object
show all
Includes:
Wisper::Publisher
Defined in:
lib/decidim/command.rb

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/decidim/command.rb', line 33

def method_missing(method_name, ...)
  if @caller.respond_to?(method_name, true)
    @caller.send(method_name, ...)
  else
    super
  end
end

Class Method Details

.callObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/decidim/command.rb', line 13

def self.call(*, **, &)
  event_recorder = Decidim::EventRecorder.new

  command = new(*, **)
  command.subscribe(event_recorder)
  command.evaluate(&) if block_given?
  command.call

  event_recorder.events
end

Instance Method Details

#evaluate(&block) ⇒ Object



24
25
26
27
# File 'lib/decidim/command.rb', line 24

def evaluate(&block)
  @caller = eval("self", block.binding, __FILE__, __LINE__)
  instance_eval(&block)
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/decidim/command.rb', line 41

def respond_to_missing?(method_name, include_private = false)
  @caller.respond_to?(method_name, include_private)
end

#transactionObject



29
30
31
# File 'lib/decidim/command.rb', line 29

def transaction(&)
  ActiveRecord::Base.transaction(&) if block_given?
end

#with_events(with_transaction: false, &block) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/decidim/command.rb', line 45

def with_events(with_transaction: false, &block)
  ActiveSupport::Notifications.publish("#{event_namespace}:before", **event_arguments)

  with_transaction ? transaction(&block) : yield

  ActiveSupport::Notifications.publish("#{event_namespace}:after", **event_arguments)
end