laserlemon-hot_serial 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +18 -0
- data/Rakefile +37 -0
- data/VERSION +1 -0
- data/hot_serial.gemspec +57 -0
- data/init.rb +1 -0
- data/lib/hot_serial.rb +31 -0
- data/lib/serializer.rb +52 -0
- data/test/hot_serial_test.rb +7 -0
- data/test/test_helper.rb +10 -0
- metadata +85 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Steve Richert
|
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,18 @@
|
|
1
|
+
= hot_serial
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but
|
13
|
+
bump version in a commit by itself I can ignore when I pull)
|
14
|
+
* Send me a pull request. Bonus points for topic branches.
|
15
|
+
|
16
|
+
== Copyright
|
17
|
+
|
18
|
+
Copyright (c) 2009 Steve Richert. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/testtask'
|
4
|
+
require 'rake/rdoctask'
|
5
|
+
|
6
|
+
begin
|
7
|
+
require 'jeweler'
|
8
|
+
Jeweler::Tasks.new do |g|
|
9
|
+
g.name = 'hot_serial'
|
10
|
+
g.summary = %(Define serialization styles for your ActiveRecord models)
|
11
|
+
g.description = %(Define serialization styles for your ActiveRecord models)
|
12
|
+
g.email = 'steve@laserlemon.com'
|
13
|
+
g.homepage = 'http://github.com/laserlemon/hot_serial'
|
14
|
+
g.authors = %w(laserlemon)
|
15
|
+
g.add_development_dependency 'activerecord'
|
16
|
+
g.add_development_dependency 'thoughtbot-shoulda'
|
17
|
+
end
|
18
|
+
rescue LoadError
|
19
|
+
puts 'Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler'
|
20
|
+
end
|
21
|
+
|
22
|
+
Rake::TestTask.new(:test) do |t|
|
23
|
+
t.libs = %w(test)
|
24
|
+
t.pattern = 'test/**/*_test.rb'
|
25
|
+
end
|
26
|
+
|
27
|
+
task :test => :check_dependencies
|
28
|
+
|
29
|
+
task :default => :test
|
30
|
+
|
31
|
+
Rake::RDocTask.new do |r|
|
32
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : nil
|
33
|
+
r.rdoc_dir = 'rdoc'
|
34
|
+
r.title = ['hot_serial', version].compact.join(' ')
|
35
|
+
r.rdoc_files.include('README*')
|
36
|
+
r.rdoc_files.include('lib/**/*.rb')
|
37
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/hot_serial.gemspec
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
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 = %q{hot_serial}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["laserlemon"]
|
12
|
+
s.date = %q{2009-08-21}
|
13
|
+
s.description = %q{Define serialization styles for your ActiveRecord models}
|
14
|
+
s.email = %q{steve@laserlemon.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".gitignore",
|
21
|
+
"LICENSE",
|
22
|
+
"README.rdoc",
|
23
|
+
"Rakefile",
|
24
|
+
"VERSION",
|
25
|
+
"hot_serial.gemspec",
|
26
|
+
"init.rb",
|
27
|
+
"lib/hot_serial.rb",
|
28
|
+
"lib/serializer.rb",
|
29
|
+
"test/hot_serial_test.rb",
|
30
|
+
"test/test_helper.rb"
|
31
|
+
]
|
32
|
+
s.homepage = %q{http://github.com/laserlemon/hot_serial}
|
33
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
34
|
+
s.require_paths = ["lib"]
|
35
|
+
s.rubygems_version = %q{1.3.5}
|
36
|
+
s.summary = %q{Define serialization styles for your ActiveRecord models}
|
37
|
+
s.test_files = [
|
38
|
+
"test/hot_serial_test.rb",
|
39
|
+
"test/test_helper.rb"
|
40
|
+
]
|
41
|
+
|
42
|
+
if s.respond_to? :specification_version then
|
43
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
44
|
+
s.specification_version = 3
|
45
|
+
|
46
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
47
|
+
s.add_development_dependency(%q<activerecord>, [">= 0"])
|
48
|
+
s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
49
|
+
else
|
50
|
+
s.add_dependency(%q<activerecord>, [">= 0"])
|
51
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
52
|
+
end
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<activerecord>, [">= 0"])
|
55
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
56
|
+
end
|
57
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'hot_serial'
|
data/lib/hot_serial.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'serializer'
|
2
|
+
|
3
|
+
module LaserLemon
|
4
|
+
module HotSerial
|
5
|
+
def self.included(base)
|
6
|
+
base.class_eval do
|
7
|
+
class_inheritable_hash :serialization_options
|
8
|
+
self.serialization_options = {}
|
9
|
+
|
10
|
+
class_inheritable_accessor :default_serialization_style
|
11
|
+
self.default_serialization_style = nil
|
12
|
+
end
|
13
|
+
base.extend ClassMethods
|
14
|
+
end
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
def hot_serial(*args)
|
18
|
+
options = args.extract_options!
|
19
|
+
style = args.first || :default
|
20
|
+
self.serialization_options[style] = options
|
21
|
+
self.default_serialization_style ||= style
|
22
|
+
end
|
23
|
+
|
24
|
+
def default_serialization_options
|
25
|
+
self.serialization_options.fetch(self.default_serialization_style, {})
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
ActiveRecord::Base.send(:include, LaserLemon::HotSerial)
|
data/lib/serializer.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
module LaserLemon
|
2
|
+
module HotSerial
|
3
|
+
module Serializer
|
4
|
+
def self.included(base)
|
5
|
+
base.alias_method_chain :initialize, :breakfast
|
6
|
+
base.alias_method_chain :add_includes, :breakfast
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize_with_breakfast(record, options = nil)
|
10
|
+
default_options = record.class.default_serialization_options
|
11
|
+
|
12
|
+
new_options = case options
|
13
|
+
when Hash
|
14
|
+
options.reverse_merge!(:style => record.class.default_serialization_style)
|
15
|
+
style_options = record.class.serialization_options.fetch(options[:style], default_options)
|
16
|
+
options.reverse_merge(style_options)
|
17
|
+
else
|
18
|
+
record.class.serialization_options.fetch(options, default_options).merge(:style => options)
|
19
|
+
end
|
20
|
+
|
21
|
+
initialize_without_breakfast(record, new_options)
|
22
|
+
end
|
23
|
+
|
24
|
+
def add_includes_with_breakfast(&block)
|
25
|
+
if include_associations = options.delete(:include)
|
26
|
+
include_has_options = include_associations.is_a?(Hash)
|
27
|
+
associations = include_has_options ? include_associations.keys : Array(include_associations)
|
28
|
+
|
29
|
+
for association in associations
|
30
|
+
records = case @record.class.reflect_on_association(association).macro
|
31
|
+
when :has_many, :has_and_belongs_to_many
|
32
|
+
@record.send(association).to_a
|
33
|
+
when :has_one, :belongs_to
|
34
|
+
@record.send(association)
|
35
|
+
end
|
36
|
+
|
37
|
+
unless records.nil?
|
38
|
+
association_options = include_has_options ? include_associations[association] : {}
|
39
|
+
association_options.reverse_merge!(:style => options[:style], :include => nil)
|
40
|
+
opts = options.except(:only, :except, :methods).merge(association_options)
|
41
|
+
yield(association, records, opts)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
options[:include] = include_associations
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
ActiveRecord::Serialization::Serializer.send(:include, LaserLemon::HotSerial::Serializer)
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: laserlemon-hot_serial
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- laserlemon
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-08-21 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activerecord
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: thoughtbot-shoulda
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
description: Define serialization styles for your ActiveRecord models
|
36
|
+
email: steve@laserlemon.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.rdoc
|
44
|
+
files:
|
45
|
+
- .gitignore
|
46
|
+
- LICENSE
|
47
|
+
- README.rdoc
|
48
|
+
- Rakefile
|
49
|
+
- VERSION
|
50
|
+
- hot_serial.gemspec
|
51
|
+
- init.rb
|
52
|
+
- lib/hot_serial.rb
|
53
|
+
- lib/serializer.rb
|
54
|
+
- test/hot_serial_test.rb
|
55
|
+
- test/test_helper.rb
|
56
|
+
has_rdoc: false
|
57
|
+
homepage: http://github.com/laserlemon/hot_serial
|
58
|
+
licenses:
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options:
|
61
|
+
- --charset=UTF-8
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "0"
|
69
|
+
version:
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: "0"
|
75
|
+
version:
|
76
|
+
requirements: []
|
77
|
+
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 1.3.5
|
80
|
+
signing_key:
|
81
|
+
specification_version: 3
|
82
|
+
summary: Define serialization styles for your ActiveRecord models
|
83
|
+
test_files:
|
84
|
+
- test/hot_serial_test.rb
|
85
|
+
- test/test_helper.rb
|