Class: ROM::Changeset::Stateful Abstract
- Inherits:
-
ROM::Changeset
- Object
- ROM::Changeset
- ROM::Changeset::Stateful
- Defined in:
- lib/rom/repository/changeset/stateful.rb
Overview
Stateful changesets carry data and can transform it into a different structure compatible with a persistence backend
Constant Summary collapse
Instance Attribute Summary collapse
-
#__data__ ⇒ Hash
readonly
private
The relation data.
-
#pipe ⇒ Changeset::Pipe
readonly
private
Data transformation pipe.
Class Method Summary collapse
-
.default_pipe(context) ⇒ Pipe
Build default pipe object.
- .inherited(klass) ⇒ Object private
-
.map(&block) ⇒ Array<Pipe>, Transproc::Function
Define a changeset mapping.
- .pipes ⇒ Object private
Instance Method Summary collapse
-
#associate(other, name) ⇒ 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.
-
#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.
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.
225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/rom/repository/changeset/stateful.rb', line 225 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/repository/changeset/stateful.rb', line 16 option :__data__, reader: true, optional: true, default: proc { nil } |
#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.
Returns data transformation pipe.
21 22 23 |
# File 'lib/rom/repository/changeset/stateful.rb', line 21 option :pipe, reader: true, accept: [Proc, Pipe], default: -> changeset { changeset.class.default_pipe(changeset) } |
Class Method Details
.default_pipe(context) ⇒ Pipe
Build default pipe object
This can be overridden in a custom changeset subclass
73 74 75 |
# File 'lib/rom/repository/changeset/stateful.rb', line 73 def self.default_pipe(context) pipes.size > 0 ? pipes.map { |p| p.bind(context) }.reduce(:>>) : EMPTY_PIPE 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.
78 79 80 81 82 |
# File 'lib/rom/repository/changeset/stateful.rb', line 78 def self.inherited(klass) return if klass == ROM::Changeset super klass.instance_variable_set(:@__pipes__, pipes ? pipes.dup : EMPTY_ARRAY) end |
.map(&block) ⇒ Array<Pipe>, Transproc::Function
Define a changeset mapping
Subsequent mapping definitions will be composed together and applied in the order they way defined
60 61 62 63 64 65 66 |
# File 'lib/rom/repository/changeset/stateful.rb', line 60 def self.map(&block) if block.arity.zero? 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.
85 86 87 |
# File 'lib/rom/repository/changeset/stateful.rb', line 85 def self.pipes @__pipes__ end |
Instance Method Details
#associate(other, name) ⇒ Object
Associate a changeset with another changeset or hash-like object
190 191 192 |
# File 'lib/rom/repository/changeset/stateful.rb', line 190 def associate(other, name) Associated.new(self, other, association: name) end |
#command ⇒ Object
204 205 206 |
# File 'lib/rom/repository/changeset/stateful.rb', line 204 def command command_compiler.(command_type, relation_identifier, DEFAULT_COMMAND_OPTS.merge(result: result)) end |
#commit ⇒ Object
Commit stateful changeset
168 169 170 |
# File 'lib/rom/repository/changeset/stateful.rb', line 168 def commit command.call(self) end |
#data(data) ⇒ Changeset
Return changeset with data
135 136 137 |
# File 'lib/rom/repository/changeset/stateful.rb', line 135 def data(data) with(__data__: data) end |
#inspect ⇒ String
Return string representation of the changeset
213 214 215 |
# File 'lib/rom/repository/changeset/stateful.rb', line 213 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
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/rom/repository/changeset/stateful.rb', line 116 def map(*steps, &block) if block if steps.size > 0 map(*steps).map(&block) else with(pipe: pipe >> Pipe.new(block).bind(self)) end else with(pipe: steps.reduce(pipe) { |a, e| a >> pipe[e] }) end 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
199 200 201 |
# File 'lib/rom/repository/changeset/stateful.rb', line 199 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
158 159 160 |
# File 'lib/rom/repository/changeset/stateful.rb', line 158 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
146 147 148 |
# File 'lib/rom/repository/changeset/stateful.rb', line 146 def to_h pipe.call(__data__) end |