contentz 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +20 -0
- data/README.rdoc +21 -0
- data/Rakefile +70 -0
- data/VERSION.yml +4 -0
- data/lib/contentz.rb +29 -0
- data/test/config.rb +2 -0
- data/test/contentz_test.rb +60 -0
- data/test/schema.rb +5 -0
- data/test/setup.rb +23 -0
- data/test/test_helper.rb +21 -0
- metadata +78 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Ben Pickles
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
= contentz
|
2
|
+
|
3
|
+
Contentz (pronounced "content zee") is an ActiveRecord extension that uses Zlib to compress/uncompress attribute data on write/read.
|
4
|
+
|
5
|
+
== Usage
|
6
|
+
|
7
|
+
Require the gem in <tt>config/environment.rb</tt>:
|
8
|
+
|
9
|
+
Rails::Initializer.run do |config|
|
10
|
+
config.gem 'benpickles-contentz', :lib => 'contentz', :source => 'http://gems.github.com'
|
11
|
+
end
|
12
|
+
|
13
|
+
Use the <tt>contentz</tt> method in any ActiveRecord model to apply compression to attribute(s):
|
14
|
+
|
15
|
+
class Article < ActiveRecord::Base
|
16
|
+
contentz :body
|
17
|
+
end
|
18
|
+
|
19
|
+
== Copyright
|
20
|
+
|
21
|
+
Copyright (c) 2009 Ben Pickles. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "contentz"
|
8
|
+
gem.summary = 'Contentz (pronounced "content zee") is an ActiveRecord extension that uses Zlib to compress/uncompress attribute data on write/read.'
|
9
|
+
gem.email = "spideryoung@gmail.com"
|
10
|
+
gem.homepage = "http://github.com/benpickles/contentz"
|
11
|
+
gem.authors = ["Ben Pickles"]
|
12
|
+
gem.add_dependency "activerecord"
|
13
|
+
end
|
14
|
+
rescue LoadError
|
15
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
16
|
+
end
|
17
|
+
|
18
|
+
require 'rake/testtask'
|
19
|
+
Rake::TestTask.new(:test) do |test|
|
20
|
+
test.libs << 'lib' << 'test'
|
21
|
+
test.pattern = 'test/**/*_test.rb'
|
22
|
+
test.verbose = true
|
23
|
+
end
|
24
|
+
|
25
|
+
begin
|
26
|
+
require 'rcov/rcovtask'
|
27
|
+
Rcov::RcovTask.new do |test|
|
28
|
+
test.libs << 'test'
|
29
|
+
test.pattern = 'test/**/*_test.rb'
|
30
|
+
test.verbose = true
|
31
|
+
end
|
32
|
+
rescue LoadError
|
33
|
+
task :rcov do
|
34
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
task :default => :test
|
39
|
+
|
40
|
+
require 'rake/rdoctask'
|
41
|
+
Rake::RDocTask.new do |rdoc|
|
42
|
+
if File.exist?('VERSION.yml')
|
43
|
+
config = YAML.load(File.read('VERSION.yml'))
|
44
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
45
|
+
else
|
46
|
+
version = ""
|
47
|
+
end
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "contentz #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
54
|
+
|
55
|
+
require File.join(File.dirname(__FILE__), 'test', 'config')
|
56
|
+
|
57
|
+
namespace :db do
|
58
|
+
desc 'Build the MySQL test database.'
|
59
|
+
task :build do
|
60
|
+
`mysqladmin -u #{DB_USER} create #{DB_NAME}`
|
61
|
+
end
|
62
|
+
|
63
|
+
desc 'Drop the MySQL test database.'
|
64
|
+
task :drop do
|
65
|
+
`mysqladmin -u #{DB_USER} -f drop #{DB_NAME}`
|
66
|
+
end
|
67
|
+
|
68
|
+
desc 'Rebuild the MySQL test databases'
|
69
|
+
task :rebuild => [:drop, :build]
|
70
|
+
end
|
data/VERSION.yml
ADDED
data/lib/contentz.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
module Contentz
|
2
|
+
require 'zlib'
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def contentz(*methods)
|
10
|
+
methods.each do |name|
|
11
|
+
define_method name do
|
12
|
+
begin
|
13
|
+
data = read_attribute(name)
|
14
|
+
Zlib::Inflate.inflate(data.to_s)
|
15
|
+
rescue Zlib::BufError
|
16
|
+
''
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
define_method "#{name}=" do |value|
|
21
|
+
data = Zlib::Deflate.deflate(value.to_s, Zlib::BEST_COMPRESSION)
|
22
|
+
write_attribute(name, data)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
ActiveRecord::Base.send :include, Contentz
|
data/test/config.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ContentzTest < Test::Unit::TestCase
|
4
|
+
context 'contentz method defined' do
|
5
|
+
test 'on ActiveRecord::Base' do
|
6
|
+
assert_respond_to ActiveRecord::Base, :contentz
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
context 'read/write data:' do
|
11
|
+
setup do
|
12
|
+
@article = Article.new
|
13
|
+
end
|
14
|
+
|
15
|
+
test 'read non-binary (or unset/nil/empty) content as a blank string' do
|
16
|
+
assert_equal '', @article.body
|
17
|
+
end
|
18
|
+
|
19
|
+
test 'store content as binary data' do
|
20
|
+
@article.body = 'True or false, yes or no, the choice is yours.'
|
21
|
+
assert @article.read_attribute(:body).is_binary_data?
|
22
|
+
end
|
23
|
+
|
24
|
+
test 'store nil as a empty string' do
|
25
|
+
@article.body = ''
|
26
|
+
assert_equal '', @article.body
|
27
|
+
@article.body = nil
|
28
|
+
assert_equal '', @article.body
|
29
|
+
end
|
30
|
+
|
31
|
+
test 'write/read correctly' do
|
32
|
+
[
|
33
|
+
'Lorem ipsum',
|
34
|
+
'!@£$%^&*()œ∑†¥¨πåß∂ƒ©˙∆˚¬Ω≈ç√∫',
|
35
|
+
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. ' * 666
|
36
|
+
].each do |string|
|
37
|
+
@article.body = string
|
38
|
+
assert_equal string, @article.body
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'saving and reloading' do
|
44
|
+
test 'should not alter the content' do
|
45
|
+
string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'
|
46
|
+
id = Article.create!(:body => string).id
|
47
|
+
assert_equal string, Article.find(id).body
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context 'with memoization' do
|
52
|
+
test 'should only uncompress the same content once' do
|
53
|
+
Zlib::Inflate.expects(:inflate).once
|
54
|
+
@article = ArticleWithMemoization.new
|
55
|
+
@article.body = 'content'
|
56
|
+
@article.body
|
57
|
+
@article.body
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/test/schema.rb
ADDED
data/test/setup.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'config'
|
2
|
+
require 'logger'
|
3
|
+
|
4
|
+
ActiveRecord::Base.configurations = {
|
5
|
+
'test' => {
|
6
|
+
:adapter => 'mysql',
|
7
|
+
:database => DB_NAME,
|
8
|
+
:username => DB_USER
|
9
|
+
}
|
10
|
+
}
|
11
|
+
|
12
|
+
ActiveRecord::Base.establish_connection 'test'
|
13
|
+
|
14
|
+
require 'schema'
|
15
|
+
|
16
|
+
class Article < ActiveRecord::Base
|
17
|
+
contentz :body
|
18
|
+
end
|
19
|
+
|
20
|
+
class ArticleWithMemoization < Article
|
21
|
+
extend ActiveSupport::Memoizable
|
22
|
+
memoize :body
|
23
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'activerecord'
|
4
|
+
|
5
|
+
{
|
6
|
+
'context' => 'jeremymcanally-context -s http://gems.github.com',
|
7
|
+
'mocha' => 'mocha'
|
8
|
+
}.each do |lib, command|
|
9
|
+
begin
|
10
|
+
require lib
|
11
|
+
rescue LoadError
|
12
|
+
puts "#{lib.capitalize} not available. Install it with: sudo gem install #{command}"
|
13
|
+
exit
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
18
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
19
|
+
|
20
|
+
require 'contentz'
|
21
|
+
require 'setup' # ActiveRecord setup.
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: contentz
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ben Pickles
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-04-18 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activerecord
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description:
|
26
|
+
email: spideryoung@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- LICENSE
|
36
|
+
- README.rdoc
|
37
|
+
- Rakefile
|
38
|
+
- VERSION.yml
|
39
|
+
- lib/contentz.rb
|
40
|
+
- test/config.rb
|
41
|
+
- test/contentz_test.rb
|
42
|
+
- test/schema.rb
|
43
|
+
- test/setup.rb
|
44
|
+
- test/test_helper.rb
|
45
|
+
has_rdoc: true
|
46
|
+
homepage: http://github.com/benpickles/contentz
|
47
|
+
licenses: []
|
48
|
+
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options:
|
51
|
+
- --charset=UTF-8
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
version:
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
version:
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.3.5
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Contentz (pronounced "content zee") is an ActiveRecord extension that uses Zlib to compress/uncompress attribute data on write/read.
|
73
|
+
test_files:
|
74
|
+
- test/config.rb
|
75
|
+
- test/contentz_test.rb
|
76
|
+
- test/schema.rb
|
77
|
+
- test/setup.rb
|
78
|
+
- test/test_helper.rb
|