serialize_variants 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Goyman.com
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,23 @@
1
+ SerializeVariants
2
+ =================
3
+
4
+ Provides a simple mechanism to specify how an object should be serialized at the model level.
5
+
6
+ At present only the :short and :long variants are supported, any variant will be supported in the future.
7
+
8
+ Example
9
+ =======
10
+
11
+ In your model file:
12
+
13
+ short_serialize_options :only => [:id, :name]
14
+
15
+ Anywhere, but typically in your controller file:
16
+
17
+ render :json => my_object.to_json(:variant => :short)
18
+
19
+
20
+
21
+
22
+
23
+ Copyright (c) 2011 Nicolas Goy, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+ require 'rake/gempackagetask'
5
+
6
+ desc 'Default: run unit tests.'
7
+ task :default => :test
8
+
9
+ desc 'Test the serialize_variants plugin.'
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.libs << 'lib'
12
+ t.libs << 'test'
13
+ t.pattern = 'test/**/*_test.rb'
14
+ t.verbose = true
15
+ end
16
+
17
+ desc 'Generate documentation for the serialize_variants plugin.'
18
+ Rake::RDocTask.new(:rdoc) do |rdoc|
19
+ rdoc.rdoc_dir = 'rdoc'
20
+ rdoc.title = 'SerializeVariants'
21
+ rdoc.options << '--line-numbers' << '--inline-source'
22
+ rdoc.rdoc_files.include('README')
23
+ rdoc.rdoc_files.include('lib/**/*.rb')
24
+ end
25
+
26
+ PKG_FILES = FileList[
27
+ '[a-zA-Z]*',
28
+ 'generators/**/*',
29
+ 'lib/**/*',
30
+ 'rails/**/*',
31
+ 'tasks/**/*',
32
+ 'test/**/*'
33
+ ]
34
+
35
+ spec = Gem::Specification.new do |s|
36
+ s.name = "serialize_variants"
37
+ s.version = "0.0.1"
38
+ s.author = "Nicolas Goy"
39
+ s.email = "kuon@goyman.com"
40
+ s.homepage = "http://goyman.com/"
41
+ s.platform = Gem::Platform::RUBY
42
+ s.summary = "Provides serialization options as variants"
43
+ s.description = "Serialize Variants provides a way to specify serialize options at the model level and reuse those options"
44
+ s.files = PKG_FILES.to_a
45
+ s.require_path = "lib"
46
+ s.extra_rdoc_files = ["README"]
47
+ end
48
+
49
+ desc 'Turn this plugin into a gem.'
50
+ Rake::GemPackageTask.new(spec) do |pkg|
51
+ pkg.gem_spec = spec
52
+ end
53
+
data/init.rb ADDED
@@ -0,0 +1,4 @@
1
+ # Include hook code here
2
+ require 'serialize_variants'
3
+
4
+ ActiveRecord::Base.send(:include, SerializeVariants::Variants)
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,36 @@
1
+ # SerializeVariants
2
+ #
3
+
4
+ module SerializeVariants
5
+ module Variants
6
+ include ActiveModel::Serialization
7
+ module ClassMethods
8
+ def short_serialize_options(options=nil)
9
+ @short_serialize_options = options if options
10
+ @short_serialize_options
11
+ end
12
+ def long_serialize_options(options=nil)
13
+ @long_serialize_options = options if options
14
+ @long_serialize_options
15
+ end
16
+ end
17
+
18
+ def self.included(base)
19
+ base.extend(ClassMethods)
20
+ end
21
+
22
+ def serializable_hash(options = nil)
23
+ options ||= {}
24
+ variant = options[:variant]
25
+
26
+ case options[:variant]
27
+ when :short
28
+ super(self.class.short_serialize_options)
29
+ when :long
30
+ super(self.class.long_serialize_options)
31
+ else
32
+ super(options)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class SerializeVariantsTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_support'
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: serialize_variants
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Nicolas Goy
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-03 00:00:00 Z
14
+ dependencies: []
15
+
16
+ description: Serialize Variants provides a way to specify serialize options at the model level and reuse those options
17
+ email: kuon@goyman.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - init.rb
26
+ - install.rb
27
+ - MIT-LICENSE
28
+ - Rakefile
29
+ - README
30
+ - uninstall.rb
31
+ - lib/serialize_variants.rb
32
+ - test/serialize_variants_test.rb
33
+ - test/test_helper.rb
34
+ homepage: http://goyman.com/
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.7.2
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: Provides serialization options as variants
61
+ test_files: []
62
+