Class: CoPlan::Plans::ApplyOperations

Inherits:
Object
  • Object
show all
Defined in:
app/services/coplan/plans/apply_operations.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content:, operations:) ⇒ ApplyOperations

Returns a new instance of ApplyOperations.



8
9
10
11
12
# File 'app/services/coplan/plans/apply_operations.rb', line 8

def initialize(content:, operations:)
  @content = content.dup
  @operations = operations
  @applied = []
end

Class Method Details

.call(content:, operations:) ⇒ Object



4
5
6
# File 'app/services/coplan/plans/apply_operations.rb', line 4

def self.call(content:, operations:)
  new(content:, operations:).call
end

Instance Method Details

#callObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/services/coplan/plans/apply_operations.rb', line 14

def call
  @operations.each_with_index do |op, index|
    op = op.transform_keys(&:to_s)
    applied_data = case op["op"]
    when "replace_exact"
      apply_replace_exact(op, index)
    when "insert_under_heading"
      apply_insert_under_heading(op, index)
    when "delete_paragraph_containing"
      apply_delete_paragraph_containing(op, index)
    else
      raise OperationError, "Operation #{index}: unknown op '#{op["op"]}'"
    end
    @applied << applied_data
  end

  { content: @content, applied: @applied }
end