Module: LockBlock

Defined in:
lib/lock_block.rb,
lib/lock_block/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.broken_locks(source) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lock_block.rb', line 20

def LockBlock.broken_locks source
  broken = []
  source.lines.each_with_index do |line, number|
    if tags(line).any?
      got      = tags(line).first
      expected = tag innards(source, got)
      if got != expected
        broken.push({line: number+1, expected: expected, got: got})
      end
    end
  end
  broken
end

.innards(source, tag) ⇒ Object



49
50
51
52
# File 'lib/lock_block.rb', line 49

def LockBlock.innards source, tag
  match = source.match /# lock do #{tag}\n(.*?)# lock end #{tag}/m
  match ? match[1] : ''
end

.lock(source) ⇒ Object



6
7
8
9
10
# File 'lib/lock_block.rb', line 6

def LockBlock.lock source
  indent = source.match(/^(\s+)/) ? $1 : ''
  tag    = tag source
  "#{indent}# lock do #{tag}\n#{source}#{indent}# lock end #{tag}"
end

.resolve(source) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/lock_block.rb', line 12

def LockBlock.resolve source
  tags(source).each do |tag|
    resolved_tag = tag innards(source, tag)
    source = update_tag source, tag, resolved_tag
  end
  source
end

.tag(source) ⇒ Object



34
35
36
37
38
39
# File 'lib/lock_block.rb', line 34

def LockBlock.tag source
  tokens = Ripper.tokenize(source).select do |t|
    t.gsub(/\s+/, "") != ''
  end
  Digest::SHA1.hexdigest tokens.to_s
end

.tags(source) ⇒ Object



41
42
43
# File 'lib/lock_block.rb', line 41

def LockBlock.tags source
  source.scan(/# lock do ([a-f0-9]{40})/).map &:first
end

.update_tag(source, old_tag, new_tag) ⇒ Object



45
46
47
# File 'lib/lock_block.rb', line 45

def LockBlock.update_tag source, old_tag, new_tag
  source.gsub /# lock (do|end) #{old_tag}/, "# lock \\1 #{new_tag}"
end