Class: Konjac::Office::Tag

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTag

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

#addedObject

The text with which to replaced the removed content



13
14
15
# File 'lib/konjac/office/tag.rb', line 13

def added
  @added
end

#indicesObject

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

#removedObject

The text that has been removed



10
11
12
# File 'lib/konjac/office/tag.rb', line 10

def removed
  @removed
end

#typeObject

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(tags, 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}"
    tags.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)
  tags = []
  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?
        tags << 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
  tags << temp unless temp.blank?
  tags
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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


53
54
55
# File 'lib/konjac/office/tag.rb', line 53

def special?
  !!@type
end

#to_sObject

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