Class: Packr::Encoder

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

Direct Known Subclasses

Base62, Privates

Instance Method Summary collapse

Constructor Details

#initialize(pattern = nil, encoder = nil, ignore = nil) ⇒ Encoder

Returns a new instance of Encoder.



4
5
6
7
8
# File 'lib/packr/encoder.rb', line 4

def initialize(pattern = nil, encoder = nil, ignore = nil)
  @parser = Parser.new(ignore)
  @parser.put(pattern, "") if pattern
  @encoder = encoder
end

Instance Method Details

#encode(script) ⇒ Object



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

def encode(script)
  words = search(script)
  words.sort!
  index = 0
  words.each do |word, key|
    word.encoded = @encoder.call(index)
    index += 1
  end
  @parser.put_at(-1, lambda { |word, *args|
    words.get(word).encoded
  })
  @parser.exec(script)
end

#search(script) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/packr/encoder.rb', line 10

def search(script)
  words = Words.new
  @parser.put_at(-1, lambda { |word, *args|
    words.add(word)
  })
  @parser.exec(script)
  words
end