/
lazy.rb
97 lines (82 loc) · 2.18 KB
/
lazy.rb
1
2
3
4
5
6
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
require 'rom/commands/composite'
require 'rom/commands/graph'
module ROM
module Commands
# Lazy command wraps another command and evaluates its input when called
#
# @api private
class Lazy
include Dry::Equalizer(:command, :evaluator)
# @attr_reader [Command] command The wrapped command
attr_reader :command
# @attr_reader [Proc] evaluator The proc that will evaluate the input
attr_reader :evaluator
attr_reader :command_proc
# @api private
def self.[](command)
case command
when Commands::Create then Lazy::Create
when Commands::Update then Lazy::Update
when Commands::Delete then Lazy::Delete
else
self
end
end
# @api private
def initialize(command, evaluator, command_proc = nil)
@command = command
@evaluator = evaluator
@command_proc = command_proc || proc { |*| command }
end
# Evaluate command's input using the input proc and pass to command
#
# @return [Array,Hash]
#
# @api public
def call(*args)
raise NotImplementedError
end
# Compose a lazy command with another one
#
# @see Commands::Abstract#>>
#
# @return [Composite]
#
# @api public
def >>(other)
Composite.new(self, other)
end
# @see Abstract#combine
#
# @api public
def combine(*others)
Graph.new(self, others)
end
# @api private
def lazy?
true
end
# @api private
def respond_to_missing?(name, include_private = false)
super || command.respond_to?(name)
end
private
# @api private
def method_missing(name, *args, &block)
if command.respond_to?(name)
response = command.public_send(name, *args, &block)
if response.instance_of?(command.class)
self.class.new(response, evaluator, command_proc)
else
response
end
else
super
end
end
end
end
end
require 'rom/commands/lazy/create'
require 'rom/commands/lazy/update'
require 'rom/commands/lazy/delete'