Class: Fugu

Inherits:
Object
  • Object
show all
Defined in:
lib/fugu.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#delimiterObject

Returns the value of attribute delimiter.



4
5
6
# File 'lib/fugu.rb', line 4

def delimiter
  @delimiter
end

#textObject

Returns the value of attribute text.



4
5
6
# File 'lib/fugu.rb', line 4

def text
  @text
end

Class Method Details

.puff(string) ⇒ Object



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

def self.puff(string)
  f = self.new
  f.text = string
  f.puff
end

.shrink(string, delimiter) ⇒ Object



33
34
35
36
37
38
# File 'lib/fugu.rb', line 33

def self.shrink(string, delimiter)
  f = self.new
  f.text = string
  f.delimiter = delimiter
  f.shrink
end

Instance Method Details

#pad_expression(string) ⇒ Object



65
66
67
68
# File 'lib/fugu.rb', line 65

def pad_expression(string)
  fat = string.split('-')
  "%0#{fat.last.length}d-#{fat.last}" % fat.first.to_i
end

#puffObject



12
13
14
15
16
17
18
19
20
21
# File 'lib/fugu.rb', line 12

def puff
  @text.split('|').collect do |range|
    if range.match(/\{/)
      before, expand_string, after = range.scan(/(.*)\{(.*)\}(.*)/)[0]
      expanded_string = puff_expression(expand_string)
      range = expanded_string.map { |piece| before + piece + after }
    end
    range
  end.flatten
end

#puff!Object



23
24
25
# File 'lib/fugu.rb', line 23

def puff!
  @text = puff
end

#puff_expression(string) ⇒ Object



27
28
29
30
31
# File 'lib/fugu.rb', line 27

def puff_expression(string)
  string.split(",").collect do |v|
    (v[/-/]) ? Range.new(*pad_expression(v).split('-')).to_a : v
  end.flatten
end

#shrinkObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/fugu.rb', line 40

def shrink
  pieces = @text.split("#{@delimiter}")
  first, base = pieces.first, pieces.first
  first.size.times do |len|
    base = first[0, first.size-len]
    break if pieces.all? {|p| p.match(base)}
  end
  diffs = pieces.map {|p| (p.scan(/./) - base.scan(/./)).join}
  base + "{#{shrink_array(diffs)}}"
end

#shrink!Object



51
52
53
# File 'lib/fugu.rb', line 51

def shrink!
  @text = shrink
end

#shrink_array(array) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/fugu.rb', line 55

def shrink_array(array)
  array.flatten.to_ranges.map do |r|
    if r.first == r.last
      r.first
    else
      "#{r.first}-#{r.last}"
    end
  end.join(",")
end