Class: Germinate::Hunk

Inherits:
Array
  • Object
show all
Includes:
SharedStyleAttributes
Defined in:
lib/germinate/hunk.rb

Overview

A Hunk represents a chunk of content. There are different types of Hunk, like Code or Text, which may be formatted differently by the Formatter. At its most basic a Hunk is just a list of Strings, each one representing a single line.

Direct Known Subclasses

CodeHunk, NullHunk, TextHunk

Constant Summary

Constants included from SharedStyleAttributes

SharedStyleAttributes::TEXT_TRANSFORMS

Instance Method Summary collapse

Methods included from SharedStyleAttributes

#copy_attributes!, #copy_shared_style_attributes_from, #disable_all_transforms!, #shared_style_attributes

Constructor Details

#initialize(contents = [], template = {}) ⇒ Hunk

Returns a new instance of Hunk.



14
15
16
17
# File 'lib/germinate/hunk.rb', line 14

def initialize(contents=[], template = {})
  super(contents)
  copy_shared_style_attributes_from(template)
end

Instance Method Details

#[](*args) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/germinate/hunk.rb', line 57

def [](*args)
  returning(super) do |slice|
    if slice.kind_of?(Germinate::Hunk)
      slice.copy_shared_style_attributes_from(self)
    end
  end
end

#format_with(formatter) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/germinate/hunk.rb', line 38

def format_with(formatter)
  raise "Unresolved hunk cannot be formatted!" unless resolved?
  if nested_hunks?
    group_hunks.each do |hunk|
      hunk.format_with(formatter)
    end
  else
    yield formatter
  end
end

#index_matching(pattern, start_index = 0) ⇒ Object



73
74
75
76
77
78
# File 'lib/germinate/hunk.rb', line 73

def index_matching(pattern, start_index=0)
  (start_index...(size)).each { |i|
    return i if pattern === self[i]
  }
  nil
end

#inspectObject



49
50
51
52
53
54
55
# File 'lib/germinate/hunk.rb', line 49

def inspect
  attrs = Germinate::SharedStyleAttributes.fattrs.inject({}) {|attrs, key|
    attrs[key] = send(key)
    attrs
  }
  "#{self.class}:#{super}:#{attrs.inspect}:#{object_id}"
end

#resolve_insertionsObject



28
29
30
31
32
33
34
35
36
# File 'lib/germinate/hunk.rb', line 28

def resolve_insertions
  dup.map!{ |line_or_insertion|
    if line_or_insertion.respond_to?(:resolve)
      line_or_insertion.resolve
    else
      line_or_insertion
    end
  }
end

#slice(*args) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/germinate/hunk.rb', line 65

def slice(*args)
  returning(super) do |slice|
    if slice.kind_of?(Germinate::Hunk)
      slice.copy_shared_style_attributes_from(self)
    end
  end
end

#stripObject

return a copy with leading and trailing whitespace lines removed



24
25
26
# File 'lib/germinate/hunk.rb', line 24

def strip
  Germinate::TextTransforms.strip_blanks.call(self)
end

#to_sObject



19
20
21
# File 'lib/germinate/hunk.rb', line 19

def to_s
  "#{self.class.name}[#{origin}](#{self.size} lines)"
end

#whole_file?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/germinate/hunk.rb', line 80

def whole_file?
  false
end