Class: Packr::Words

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

Defined Under Namespace

Classes: Item

Constant Summary collapse

WORDS =
/\w+/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(script) ⇒ Words

Returns a new instance of Words.



7
8
9
10
# File 'lib/packr/words.rb', line 7

def initialize(script)
  script.to_s.scan(WORDS).each { |word| add(word) }
  encode!
end

Instance Attribute Details

#wordsObject

Returns the value of attribute words.



5
6
7
# File 'lib/packr/words.rb', line 5

def words
  @words
end

Instance Method Details

#add(word) ⇒ Object



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

def add(word)
  @words ||= []
  @words << (stored_word = Item.new(word)) unless stored_word = get(word)
  word = stored_word
  word.count = word.count + 1
  word
end

#get(word) ⇒ Object



20
21
22
# File 'lib/packr/words.rb', line 20

def get(word)
  @words.find { |w| w.word == word.to_s }
end

#has?(word) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/packr/words.rb', line 24

def has?(word)
  !!(get word)
end

#sizeObject



28
29
30
# File 'lib/packr/words.rb', line 28

def size
  @words.size
end

#to_sObject



32
33
34
# File 'lib/packr/words.rb', line 32

def to_s
  @words.join("|")
end