Class: ConcurrentPipeline::Changeset

Inherits:
Object
  • Object
show all
Defined in:
lib/concurrent_pipeline/changeset.rb

Defined Under Namespace

Classes: CreateDelta, InitialDelta, Result, UpdateDelta

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(registry:) ⇒ Changeset

Returns a new instance of Changeset.



104
105
106
107
# File 'lib/concurrent_pipeline/changeset.rb', line 104

def initialize(registry:)
  @registry = registry
  @deltas = []
end

Instance Attribute Details

#deltasObject (readonly)

Returns the value of attribute deltas.



103
104
105
# File 'lib/concurrent_pipeline/changeset.rb', line 103

def deltas
  @deltas
end

#registryObject (readonly)

Returns the value of attribute registry.



103
104
105
# File 'lib/concurrent_pipeline/changeset.rb', line 103

def registry
  @registry
end

Class Method Details

.from_json(registry:, json:) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/concurrent_pipeline/changeset.rb', line 84

def self.from_json(registry:, json:)
  type_map = {
    initial: InitialDelta,
    create: CreateDelta,
    update: UpdateDelta,
  }

  new(
    registry: registry
  ).tap do |changeset|
    json.fetch(:changes).each do |change|
      type_map
        .fetch(change.fetch(:action))
        .from_json(change)
        .then { changeset.deltas << _1 }
    end
  end
end

Instance Method Details

#applyObject



123
124
125
# File 'lib/concurrent_pipeline/changeset.rb', line 123

def apply(...)
  deltas.map { _1.apply(...) }
end

#as_jsonObject



127
128
129
130
131
# File 'lib/concurrent_pipeline/changeset.rb', line 127

def as_json(...)
  {
    changes: deltas.map { _1.as_json(...) }
  }
end

#create(type, attributes) ⇒ Object



113
114
115
116
# File 'lib/concurrent_pipeline/changeset.rb', line 113

def create(type, attributes)
  with_id = { id: SecureRandom.uuid }.merge(attributes)
  @deltas << CreateDelta.new(type: type, attributes: with_id)
end

#deltas?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/concurrent_pipeline/changeset.rb', line 109

def deltas?
  !@deltas.empty?
end

#update(model, delta) ⇒ Object



118
119
120
121
# File 'lib/concurrent_pipeline/changeset.rb', line 118

def update(model, delta)
  type = registry.type_for(model.class)
  @deltas << UpdateDelta.new(id: model.id, type: type, delta: delta)
end