Module: Gorp::StringEditingFunctions

Defined in:
lib/gorp/edit.rb,
lib/gorp/edit.rb

Instance Method Summary collapse

Instance Method Details

#allObject



153
154
155
# File 'lib/gorp/edit.rb', line 153

def all
  self
end

#all=(replacement) ⇒ Object



157
158
159
# File 'lib/gorp/edit.rb', line 157

def all=(replacement)
  self[/(.*)/m,1]=replacement
end

#clear_all_marksObject



127
128
129
130
# File 'lib/gorp/edit.rb', line 127

def clear_all_marks
  self.gsub! /^ *(\/\/#)\s*(START|END)(_HIGHLIGHT|:\w+)\n/, ''
  self.gsub! /^\s*(#|<!--|\/\*|\/\/#)\s*(START|END)(_HIGHLIGHT|:\w+)\s*(-->|\*\/)?\n/, ''
end

#clear_highlightsObject



122
123
124
125
# File 'lib/gorp/edit.rb', line 122

def clear_highlights
  self.gsub! /^ *(\/\/#)\s*(START|END)_HIGHLIGHT\n/, ''
  self.gsub! /^\s*(#|<!--|\/\*)\s*(START|END)_HIGHLIGHT\s*(-->|\*\/)?\n/, ''
end

#dcl(name, *options, &block) ⇒ Object

Raises:

  • (IndexError)


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/gorp/edit.rb', line 102

def dcl(name, *options, &block)
  options[-1] = {:mark => name} if options.last == :mark

  re = Regexp.new '^(\s*)(class|def|test)\s+"?' + name +
    '"?.*?\n\1end\n', Regexp::MULTILINE
  raise IndexError.new('regexp not matched') unless match(re)

  self.sub!(re) do |lines|
    lines.extend Gorp::StringEditingFunctions
    lines.instance_exec(lines, &block) if block_given?
    lines.mark(options.last[:mark]) if options.last.respond_to? :[]=
    lines.highlight if options.last == :highlight
    lines
  end
end

#dupObject



118
119
120
# File 'lib/gorp/edit.rb', line 118

def dup
  super.extend(Gorp::StringEditingFunctions)
end

#edit(from, *options, &block) ⇒ Object

Raises:

  • (IndexError)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/gorp/edit.rb', line 86

def edit(from, *options, &block)
  if from.instance_of? String
    from = Regexp.new('.*' + Regexp.escape(from) + '.*')
  end

  raise IndexError.new('regexp not matched') unless match(from)

  sub!(from) do |base|
    base.extend Gorp::StringEditingFunctions
    base.instance_exec(base, &block) if block_given?
    base.highlight if options.include? :highlight
    base.mark(options.last[:mark]) if options.last.respond_to? :keys
    base
  end
end

#highlightObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gorp/edit.rb', line 35

def highlight
  if self =~ /^\s*<[%!\w].*>/
    # HTML, possibly XML
    start = '<!-- START_HIGHLIGHT -->'
    close = '<!-- END_HIGHLIGHT -->'
  elsif self =~ /^\s*[.#]\w+[, ].*\{$/
    # CSS
    start = '/* START_HIGHLIGHT */'
    close = '/* END_HIGHLIGHT */'
  elsif self =~ /;\s*\}?$/
    # JavaScript
    start = '//#START_HIGHLIGHT'
    close = '//#END_HIGHLIGHT'
  elsif self =~ /->$/
    # CoffeeScript
    start = '//#START_HIGHLIGHT'
    close = '//#END_HIGHLIGHT'
  else
    # Other, most likely Ruby
    start = '#START_HIGHLIGHT'
    close = '#END_HIGHLIGHT'
  end
  
  if self =~ /\n\z/
    self[/(.*)/m,1] = "#{start}\n#{self}#{close}\n"
  else
    self[/(.*)/m,1] = "#{start}\n#{self}\n#{close}"
  end
end

#instance_exec(*arguments, &block) ⇒ Object



26
27
28
# File 'lib/gorp/edit.rb', line 26

def instance_exec(*arguments, &block)
  block.bind(self)[*arguments]
end

#mark(name) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/gorp/edit.rb', line 65

def mark name
  return unless name

  if self =~ /^\s*<[%!\w].*>/
    start = "<!-- START:#{name} -->"
    close = "<!-- END:#{name} -->"
  elsif self =~ /;\s*\}?$/
    start = "//#START:#{name}"
    close = "//#END:#{name}"
  else
    start = "#START:#{name}"
    close = "#END:#{name}"
  end

  if self =~ /\n\z/
    self[/(.*)/m,1] = "#{start}\n#{self}#{close}\n"
  else
    self[/(.*)/m,1] = "#{start}\n#{self}\n#{close}"
  end
end

#msub(pattern, replacement, option = nil) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/gorp/edit.rb', line 132

def msub pattern, replacement, option=nil
  if option == :highlight
    replacement.extend Gorp::StringEditingFunctions
    replacement.highlight if option == :highlight
  elsif option.respond_to? :keys
    replacement.extend Gorp::StringEditingFunctions
    replacement.mark(option[:mark]) 
  elsif option == :optional
    return unless match(pattern)
  end

  if replacement =~ /\\[1-9]/
    replacement.gsub! /\\([1-9])/, '#{$\1}'
    replacement = replacement.inspect.gsub('\#','#')
    match(pattern)
    replacement = eval(replacement)
  end

  self[pattern, 1] = replacement
end