Class: Squish::Bucket
- Inherits:
-
Object
- Object
- Squish::Bucket
- Defined in:
- lib/squish.rb
Instance Method Summary collapse
-
#<<(document) ⇒ Object
Adds a document to the bucket.
-
#compress(document) ⇒ Object
Returns the compression ratio for the given document with this bucket.
-
#documents ⇒ Object
Returns the list of documents contained within the bucket.
-
#initialize(name) ⇒ Bucket
constructor
Creates a new bucket with a given name.
-
#invalidate ⇒ Object
Invalidates the bucket compression data.
-
#magnets ⇒ Object
Magnets are Strings or Regexps which can be attached to a bucket.
-
#name ⇒ Object
Returns the name of the bucket.
Constructor Details
#initialize(name) ⇒ Bucket
Creates a new bucket with a given name.
62 63 64 |
# File 'lib/squish.rb', line 62 def initialize(name) @name = name end |
Instance Method Details
#<<(document) ⇒ Object
Adds a document to the bucket. The supplied document must be a Hash. Suggested convention is to use a Hash such as this:
{
:name => "Bob Aman",
:email => "[email protected]",
:body => <<-TEXT
This is some example text from a hypothetical comment I left on
someone's blog.
TEXT
}
Supplying a String will convert the string to the form:
{
:body => string
}
Automatically invalidates the previously calculated bucket data.
111 112 113 114 115 116 |
# File 'lib/squish.rb', line 111 def <<(document) self.invalidate() document = {:body => document} if document.kind_of?(String) self.documents << document return self.documents end |
#compress(document) ⇒ Object
Returns the compression ratio for the given document with this bucket. The lower this number is, the better the fit.
Supplying a String will convert the string to the form:
{
:body => string
}
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/squish.rb', line 127 def compress(document) document = {:body => document} if document.kind_of?(String) # Magically compress anything matched by the magnets to nothing. # This strongly attracts the document to this bucket. magnetized_document = document.dup for magnet in self.magnets regexp = nil if magnet.kind_of?(String) regexp = Regexp.new(Regexp.escape(magnet)) elsif magnet.kind_of?(Regexp) regexp = magnet end if regexp != nil for key, value in magnetized_document value.gsub!(regexp, "") end end end document_bytes = Marshal.dump( Squish.filter_document(magnetized_document)) document_compressed_binary = "" document_compressed_bytes = "" sorted_symbol_table = self.symbol_table.sort { |a, b| b[0].size <=> a[0].size } while document_bytes.size > 0 for symbol, coding in sorted_symbol_table symbol_regexp = Regexp.new("^" + Regexp.escape(symbol)) if document_bytes =~ symbol_regexp document_bytes.gsub!(symbol_regexp, "") document_compressed_binary << coding break end end end while document_compressed_binary != nil && document_compressed_binary.size > 0 document_compressed_bytes << document_compressed_binary[0...8].to_i(2).chr document_compressed_binary = document_compressed_binary[8..-1] end return (document_compressed_bytes.size.to_f / Marshal.dump(document).size.to_f) end |
#documents ⇒ Object
Returns the list of documents contained within the bucket. Each document is simply a Hash object.
73 74 75 76 77 78 |
# File 'lib/squish.rb', line 73 def documents if !defined?(@documents) || @documents.nil? @documents = [] end return @documents end |
#invalidate ⇒ Object
Invalidates the bucket compression data. This method should be called any time the bucket’s list of documents changes. The << method calls this method automatically.
176 177 178 179 |
# File 'lib/squish.rb', line 176 def invalidate @tree = nil @symbol_table = nil end |
#magnets ⇒ Object
Magnets are Strings or Regexps which can be attached to a bucket. They cause any incoming document that matches them to be very, very strongly attracted to the bucket that they are attached to. In essence, it makes the string that the magnet matches infinitely compressible by that bucket.
85 86 87 88 89 90 |
# File 'lib/squish.rb', line 85 def magnets if !defined?(@magnets) || @magnets.nil? @magnets = [] end return @magnets end |
#name ⇒ Object
Returns the name of the bucket.
67 68 69 |
# File 'lib/squish.rb', line 67 def name return @name end |