autoserialize 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/MIT-LICENSE +20 -0
- data/README +13 -0
- data/Rakefile +17 -0
- data/autoserialize.gemspec +21 -0
- data/init.rb +2 -0
- data/lib/autoserialize.rb +64 -0
- data/lib/autoserialize/version.rb +3 -0
- data/test/autoserialize_test.rb +12 -0
- data/test/test_helper.rb +6 -0
- metadata +74 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 [name of plugin creator]
|
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
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/testtask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the autoserialize plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.libs << 'test'
|
12
|
+
t.pattern = 'test/**/*_test.rb'
|
13
|
+
t.verbose = true
|
14
|
+
end
|
15
|
+
|
16
|
+
include Rake::DSL
|
17
|
+
Bundler::GemHelper.install_tasks
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "autoserialize/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "autoserialize"
|
7
|
+
s.version = Autoserialize::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Trae Robrock"]
|
10
|
+
s.email = ["trobrock@gmail.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{Autoserialize AR attributes, dont use this. It's old}
|
13
|
+
s.description = %q{Autoserialize AR attributes, dont use this. It's old}
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_dependency "activerecord", "~> 3.1.10"
|
21
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
# Autoserialize
|
4
|
+
module AutoSerialize #:nodoc:
|
5
|
+
def self.included(klass)
|
6
|
+
klass.extend ClassMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def autoserialize(attribute, format=:json, db_column=nil, default_value={})
|
11
|
+
format = validate_serialization_format!(format)
|
12
|
+
db_column ||= :"#{attribute}_#{format}"
|
13
|
+
class_name = self.name
|
14
|
+
|
15
|
+
options_str = ''
|
16
|
+
if default_value.respond_to?('with_indifferent_access')
|
17
|
+
options_str += '.with_indifferent_access'
|
18
|
+
end
|
19
|
+
default_value = default_value.inspect
|
20
|
+
|
21
|
+
define_methods_str = <<END_DEFINE_METHODS_STRING
|
22
|
+
def save_#{attribute}
|
23
|
+
self.#{db_column} = #{class_name}.to_#{format}(@#{attribute}) if @#{attribute}
|
24
|
+
end
|
25
|
+
before_validation :save_#{attribute}
|
26
|
+
|
27
|
+
def #{attribute}
|
28
|
+
return @#{attribute} if @#{attribute}
|
29
|
+
@#{attribute} = ( self.#{db_column} ? #{class_name}.from_#{format}(self.#{db_column}) || #{default_value} : #{default_value} )#{options_str}
|
30
|
+
end
|
31
|
+
|
32
|
+
def #{attribute}=(attr)
|
33
|
+
self.#{db_column} = #{class_name}.to_#{format}(attr)
|
34
|
+
end
|
35
|
+
|
36
|
+
def #{db_column}=(attr_#{format})
|
37
|
+
self[:#{db_column}] = attr_#{format}
|
38
|
+
@#{attribute} = nil # Clear the cached #{attribute}
|
39
|
+
end
|
40
|
+
END_DEFINE_METHODS_STRING
|
41
|
+
self.send(:class_eval, define_methods_str)
|
42
|
+
end
|
43
|
+
|
44
|
+
def to_json(object)
|
45
|
+
object.to_json
|
46
|
+
end
|
47
|
+
def from_json(object_str)
|
48
|
+
return nil if object_str == "null"
|
49
|
+
ActiveSupport::JSON.decode(object_str)
|
50
|
+
end
|
51
|
+
|
52
|
+
protected
|
53
|
+
VALID_FORMATS = [:json]
|
54
|
+
def validate_serialization_format!(format)
|
55
|
+
format = format.to_sym
|
56
|
+
if !VALID_FORMATS.include?(format)
|
57
|
+
raise "Unhandled serialization format #{format.inspect}"
|
58
|
+
end
|
59
|
+
format
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
ActiveRecord::Base.send(:include, AutoSerialize)
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: autoserialize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Trae Robrock
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-03 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.10
|
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.10
|
30
|
+
description: Autoserialize AR attributes, dont use this. It's old
|
31
|
+
email:
|
32
|
+
- trobrock@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- MIT-LICENSE
|
40
|
+
- README
|
41
|
+
- Rakefile
|
42
|
+
- autoserialize.gemspec
|
43
|
+
- init.rb
|
44
|
+
- lib/autoserialize.rb
|
45
|
+
- lib/autoserialize/version.rb
|
46
|
+
- test/autoserialize_test.rb
|
47
|
+
- test/test_helper.rb
|
48
|
+
homepage: ''
|
49
|
+
licenses: []
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
requirements: []
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.8.24
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Autoserialize AR attributes, dont use this. It's old
|
72
|
+
test_files:
|
73
|
+
- test/autoserialize_test.rb
|
74
|
+
- test/test_helper.rb
|