ar_marshal_store 1.0.0

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.
@@ -0,0 +1,98 @@
1
+ = ActiveRecord Marshal Store
2
+
3
+ Rails/ActiveRecord 3 comes with a cool ActiveRecord::Store feature
4
+ that allows for creating a fairly lightweight schema-less
5
+ model. However, it uses YAML, which has poor performance and is just
6
+ generally silly.
7
+
8
+ This gem enhances ActiveRecord to store such things using the Marshal
9
+ module which is pretty darn fast, compact and seemingly less flakey
10
+ than Psych.
11
+
12
+ == Usage
13
+
14
+ You can call marshal_store similary to how you would previously have
15
+ called store.
16
+
17
+ class FooModel < ActiveRecord::Base
18
+ # The old, YAML-based version would look like:
19
+ # store :extras, :accessors => :foo, :bar
20
+
21
+ # The new, marshal-based version would look like:
22
+ marshal_store :extras, :accessors => :foo, :bar
23
+ end
24
+
25
+ In addition, you can also use the exposed marshal coder as a column
26
+ coder, much like YamlCoder in serialize:
27
+
28
+ class FooModel < ActiveRecord::Base
29
+ # Used to serialize field as a hash via YAML
30
+ # serialize :field, Hash
31
+
32
+ # Now use marshal!
33
+ serialize :field, ArMarshalStore::NilFriendlyMarshal
34
+ end
35
+
36
+ === Ruby
37
+
38
+ This gem has been tested with:
39
+
40
+ * Ruby 1.8.7
41
+ * Ruby 1.9.3
42
+
43
+ If this works for you, great! If you use a different ruby, please let
44
+ me know, and I will update this.
45
+
46
+ === Rails
47
+
48
+ Add ar_marshal_store to your Gemfile.
49
+
50
+ gem 'ar_marshal_store', :git => 'git://github.com/hfwang/ar_marshal_store.git'
51
+
52
+ The necessary code is included into ActiveRecord::Base automatically.
53
+
54
+ === Outside of Rails
55
+ require 'ar_marshal_store'
56
+ class MyModel < ActiveRecord::Base
57
+ include ArMarshalStore
58
+
59
+ # ... usage of ArMarshalStore here.
60
+
61
+ end
62
+
63
+ == Note on Patches/Pull Requests
64
+
65
+ * Fork the project.
66
+ * Make your feature addition or bug fix.
67
+ * Add tests for it. This is important so I don't break it in a
68
+ future version unintentionally.
69
+ * Commit, do not mess with rakefile, version, or history.
70
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
71
+ * Send me a pull request. Bonus points for topic branches.
72
+
73
+ == Copyright / License
74
+
75
+ (2-clause BSD)
76
+
77
+ Copyright (c) 2012, Hsiu-Fan Wang
78
+
79
+ All rights reserved.
80
+
81
+ Redistribution and use in source and binary forms, with or without
82
+ modification, are permitted provided that the following conditions are
83
+ met:
84
+
85
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
86
+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
87
+
88
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
89
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
90
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
91
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
92
+ HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
93
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
94
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
95
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
96
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
97
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
98
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,19 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "ar_marshal_store"
8
+ gem.summary = %Q{Use marshal to serialize things, similar to ActiveRecord::Store}
9
+ gem.description = %Q{Use marshal to serialize things, similar to ActiveRecord::Store}
10
+ gem.email = "hfwang@porkbuns.net"
11
+ gem.homepage = "http://github.com/hfwang/ar_marshal_store"
12
+ gem.authors = ["Hsiu-Fan Wang"]
13
+ gem.add_dependency('activerecord', '>= 3.1.0')
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,43 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "ar_marshal_store"
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Hsiu-Fan Wang"]
12
+ s.date = "2013-04-30"
13
+ s.description = "Use marshal to serialize things, similar to ActiveRecord::Store"
14
+ s.email = "hfwang@porkbuns.net"
15
+ s.extra_rdoc_files = [
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ "README.rdoc",
20
+ "Rakefile",
21
+ "VERSION",
22
+ "ar_marshal_store.gemspec",
23
+ "lib/ar_marshal_store.rb",
24
+ "lib/ar_marshal_store/railtie.rb"
25
+ ]
26
+ s.homepage = "http://github.com/hfwang/ar_marshal_store"
27
+ s.require_paths = ["lib"]
28
+ s.rubygems_version = "1.8.24"
29
+ s.summary = "Use marshal to serialize things, similar to ActiveRecord::Store"
30
+
31
+ if s.respond_to? :specification_version then
32
+ s.specification_version = 3
33
+
34
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
35
+ s.add_runtime_dependency(%q<activerecord>, [">= 3.1.0"])
36
+ else
37
+ s.add_dependency(%q<activerecord>, [">= 3.1.0"])
38
+ end
39
+ else
40
+ s.add_dependency(%q<activerecord>, [">= 3.1.0"])
41
+ end
42
+ end
43
+
@@ -0,0 +1,46 @@
1
+ module ArMarshalStore
2
+ class NilFriendlyMarshal
3
+ def initialize(opts = nil)
4
+ @opts = opts
5
+ @opts ||= { :default => Hash.new() }
6
+ end
7
+
8
+ def load(str)
9
+ if str.nil?
10
+ return default_value
11
+ else
12
+ Marshal.load(str) || default_value
13
+ end
14
+ end
15
+
16
+ def dump(str)
17
+ Marshal.dump(str)
18
+ end
19
+
20
+ def self.load(str)
21
+ ArMarshalStore::NilFriendlyMarshal::DEFAULT.load(str)
22
+ end
23
+
24
+ def self.dump(str)
25
+ ArMarshalStore::NilFriendlyMarshal::DEFAULT.dump(str)
26
+ end
27
+
28
+ private
29
+ def default_value
30
+ @opts[:default].duplicable? ? @opts[:default].dup : @opts[:default]
31
+ end
32
+ end
33
+
34
+ extend ActiveSupport::Concern
35
+
36
+ module ClassMethods
37
+ def marshal_store(store_attribute, options={})
38
+ serialize(store_attribute, NilFriendlyMarshal)
39
+ store_accessor(store_attribute, options[:accessors]) if options.has_key? :accessors
40
+ end
41
+ end
42
+ end
43
+
44
+ ArMarshalStore::NilFriendlyMarshal::DEFAULT = ArMarshalStore::NilFriendlyMarshal.new
45
+
46
+ require 'ar_marshal_store/railtie.rb' if defined?(Rails)
@@ -0,0 +1,12 @@
1
+ require 'ar_marshal_store'
2
+ require 'rails'
3
+
4
+ module ArMarshalStore
5
+ class Railtie < ::Rails::Railtie
6
+ initializer "ar_marshal_store.active_record" do |app|
7
+ ActiveSupport.on_load :active_record do
8
+ ActiveRecord::Base.send :include, ArMarshalStore
9
+ end
10
+ end
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ar_marshal_store
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Hsiu-Fan Wang
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.1.0
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: 3.1.0
30
+ description: Use marshal to serialize things, similar to ActiveRecord::Store
31
+ email: hfwang@porkbuns.net
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files:
35
+ - README.rdoc
36
+ files:
37
+ - README.rdoc
38
+ - Rakefile
39
+ - VERSION
40
+ - ar_marshal_store.gemspec
41
+ - lib/ar_marshal_store.rb
42
+ - lib/ar_marshal_store/railtie.rb
43
+ homepage: http://github.com/hfwang/ar_marshal_store
44
+ licenses: []
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 1.8.24
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: Use marshal to serialize things, similar to ActiveRecord::Store
67
+ test_files: []