Class: ROM::Changeset::Stateful Abstract
- Inherits:
-
ROM::Changeset
- Object
- ROM::Changeset
- ROM::Changeset::Stateful
- Defined in:
- lib/rom/changeset/stateful.rb
Overview
Stateful changesets carry data and can transform it into a different structure compatible with a persistence backend
Constant Summary collapse
- EMPTY_PIPE =
Default no-op pipe
Pipe.new(use_for_diff: false).freeze
Constants inherited from ROM::Changeset
Instance Attribute Summary collapse
-
#__data__ ⇒ Hash
readonly
private
The relation data.
-
#pipe ⇒ Changeset::Pipe
readonly
private
Data transformation pipe.
Attributes inherited from ROM::Changeset
Class Method Summary collapse
-
.default_pipe(context) ⇒ Pipe
Build default pipe object.
-
.extend(&block) ⇒ Array<Pipe>, Transproc::Function
Define a changeset mapping excluded from diffs.
- .inherited(klass) ⇒ Object private
-
.map(options = EMPTY_HASH, &block) ⇒ Array<Pipe>, Transproc::Function
Define a changeset mapping.
- .pipes ⇒ Object private
Instance Method Summary collapse
-
#associate(other, name = Associated.infer_assoc_name(other)) ⇒ Object
Associate a changeset with another changeset or hash-like object.
- #command ⇒ Object
-
#commit ⇒ Object
Commit stateful changeset.
-
#data(data) ⇒ Changeset
Return changeset with data.
-
#extend(*steps, **options, &block) ⇒ Changeset
Pipe changeset’s data using custom steps define on the pipe.
-
#inspect ⇒ String
Return string representation of the changeset.
-
#map(*steps, &block) ⇒ Changeset
Pipe changeset’s data using custom steps define on the pipe.
-
#result ⇒ Symbol
private
Return command result type.
-
#to_a ⇒ Array
(also: #to_ary)
Coerce changeset to an array.
-
#to_h ⇒ Hash
(also: #to_hash)
Coerce changeset to a hash.
Methods inherited from ROM::Changeset
[], command_type, #new, relation
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/rom/changeset/stateful.rb', line 266 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 |
Instance Attribute Details
#__data__ ⇒ Hash (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns The relation data.
16 |
# File 'lib/rom/changeset/stateful.rb', line 16 option :__data__, optional: true |
#pipe ⇒ Changeset::Pipe (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Data transformation pipe
21 |
# File 'lib/rom/changeset/stateful.rb', line 21 option :pipe, reader: false, optional: true |
Class Method Details
.default_pipe(context) ⇒ Pipe
Build default pipe object
This can be overridden in a custom changeset subclass
87 88 89 |
# File 'lib/rom/changeset/stateful.rb', line 87 def self.default_pipe(context) pipes.size > 0 ? pipes.map { |p| p.bind(context) }.reduce(:>>) : EMPTY_PIPE end |
.extend(&block) ⇒ Array<Pipe>, Transproc::Function
Define a changeset mapping excluded from diffs
74 75 76 77 78 79 80 |
# File 'lib/rom/changeset/stateful.rb', line 74 def self.extend(*, &block) if block map(use_for_diff: false, &block) else super end end |
.inherited(klass) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
92 93 94 95 96 |
# File 'lib/rom/changeset/stateful.rb', line 92 def self.inherited(klass) return if klass == ROM::Changeset super klass.instance_variable_set(:@__pipes__, pipes.dup) end |
.map(options = EMPTY_HASH, &block) ⇒ Array<Pipe>, Transproc::Function
Define a changeset mapping
Subsequent mapping definitions will be composed together and applied in the order they way defined
58 59 60 61 62 63 64 |
# File 'lib/rom/changeset/stateful.rb', line 58 def self.map( = EMPTY_HASH, &block) if block.parameters.empty? pipes << Class.new(Pipe, &block).new() else pipes << Pipe.new(block, ) end end |
.pipes ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
99 100 101 |
# File 'lib/rom/changeset/stateful.rb', line 99 def self.pipes @__pipes__ end |
Instance Method Details
#associate(other, name = Associated.infer_assoc_name(other)) ⇒ Object
Associate a changeset with another changeset or hash-like object
222 223 224 |
# File 'lib/rom/changeset/stateful.rb', line 222 def associate(other, name = Associated.infer_assoc_name(other)) Associated.new(self, associations: { name => other }) end |
#command ⇒ Object
236 237 238 |
# File 'lib/rom/changeset/stateful.rb', line 236 def command relation.command(command_type, DEFAULT_COMMAND_OPTS.merge(result: result)) end |
#commit ⇒ Object
Commit stateful changeset
200 201 202 |
# File 'lib/rom/changeset/stateful.rb', line 200 def commit command.call(self) end |
#data(data) ⇒ Changeset
Return changeset with data
167 168 169 |
# File 'lib/rom/changeset/stateful.rb', line 167 def data(data) with(__data__: data) end |
#extend(*steps, **options, &block) ⇒ Changeset
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.
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/rom/changeset/stateful.rb', line 148 def extend(*steps, **, &block) if block if steps.size > 0 extend(*steps, ).extend(, &block) else with(pipe: pipe.compose(Pipe.new(block).bind(self), )) end else with(pipe: steps.reduce(pipe.with()) { |a, e| a.compose(pipe[e], ) }) end end |
#inspect ⇒ String
Return string representation of the changeset
245 246 247 |
# File 'lib/rom/changeset/stateful.rb', line 245 def inspect %(#<#{self.class} relation=#{relation.name.inspect} data=#{__data__}>) end |
#map(*steps) ⇒ Changeset #map(&block) ⇒ Changeset #map(*steps, &block) ⇒ Changeset
Pipe changeset’s data using custom steps define on the pipe
131 132 133 |
# File 'lib/rom/changeset/stateful.rb', line 131 def map(*steps, &block) extend(*steps, for_diff: true, &block) end |
#result ⇒ Symbol
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return command result type
231 232 233 |
# File 'lib/rom/changeset/stateful.rb', line 231 def result __data__.is_a?(Array) ? :many : :one end |
#to_a ⇒ Array Also known as: to_ary
Coerce changeset to an array
This will send the data through the pipe
190 191 192 |
# File 'lib/rom/changeset/stateful.rb', line 190 def to_a result == :one ? [to_h] : __data__.map { |element| pipe.call(element) } end |
#to_h ⇒ Hash Also known as: to_hash
Coerce changeset to a hash
This will send the data through the pipe
178 179 180 |
# File 'lib/rom/changeset/stateful.rb', line 178 def to_h pipe.call(__data__) end |