/
stateful.rb
281 lines (258 loc) · 7.41 KB
/
stateful.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
require 'rom/changeset/pipe'
module ROM
class Changeset
# Stateful changesets carry data and can transform it into
# a different structure compatible with a persistence backend
#
# @abstract
class Stateful < Changeset
# Default no-op pipe
EMPTY_PIPE = Pipe.new(use_for_diff: false).freeze
# @!attribute [r] __data__
# @return [Hash] The relation data
# @api private
option :__data__, optional: true
# @!attribute [r] pipe
# @return [Changeset::Pipe] data transformation pipe
# @api private
option :pipe, reader: false, optional: true
# Define a changeset mapping
#
# Subsequent mapping definitions will be composed together
# and applied in the order they way defined
#
# @example Transformation DSL
# class NewUser < ROM::Changeset::Create
# map do
# unwrap :address, prefix: true
# end
# end
#
# @example Using custom block
# class NewUser < ROM::Changeset::Create
# map do |tuple|
# tuple.merge(created_at: Time.now)
# end
# end
#
# @example Multiple mappings (executed in the order of definition)
# class NewUser < ROM::Changeset::Create
# map do
# unwrap :address, prefix: true
# end
#
# map do |tuple|
# tuple.merge(created_at: Time.now)
# end
# end
#
# @return [Array<Pipe>, Transproc::Function>]
#
# @see https://github.com/solnic/transproc Transproc
#
# @api public
def self.map(options = EMPTY_HASH, &block)
if block.parameters.empty?
pipes << Class.new(Pipe, &block).new(options)
else
pipes << Pipe.new(block, options)
end
end
# Define a changeset mapping excluded from diffs
#
# @see Changeset::Stateful.map
# @see Changeset::Stateful#extend
#
# @return [Array<Pipe>, Transproc::Function>]
#
# @api public
def self.extend(*, &block)
if block
map(use_for_diff: false, &block)
else
super
end
end
# Build default pipe object
#
# This can be overridden in a custom changeset subclass
#
# @return [Pipe]
def self.default_pipe(context)
pipes.size > 0 ? pipes.map { |p| p.bind(context) }.reduce(:>>) : EMPTY_PIPE
end
# @api private
def self.inherited(klass)
return if klass == ROM::Changeset
super
klass.instance_variable_set(:@__pipes__, pipes.dup)
end
# @api private
def self.pipes
@__pipes__
end
@__pipes__ = EMPTY_ARRAY
# Pipe changeset's data using custom steps define on the pipe
#
# @overload map(*steps)
# Apply mapping using built-in transformations
#
# @example
# changeset.map(:add_timestamps)
#
# @param [Array<Symbol>] steps A list of mapping steps
#
# @overload map(&block)
# Apply mapping using a custom block
#
# @example
# changeset.map { |tuple| tuple.merge(created_at: Time.now) }
#
# @overload map(*steps, &block)
# Apply mapping using built-in transformations and a custom block
#
# @example
# changeset.map(:add_timestamps) { |tuple| tuple.merge(status: 'published') }
#
# @param [Array<Symbol>] steps A list of mapping steps
#
# @return [Changeset]
#
# @api public
def map(*steps, &block)
extend(*steps, for_diff: true, &block)
end
# Pipe changeset's data using custom steps define on the pipe.
# You should use #map instead except updating timestamp fields.
# Calling changeset.extend builds a pipe that excludes certain
# steps for generating the diff. Currently the only place where
# it is used is update changesets with the `:touch` step, i.e.
# `changeset.extend(:touch).diff` will exclude `:updated_at`
# from the diff.
#
# @see Changeset::Stateful#map
#
# @return [Changeset]
#
# @api public
def extend(*steps, **options, &block)
if block
if steps.size > 0
extend(*steps, options).extend(options, &block)
else
with(pipe: pipe.compose(Pipe.new(block).bind(self), options))
end
else
with(pipe: steps.reduce(pipe.with(options)) { |a, e| a.compose(pipe[e], options) })
end
end
# Return changeset with data
#
# @param [Hash] data
#
# @return [Changeset]
#
# @api public
def data(data)
with(__data__: data)
end
# Coerce changeset to a hash
#
# This will send the data through the pipe
#
# @return [Hash]
#
# @api public
def to_h
pipe.call(__data__)
end
alias_method :to_hash, :to_h
# Coerce changeset to an array
#
# This will send the data through the pipe
#
# @return [Array]
#
# @api public
def to_a
result == :one ? [to_h] : __data__.map { |element| pipe.call(element) }
end
alias_method :to_ary, :to_a
# Commit stateful changeset
#
# @see Changeset#commit
#
# @api public
def commit
command.call(self)
end
# Associate a changeset with another changeset or hash-like object
#
# @example with another changeset
# new_user = users.changeset(name: 'Jane')
# new_task = users.changeset(:tasks, title: 'A task')
#
# new_task.associate(new_user, :users)
#
# @example with a hash-like object
# user = users.users.by_pk(1).one
# new_task = users.changeset(:tasks, title: 'A task')
#
# new_task.associate(user, :users)
#
# @param [#to_hash, Changeset] other Other changeset or hash-like object
# @param [Symbol] assoc The association identifier from schema
#
# @api public
def associate(other, name = Associated.infer_assoc_name(other))
Associated.new(self, associations: { name => other })
end
# Return command result type
#
# @return [Symbol]
#
# @api private
def result
__data__.is_a?(Array) ? :many : :one
end
# @api public
def command
relation.command(command_type, DEFAULT_COMMAND_OPTS.merge(result: result))
end
# Return string representation of the changeset
#
# @return [String]
#
# @api public
def inspect
%(#<#{self.class} relation=#{relation.name.inspect} data=#{__data__}>)
end
# Data transformation pipe
#
# @return [Changeset::Pipe]
#
# @api private
def pipe
@pipe ||= self.class.default_pipe(self)
end
private
# @api private
def respond_to_missing?(meth, include_private = false)
super || __data__.respond_to?(meth)
end
# @api private
def method_missing(meth, *args, &block)
if __data__.respond_to?(meth)
response = __data__.__send__(meth, *args, &block)
if response.is_a?(__data__.class)
with(__data__: response)
else
response
end
else
super
end
end
end
end
end