Class: C0::DiffBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/c0/diff.rb

Overview

Writes a C0DIFF document over a C0::Builder: ␖␁shape␟diff, then files (file) of records (record, replace, insert_after, delete, assert), notes and an optional eot. Every method returns the builder.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(heading: true) ⇒ DiffBuilder

Returns a new instance of DiffBuilder.



114
115
116
117
# File 'lib/c0/diff.rb', line 114

def initialize(heading: true)
  @b = Builder.new
  @b.syn(shape: "diff") if heading
end

Class Method Details

.build(heading: true) {|d| ... } ⇒ Object

Yields:

  • (d)


108
109
110
111
112
# File 'lib/c0/diff.rb', line 108

def self.build(heading: true)
  d = new(heading: heading)
  yield d
  d.bytes
end

Instance Method Details

#assert(text) ⇒ Object

One record: assert that text is present once; nothing changes.



156
# File 'lib/c0/diff.rb', line 156

def assert(text) = record { |r| r.anchor(text) }

#bytes ⇒ Object Also known as: to_s

The diff's bytes so far (a binary String).



170
# File 'lib/c0/diff.rb', line 170

def bytes = @b.bytes

#delete(old_text, context_before = "", context_after = "") ⇒ Object

One record: remove old_text, found once after the cursor, between optional anchors.



151
152
153
# File 'lib/c0/diff.rb', line 151

def delete(old_text, context_before = "", context_after = "")
  replace(context_before, old_text, "", context_after)
end

#eot ⇒ Object



164
165
166
167
# File 'lib/c0/diff.rb', line 164

def eot
  @b.eot
  self
end

#file(path) {|_self| ... } ⇒ Object

FS and the path (a name); the block writes the file's records.

Yields:

  • (_self)

Yield Parameters:



120
121
122
123
124
# File 'lib/c0/diff.rb', line 120

def file(path)
  @b.fs(path)
  yield self if block_given?
  self
end

#insert_after(anchor, text) ⇒ Object

One record: insert text after anchor (␞anchor␟␚␟text).



143
144
145
146
147
148
# File 'lib/c0/diff.rb', line 143

def insert_after(anchor, text)
  record do |r|
    r.anchor(anchor)
    r.sub("", text)
  end
end

#note(text) ⇒ Object

BEL and a note (a hunk kept without applying goes in one).



159
160
161
162
# File 'lib/c0/diff.rb', line 159

def note(text)
  @b.note(text)
  self
end

#record {|PatternBuilder.new(@b)| ... } ⇒ Object

One anchored pattern: the block gets a PatternBuilder (anchor, sub).

Yields:



127
128
129
130
131
# File 'lib/c0/diff.rb', line 127

def record
  @b.rs
  yield PatternBuilder.new(@b) if block_given?
  self
end

#replace(context_before, old_text, new_text, context_after = "") ⇒ Object

One record: anchor, old␚␟new, anchor (empty anchors are omitted).



134
135
136
137
138
139
140
# File 'lib/c0/diff.rb', line 134

def replace(context_before, old_text, new_text, context_after = "")
  record do |r|
    r.anchor(context_before) unless context_before.empty?
    r.sub(old_text, new_text)
    r.anchor(context_after) unless context_after.empty?
  end
end