Class: ROM::Changeset
- Inherits:
-
Object
- Object
- ROM::Changeset
- Includes:
- Options
- Defined in:
- lib/rom/repository/changeset.rb,
lib/rom/repository/changeset/pipe.rb,
lib/rom/repository/changeset/create.rb,
lib/rom/repository/changeset/update.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#pipe ⇒ Changeset::Pipe
readonly
Data transformation pipe.
-
#relation ⇒ Object
readonly
Returns the value of attribute relation.
Class Method Summary collapse
-
.default_pipe ⇒ Pipe
Build default pipe object.
Instance Method Summary collapse
-
#initialize(relation, data, options = EMPTY_HASH) ⇒ Changeset
constructor
private
A new instance of Changeset.
-
#map(*steps) ⇒ Changeset
Pipe changeset’s data using custom steps define on the pipe.
-
#to_h ⇒ Hash
(also: #to_hash)
Coerce changeset to a hash.
-
#with(new_options) ⇒ Changeset
private
Return a new changeset with updated options.
Constructor Details
#initialize(relation, data, options = EMPTY_HASH) ⇒ Changeset
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 a new instance of Changeset.
34 35 36 37 38 |
# File 'lib/rom/repository/changeset.rb', line 34 def initialize(relation, data, = EMPTY_HASH) @relation = relation @data = data super end |
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.
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/rom/repository/changeset.rb', line 82 def method_missing(meth, *args, &block) if data.respond_to?(meth) response = data.__send__(meth, *args, &block) if response.is_a?(Hash) self.class.new(relation, response, ) else response end else super end end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
22 23 24 |
# File 'lib/rom/repository/changeset.rb', line 22 def data @data end |
#pipe ⇒ Changeset::Pipe (readonly)
Returns data transformation pipe.
12 13 14 |
# File 'lib/rom/repository/changeset.rb', line 12 option :pipe, reader: true, accept: [Proc, Pipe], default: -> changeset { changeset.class.default_pipe } |
#relation ⇒ Object (readonly)
Returns the value of attribute relation.
18 19 20 |
# File 'lib/rom/repository/changeset.rb', line 18 def relation @relation end |
Class Method Details
.default_pipe ⇒ Pipe
Build default pipe object
This can be overridden in a custom changeset subclass
29 30 31 |
# File 'lib/rom/repository/changeset.rb', line 29 def self.default_pipe Pipe.new end |
Instance Method Details
#map(*steps) ⇒ Changeset
Pipe changeset’s data using custom steps define on the pipe
47 48 49 |
# File 'lib/rom/repository/changeset.rb', line 47 def map(*steps) with(pipe: steps.reduce(pipe) { |a, e| a >> pipe.class[e] }) end |
#to_h ⇒ Hash Also known as: to_hash
Coerce changeset to a hash
This will send the data through the pipe
58 59 60 |
# File 'lib/rom/repository/changeset.rb', line 58 def to_h pipe.call(data) end |
#with(new_options) ⇒ Changeset
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 a new changeset with updated options
70 71 72 |
# File 'lib/rom/repository/changeset.rb', line 70 def with() self.class.new(relation, data, .merge()) end |