Class: C0::DiffBuilder
- Inherits:
-
Object
- Object
- C0::DiffBuilder
- 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
-
#assert(text) ⇒ Object
One record: assert that
textis present once; nothing changes. -
#bytes ⇒ Object
(also: #to_s)
The diff's bytes so far (a binary String).
-
#delete(old_text, context_before = "", context_after = "") ⇒ Object
One record: remove
old_text, found once after the cursor, between optional anchors. - #eot ⇒ Object
-
#file(path) {|_self| ... } ⇒ Object
FS and the path (a name); the block writes the file's records.
-
#initialize(heading: true) ⇒ DiffBuilder
constructor
A new instance of DiffBuilder.
-
#insert_after(anchor, text) ⇒ Object
One record: insert
textafteranchor(␞anchor␟␚␟text). -
#note(text) ⇒ Object
BEL and a note (a hunk kept without applying goes in one).
-
#record {|PatternBuilder.new(@b)| ... } ⇒ Object
One anchored pattern: the block gets a PatternBuilder (
anchor,sub). -
#replace(context_before, old_text, new_text, context_after = "") ⇒ Object
One record: anchor, old␚␟new, anchor (empty anchors are omitted).
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
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.
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).
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 |