Class: Konjac::Office::Tag
- Inherits:
-
Object
- Object
- Konjac::Office::Tag
- Defined in:
- lib/konjac/office/tag.rb
Overview
A tag item, used to export and import data to and from the document
Constant Summary collapse
- TAG_MATCHES =
A list of regular expressions for parsing a document in unified diff-esque style into Tags
{ :header => /^(?:---|\+\+\+)/, :comment => /^\@\@ ([a-z]*) ?([\d, ]+) \@\@$/i, :removed => /^-(.*)/, :added => /^\+(.*)/, }
Instance Attribute Summary collapse
-
#added ⇒ Object
The text with which to replaced the removed content.
-
#indices ⇒ Object
The indices used to scan the document’s hierarchy to reach the item.
-
#removed ⇒ Object
The text that has been removed.
-
#type ⇒ Object
The type, used to identify special objects such as a
:shape
.
Class Method Summary collapse
-
.dump(tags, path, opts = {}) ⇒ Object
Dumps the tags into a .diff document with the same name as the document from which those tags were extracted.
-
.load(path) ⇒ Object
Loads the tags for the specified document path or the tags file itself.
Instance Method Summary collapse
-
#blank? ⇒ Boolean
Determines whether the tag is blank, that is, if it has no
indices
or bothremoved
andadded
are empty. -
#changed? ⇒ Boolean
Whether the tag has changed content.
-
#default? ⇒ Boolean
Whether the tag’s content if for the default kind of object.
-
#initialize ⇒ Tag
constructor
Creates a new tag item.
-
#special? ⇒ Boolean
Whether the tag’s content if for a special kind of object.
-
#to_s ⇒ Object
Converts the tag to a string.
Constructor Details
#initialize ⇒ Tag
Creates a new tag item
20 21 22 23 24 25 |
# File 'lib/konjac/office/tag.rb', line 20 def initialize @indices = nil @removed = [] @added = [] @type = nil end |
Instance Attribute Details
#added ⇒ Object
The text with which to replaced the removed content
13 14 15 |
# File 'lib/konjac/office/tag.rb', line 13 def added @added end |
#indices ⇒ Object
The indices used to scan the document’s hierarchy to reach the item
7 8 9 |
# File 'lib/konjac/office/tag.rb', line 7 def indices @indices end |
#removed ⇒ Object
The text that has been removed
10 11 12 |
# File 'lib/konjac/office/tag.rb', line 10 def removed @removed end |
#type ⇒ Object
The type, used to identify special objects such as a :shape
. Defaults to nil
.
17 18 19 |
# File 'lib/konjac/office/tag.rb', line 17 def type @type end |
Class Method Details
.dump(tags, path, opts = {}) ⇒ Object
Dumps the tags into a .diff document with the same name as the document from which those tags were extracted
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/konjac/office/tag.rb', line 93 def dump(, path, opts = {}) original_path = path.sub(/\.diff$/, "") diff_path = "#{original_path}.diff" File.open(diff_path, "w") do |file| file.puts "--- #{original_path}" file.puts "+++ #{original_path}" .each do |tag| file.puts tag.to_s end end end |
.load(path) ⇒ Object
Loads the tags for the specified document path or the tags file itself
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/konjac/office/tag.rb', line 68 def load(path) = [] temp = Tag.new File.read("#{path.sub(/\.diff$/, "")}.diff").each_line do |line| case line when TAG_MATCHES[:header] when TAG_MATCHES[:comment] unless temp.blank? << temp temp = Tag.new end temp.type = $1.to_sym unless $1.nil? temp.indices = $2.scan(/\d+/).map(&:to_i) # this changes $~ when TAG_MATCHES[:removed] temp.removed << $1 when TAG_MATCHES[:added] temp.added << $1 end end << temp unless temp.blank? end |
Instance Method Details
#blank? ⇒ Boolean
Determines whether the tag is blank, that is, if it has no indices
or both removed
and added
are empty
34 35 36 |
# File 'lib/konjac/office/tag.rb', line 34 def blank? @indices.nil? || (@removed.join.empty? && @added.join.empty?) end |
#changed? ⇒ Boolean
Whether the tag has changed content
28 29 30 |
# File 'lib/konjac/office/tag.rb', line 28 def changed? @removed != @added end |
#default? ⇒ Boolean
Whether the tag’s content if for the default kind of object
48 49 50 |
# File 'lib/konjac/office/tag.rb', line 48 def default? @type.nil? end |
#special? ⇒ Boolean
Whether the tag’s content if for a special kind of object
53 54 55 |
# File 'lib/konjac/office/tag.rb', line 53 def special? !!@type end |
#to_s ⇒ Object
Converts the tag to a string
39 40 41 42 43 44 45 |
# File 'lib/konjac/office/tag.rb', line 39 def to_s <<-eos @@ #{@type.nil? ? "" : "#{@type} "}#{@indices.join(",")} @@ -#{@removed.join("\n-")} +#{@added.join("\n+")} eos end |