Class: ROM::Changeset

Inherits:
Object
  • Object
show all
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

Direct Known Subclasses

Create, Update

Defined Under Namespace

Classes: Create, Pipe, Update

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options = 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, options)
    else
      response
    end
  else
    super
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



22
23
24
# File 'lib/rom/repository/changeset.rb', line 22

def data
  @data
end

#pipeChangeset::Pipe (readonly)

Returns data transformation pipe.

Returns:



12
13
14
# File 'lib/rom/repository/changeset.rb', line 12

option :pipe, reader: true, accept: [Proc, Pipe], default: -> changeset {
  changeset.class.default_pipe
}

#relationObject (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_pipePipe

Build default pipe object

This can be overridden in a custom changeset subclass

Returns:



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

Parameters:

  • *steps (Array<Symbol>)

    A list of mapping steps

Returns:



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_hHash Also known as: to_hash

Coerce changeset to a hash

This will send the data through the pipe

Returns:

  • (Hash)


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

Parameters:

  • new_options (Hash)

    The new options

Returns:



70
71
72
# File 'lib/rom/repository/changeset.rb', line 70

def with(new_options)
  self.class.new(relation, data, options.merge(new_options))
end