autoserialize 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in autoserialize.gemspec
4
+ gemspec
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
@@ -0,0 +1,13 @@
1
+ Autoserialize
2
+ =============
3
+
4
+ Introduction goes here.
5
+
6
+
7
+ Example
8
+ =======
9
+
10
+ Example goes here.
11
+
12
+
13
+ Copyright (c) 2010 [name of plugin creator], released under the MIT license
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,2 @@
1
+ # Include hook code here
2
+ require 'autoserialize'
@@ -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)
@@ -0,0 +1,3 @@
1
+ module Autoserialize
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,12 @@
1
+ require 'test_helper'
2
+
3
+ class ARModel < ActiveRecord::Base
4
+ autoserialize :configuration
5
+ end
6
+
7
+ class AutoserializeTest < ActiveSupport::TestCase
8
+ # Replace this with your real tests.
9
+ test "the truth" do
10
+ assert true
11
+ end
12
+ end
@@ -0,0 +1,6 @@
1
+ require "rubygems"
2
+ require "bundler/setup"
3
+
4
+ require 'autoserialize'
5
+
6
+ require 'active_support/test_case'
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