mongoid_snappy 0.0.4

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/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ mongoid_snappy
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-1.9.3-p392
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mongoid_snappy.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 GlebTV
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # MongoidSnappy
2
+
3
+ Allow string attributes in Mongoid to be compressed with Snappy
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'mongoid_snappy'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install mongoid_snappy
18
+
19
+ ## Usage
20
+
21
+ MongoDB doesn't have built in support for data compression.
22
+
23
+ See https://jira.mongodb.org/browse/SERVER-164
24
+
25
+ So in meantime the solution is application level compression.
26
+
27
+ Define a field of type Mongoid::Snappy
28
+
29
+ ```ruby
30
+ class MyModel
31
+ field :long_text, type: Mongoid::Snappy
32
+ end
33
+ ```
34
+
35
+ Strings are transparently compressed and decompressed:
36
+
37
+ ```ruby
38
+ m = MyModel.new
39
+ m.long_text = 'a very long string here'
40
+ m.long_text # -> 'a very long string here'
41
+ ```
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,128 @@
1
+ # coding: utf-8
2
+
3
+ require 'mongoid_snappy/version'
4
+ require 'snappy'
5
+
6
+ module Mongoid
7
+ class Snappy
8
+ instance_methods.each { |m| undef_method m unless m =~ /(^__|^send$|^object_id$)/ }
9
+
10
+ def initialize(data)
11
+ @data = data.force_encoding('UTF-8')
12
+ end
13
+
14
+ def to_s
15
+ @data
16
+ end
17
+
18
+ def inspect
19
+ '"' + @data + '"'
20
+ end
21
+
22
+ def ==(other)
23
+ if other.class == self.class
24
+ other.to_s == to_s
25
+ elsif other.class == String
26
+ @data == other
27
+ else
28
+ false
29
+ end
30
+ end
31
+
32
+ def coerce(something)
33
+ [self, something]
34
+ end
35
+
36
+ def mongoize
37
+ Moped::BSON::Binary.new(:generic, ::Snappy.deflate(@data))
38
+ end
39
+
40
+ protected
41
+
42
+ def method_missing(name, *args, &block)
43
+ @data.send(name, *args, &block)
44
+ end
45
+
46
+ class << self
47
+
48
+ # Get the object as it was stored in the database, and instantiate
49
+ # this custom class from it.
50
+ def demongoize(object)
51
+ if object.is_a?(Moped::BSON::Binary)
52
+ Mongoid::Snappy.new(::Snappy.inflate(object.data))
53
+ elsif object.is_a?(String)
54
+ Mongoid::Snappy.new(object)
55
+ else
56
+ object
57
+ end
58
+ end
59
+
60
+ # Takes any possible object and converts it to how it would be
61
+ # stored in the database.
62
+ def mongoize(object)
63
+ case
64
+ when object.is_a?(Mongoid::Snappy) then object.mongoize
65
+ when object.is_a?(String) then Mongoid::Snappy.new(object).mongoize
66
+ else object
67
+ end
68
+ end
69
+
70
+ # Converts the object that was supplied to a criteria and converts it
71
+ # into a database friendly form.
72
+ def evolve(object)
73
+ case object
74
+ when Mongoid::Snappy then object.mongoize
75
+ when String then Mongoid::Snappy.new(object).mongoize
76
+ else object
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+
83
+ if Object.const_defined?("RailsAdmin")
84
+ require 'rails_admin/adapters/mongoid'
85
+ require 'rails_admin/config/fields/types/text'
86
+ module RailsAdmin
87
+ module Adapters
88
+ module Mongoid
89
+ alias_method :type_lookup_without_mongoid_snappy, :type_lookup
90
+ def type_lookup(name, field)
91
+ if field.type.to_s == 'Mongoid::Snappy'
92
+ { :type => :mongoid_snappy }
93
+ else
94
+ type_lookup_without_mongoid_snappy(name, field)
95
+ end
96
+ end
97
+ end
98
+ end
99
+
100
+ module Config
101
+ module Fields
102
+ module Types
103
+ class MongoidSnappy < RailsAdmin::Config::Fields::Types::Text
104
+ # Register field type for the type loader
105
+ RailsAdmin::Config::Fields::Types::register(self)
106
+
107
+ register_instance_option :pretty_value do
108
+ if value.respond_to?(:data)
109
+ ::Snappy.inflate(value.data).force_encoding('UTF-8')
110
+ else
111
+ value
112
+ end
113
+ end
114
+
115
+ register_instance_option :formatted_value do
116
+ if value.respond_to?(:data)
117
+ ::Snappy.inflate(value.data).force_encoding('UTF-8')
118
+ else
119
+ value
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
127
+
128
+ end
@@ -0,0 +1,3 @@
1
+ module MongoidSnappy
2
+ VERSION = "0.0.4"
3
+ end
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mongoid_snappy/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mongoid_snappy"
8
+ spec.version = MongoidSnappy::VERSION
9
+ spec.authors = ["GlebTV"]
10
+ spec.email = ["glebtv@gmail.com"]
11
+ spec.description = %q{Mongoid Snappy}
12
+ spec.summary = %q{Allow string attributes in Mongoid to be compressed with Snappy}
13
+ spec.homepage = "https://github.com/rs-pro/mongoid_snappy"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency 'snappy', '~> 0.0.8'
22
+ spec.add_runtime_dependency 'mongoid', '>= 3.0'
23
+
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.3"
26
+ spec.add_development_dependency "rake"
27
+
28
+ spec.add_development_dependency "rspec", "~> 2.13.0"
29
+ spec.add_development_dependency "simplecov", "~> 0.7.1"
30
+ spec.add_development_dependency "database_cleaner", "~> 0.9.1"
31
+ spec.add_development_dependency "mongoid-rspec", "~> 1.7.0"
32
+ spec.add_development_dependency "faker", "~> 1.1.2"
33
+ end
@@ -0,0 +1,48 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe MongoidSnappy do
6
+ it 'correctly stores data' do
7
+ 100.times do
8
+ text = Faker::Lorem.paragraphs(5).join(" ")
9
+ c = Dummy.new(long_text: text)
10
+ c.long_text.should eq text
11
+ c.save.should be_true
12
+ c.long_text.should eq text
13
+ c.read_attribute(:long_text).data.length.should be < text.length
14
+ Dummy.find(c.id).long_text.should eq text
15
+ end
16
+ end
17
+
18
+ it 'correctly migrates data from string field' do
19
+ text = Faker::Lorem.paragraphs(3).join(" ")
20
+ c = Dummy.new(migrate_test: text)
21
+ c.save.should be_true
22
+ Migrated.first.migrate_test.should eq text
23
+ end
24
+
25
+ it 'correctly looks up string in DB' do
26
+ text = Faker::Lorem.words(5).join(" ")
27
+ c = Dummy.new(long_text: text)
28
+ c.long_text.should eq text
29
+ c.save.should be_true
30
+ c.long_text.should eq text
31
+
32
+ f = Dummy.where(long_text: text).first
33
+ f.should_not be_nil
34
+ f.long_text.should eq text
35
+ end
36
+
37
+ it 'correctly looks up string in DB' do
38
+ text = Mongoid::Snappy.new(Faker::Lorem.words(5).join(" "))
39
+ c = Dummy.new(long_text: text)
40
+ c.long_text.should eq text
41
+ c.save.should be_true
42
+ c.long_text.should eq text
43
+
44
+ f = Dummy.where(long_text: text).first
45
+ f.should_not be_nil
46
+ f.long_text.should eq text
47
+ end
48
+ end
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+
3
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
+
5
+ require 'rubygems'
6
+
7
+ require 'simplecov'
8
+ SimpleCov.start
9
+
10
+ require 'bundler/setup'
11
+ require 'mongoid'
12
+ require 'database_cleaner'
13
+ require 'mongoid-rspec'
14
+ require 'faker'
15
+ require 'mongoid_snappy'
16
+
17
+ # Requires supporting files with custom matchers and macros, etc,
18
+ # in ./support/ and its subdirectories.
19
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
20
+
21
+ Mongoid.configure do |config|
22
+ ENV["MONGOID_ENV"] = "test"
23
+ Mongoid.load!("spec/support/mongoid.yml")
24
+ end
25
+
26
+ RSpec.configure do |config|
27
+ config.before :suite do
28
+ DatabaseCleaner.strategy = :truncation
29
+ end
30
+ config.after :each do
31
+ DatabaseCleaner.clean
32
+ end
33
+ config.include Mongoid::Matchers
34
+ config.mock_with :rspec
35
+ end
@@ -0,0 +1,12 @@
1
+ class Dummy
2
+ include Mongoid::Document
3
+
4
+ field :migrate_test, type: String
5
+ field :long_text, type: Mongoid::Snappy
6
+ end
7
+
8
+ class Migrated
9
+ include Mongoid::Document
10
+ store_in collection: 'dummies'
11
+ field :migrate_test, type: Mongoid::Snappy
12
+ end
@@ -0,0 +1,8 @@
1
+ test:
2
+ sessions:
3
+ default:
4
+ database: mongoid_snappy
5
+ hosts:
6
+ - localhost:27017
7
+ options:
8
+ consistency: :strong
metadata ADDED
@@ -0,0 +1,215 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_snappy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - GlebTV
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: snappy
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.0.8
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 0.0.8
30
+ - !ruby/object:Gem::Dependency
31
+ name: mongoid
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '3.0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '3.0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '1.3'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rspec
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 2.13.0
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 2.13.0
94
+ - !ruby/object:Gem::Dependency
95
+ name: simplecov
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: 0.7.1
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 0.7.1
110
+ - !ruby/object:Gem::Dependency
111
+ name: database_cleaner
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: 0.9.1
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ~>
124
+ - !ruby/object:Gem::Version
125
+ version: 0.9.1
126
+ - !ruby/object:Gem::Dependency
127
+ name: mongoid-rspec
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ~>
132
+ - !ruby/object:Gem::Version
133
+ version: 1.7.0
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ~>
140
+ - !ruby/object:Gem::Version
141
+ version: 1.7.0
142
+ - !ruby/object:Gem::Dependency
143
+ name: faker
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ~>
148
+ - !ruby/object:Gem::Version
149
+ version: 1.1.2
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ~>
156
+ - !ruby/object:Gem::Version
157
+ version: 1.1.2
158
+ description: Mongoid Snappy
159
+ email:
160
+ - glebtv@gmail.com
161
+ executables: []
162
+ extensions: []
163
+ extra_rdoc_files: []
164
+ files:
165
+ - .gitignore
166
+ - .rspec
167
+ - .ruby-gemset
168
+ - .ruby-version
169
+ - Gemfile
170
+ - LICENSE.txt
171
+ - README.md
172
+ - Rakefile
173
+ - lib/mongoid_snappy.rb
174
+ - lib/mongoid_snappy/version.rb
175
+ - mongoid_snappy.gemspec
176
+ - spec/mongoid_snappy_spec.rb
177
+ - spec/spec_helper.rb
178
+ - spec/support/models.rb
179
+ - spec/support/mongoid.yml
180
+ homepage: https://github.com/rs-pro/mongoid_snappy
181
+ licenses:
182
+ - MIT
183
+ post_install_message:
184
+ rdoc_options: []
185
+ require_paths:
186
+ - lib
187
+ required_ruby_version: !ruby/object:Gem::Requirement
188
+ none: false
189
+ requirements:
190
+ - - ! '>='
191
+ - !ruby/object:Gem::Version
192
+ version: '0'
193
+ segments:
194
+ - 0
195
+ hash: 1394254402025025875
196
+ required_rubygems_version: !ruby/object:Gem::Requirement
197
+ none: false
198
+ requirements:
199
+ - - ! '>='
200
+ - !ruby/object:Gem::Version
201
+ version: '0'
202
+ segments:
203
+ - 0
204
+ hash: 1394254402025025875
205
+ requirements: []
206
+ rubyforge_project:
207
+ rubygems_version: 1.8.25
208
+ signing_key:
209
+ specification_version: 3
210
+ summary: Allow string attributes in Mongoid to be compressed with Snappy
211
+ test_files:
212
+ - spec/mongoid_snappy_spec.rb
213
+ - spec/spec_helper.rb
214
+ - spec/support/models.rb
215
+ - spec/support/mongoid.yml