cereal_box 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ *.gem
2
+ .bundle
3
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.2@cereal_box --create
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ script: "bundle exec rake spec"
5
+ gemfile:
6
+ - Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in cereal_box.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cereal_box (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.1.3)
10
+ guard (0.8.4)
11
+ thor (~> 0.14.6)
12
+ guard-rspec (0.5.0)
13
+ guard (>= 0.8.4)
14
+ rake (0.9.2)
15
+ rspec (2.6.0)
16
+ rspec-core (~> 2.6.0)
17
+ rspec-expectations (~> 2.6.0)
18
+ rspec-mocks (~> 2.6.0)
19
+ rspec-core (2.6.4)
20
+ rspec-expectations (2.6.0)
21
+ diff-lcs (~> 1.1.2)
22
+ rspec-mocks (2.6.0)
23
+ thor (0.14.6)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler
30
+ cereal_box!
31
+ guard
32
+ guard-rspec
33
+ rake
34
+ rspec
data/Guardfile ADDED
@@ -0,0 +1,21 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :version => 2 do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+
9
+ # Rails example
10
+ watch(%r{^spec/.+_spec\.rb$})
11
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
12
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
13
+ watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
14
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
15
+ watch('spec/spec_helper.rb') { "spec" }
16
+ watch('config/routes.rb') { "spec/routing" }
17
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
18
+ # Capybara request specs
19
+ watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }
20
+ end
21
+
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2011 Christopher Meiklejohn
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,60 @@
1
+ = Cereal box.
2
+
3
+ Serialization filters for active record.
4
+
5
+ == Motivation
6
+
7
+ Ever have a controller that looks like this?
8
+
9
+ class ThingController < ActionController::Base
10
+ respond_to :json
11
+
12
+ def show
13
+ @thing = Thing.find(1)
14
+ @thing = @thing.as_json
15
+ @thing[:other_things][:name] = thing.other_thing.name
16
+ @thing[:other_related_thing][:name] = thing.other_related_thing.name
17
+
18
+ respond_with(@thing)
19
+ end
20
+ end
21
+
22
+ Specifically adding additional related information from other models
23
+ into the serialized hash of an object? Well, no more!
24
+
25
+ Doesn't this look better?
26
+
27
+ class ThingController < ActionController::Base
28
+ respond_to :json
29
+
30
+ def show
31
+ @thing = Thing.find(1)
32
+
33
+ respond_with(OtherRelatedThingFilter.new(ThingFilter.new(@thing))
34
+ end
35
+ end
36
+
37
+ == Filters
38
+
39
+ === Implement a filter
40
+
41
+ It's simple! Just define a module that includes cereal_box and
42
+ implements an attributes method.
43
+
44
+ module OtherThingFilter
45
+ include CerealBox
46
+
47
+ def attributes(base)
48
+ { :name => base.other_thing.name }
49
+ end
50
+ end
51
+
52
+ Filters support as_xml, as_json and serializable_hash.
53
+
54
+ == License
55
+
56
+ Cereal Box is Copyright © 2011 Christopher Meiklejohn. It is free software, and may be redistributed under the terms specified in the LICENSE file.
57
+
58
+ == About
59
+
60
+ The cereal_box gem was written by {Christopher Meiklejohn}[mailto:cmeiklejohn@swipely.com] from {Swipely, Inc.}[http://www.swipely.com].
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler/setup'
5
+ require 'bundler/gem_tasks'
6
+ require 'rake'
7
+ require 'rdoc/task'
8
+ require 'rubygems/package_task'
9
+ require 'rspec/core/rake_task'
10
+
11
+ task :default => :spec
12
+
13
+ RSpec::Core::RakeTask.new do |t|
14
+ t.pattern = 'spec/**/*_spec.rb'
15
+ end
16
+
17
+ RDoc::Task.new do |rdoc|
18
+ rdoc.main = "README.rdoc"
19
+ rdoc.rdoc_files.include("README.rdoc", "lib/**/*.rb")
20
+ rdoc.options << '-f' << 'horo'
21
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: UTF-8
2
+
3
+ $:.push File.expand_path("../lib", __FILE__)
4
+ require "cereal_box/version"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "cereal_box"
8
+ s.version = CerealBox::VERSION
9
+ s.authors = ["Christopher Meiklejohn"]
10
+ s.email = ["christopher.meiklejohn@gmail.com"]
11
+ s.homepage = "https://github.com/cmeiklejohn/cereal_box"
12
+ s.summary = %q{Serialization filters for active record.}
13
+ s.description = %q{Serialization filters for active record.}
14
+
15
+ s.rubyforge_project = "cereal_box"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_development_dependency('bundler')
23
+ s.add_development_dependency('rspec')
24
+ s.add_development_dependency('guard')
25
+ s.add_development_dependency('guard-rspec')
26
+ s.add_development_dependency('rake')
27
+ end
@@ -0,0 +1,3 @@
1
+ module CerealBox
2
+ VERSION = "0.0.1"
3
+ end
data/lib/cereal_box.rb ADDED
@@ -0,0 +1,37 @@
1
+ require "cereal_box/version"
2
+
3
+ module CerealBox
4
+
5
+ def self.included?(base)
6
+ base.attr_accessor :cereal_box_base_instance
7
+ base.attr_accessor :cereal_box_previous_filter
8
+ end
9
+
10
+ def initialize(base)
11
+ @cereal_box_base_instance = base
12
+ end
13
+
14
+ def cereal_box_node_name
15
+ self.class.to_s.split(/(?=[A-Z])/).map{ |w| w.downcase }[0...-1].join("_").to_sym
16
+ end
17
+
18
+ def serializable_hash(options = {})
19
+ apply(:serializable_hash, options)
20
+ end
21
+
22
+ def as_json(options = {})
23
+ apply(:as_json, options)
24
+ end
25
+
26
+ def as_xml(options = {})
27
+ apply(:as_xml, options)
28
+ end
29
+
30
+ def apply(message, options = {})
31
+ target = @cereal_box_previous_filter ? @cereal_box_previous_filter : @cereal_box_base_instance
32
+ target.send(message.to_sym, options).merge(
33
+ { cereal_box_node_name => self.send(:attributes, @cereal_box_base_instance) }
34
+ )
35
+ end
36
+
37
+ end
@@ -0,0 +1,119 @@
1
+ require 'spec_helper'
2
+
3
+ class Base
4
+ def c
5
+ "ceee"
6
+ end
7
+
8
+ def as_xml(options = {})
9
+ { :a => "a" }
10
+ end
11
+
12
+ def serializable_hash(options = {})
13
+ { :a => "a" }
14
+ end
15
+
16
+ def as_json(options = {})
17
+ { :a => "a" }
18
+ end
19
+ end
20
+
21
+ class AdditiveFilter
22
+ include CerealBox
23
+
24
+ def attributes(base)
25
+ { :b => "b", :c => base.c }
26
+ end
27
+ end
28
+
29
+ class SecondAdditiveFilter
30
+ include CerealBox
31
+
32
+ def attributes(base)
33
+ { :d => "d" }
34
+ end
35
+ end
36
+
37
+ describe CerealBox do
38
+
39
+ describe 'with the base class' do
40
+ subject { Base.new }
41
+
42
+ it 'should serialize the base class correctly' do
43
+ subject.as_json.keys.should include :a
44
+ end
45
+ end
46
+
47
+ describe 'with one additive filter' do
48
+ subject { AdditiveFilter.new(Base.new) }
49
+
50
+ it 'should include the base attributes' do
51
+ subject.as_json.keys.should include :a
52
+ end
53
+
54
+ it 'should include the additional direct attributes' do
55
+ subject.as_json[:additive].should include :b
56
+ end
57
+
58
+ it 'should include the additional method attributes' do
59
+ subject.as_json[:additive].should include :c
60
+ subject.as_json[:additive][:c].should == "ceee"
61
+ end
62
+ end
63
+
64
+ describe 'with two additive filters' do
65
+ subject { SecondAdditiveFilter.new(AdditiveFilter.new(Base.new)) }
66
+
67
+ it 'should include the base attributes' do
68
+ subject.as_json.keys.should include :a
69
+ end
70
+
71
+ it 'should include the additional direct attributes' do
72
+ subject.as_json[:additive].should include :b
73
+ end
74
+
75
+ it 'should include the additional method attributes' do
76
+ subject.as_json[:additive].should include :c
77
+ subject.as_json[:additive][:c].should == "ceee"
78
+ end
79
+
80
+ it 'should include the additional method attributes' do
81
+ subject.as_json[:second_additive].should include :d
82
+ end
83
+ end
84
+
85
+ describe 'with one additive filter and using serializable_hash' do
86
+ subject { AdditiveFilter.new(Base.new) }
87
+
88
+ it 'should include the base attributes' do
89
+ subject.serializable_hash.keys.should include :a
90
+ end
91
+
92
+ it 'should include the additional direct attributes' do
93
+ subject.serializable_hash[:additive].should include :b
94
+ end
95
+
96
+ it 'should include the additional method attributes' do
97
+ subject.serializable_hash[:additive].should include :c
98
+ subject.serializable_hash[:additive][:c].should == "ceee"
99
+ end
100
+ end
101
+
102
+ describe 'with one additive filter and using as_xml' do
103
+ subject { AdditiveFilter.new(Base.new) }
104
+
105
+ it 'should include the base attributes' do
106
+ subject.as_xml.keys.should include :a
107
+ end
108
+
109
+ it 'should include the additional direct attributes' do
110
+ subject.as_xml[:additive].should include :b
111
+ end
112
+
113
+ it 'should include the additional method attributes' do
114
+ subject.as_xml[:additive].should include :c
115
+ subject.as_xml[:additive][:c].should == "ceee"
116
+ end
117
+ end
118
+
119
+ end
@@ -0,0 +1,10 @@
1
+ # encoding: UTF-8
2
+
3
+ ENV["RAILS_ENV"] ||= "test"
4
+
5
+ PROJECT_ROOT = File.expand_path("../..", __FILE__)
6
+ $LOAD_PATH << File.join(PROJECT_ROOT, "lib")
7
+
8
+ Bundler.require
9
+
10
+ require 'cereal_box'
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cereal_box
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Christopher Meiklejohn
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-13 00:00:00.000000000 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bundler
17
+ requirement: &18087140 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *18087140
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ requirement: &18086120 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *18086120
37
+ - !ruby/object:Gem::Dependency
38
+ name: guard
39
+ requirement: &18084760 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *18084760
48
+ - !ruby/object:Gem::Dependency
49
+ name: guard-rspec
50
+ requirement: &18082860 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *18082860
59
+ - !ruby/object:Gem::Dependency
60
+ name: rake
61
+ requirement: &18050160 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *18050160
70
+ description: Serialization filters for active record.
71
+ email:
72
+ - christopher.meiklejohn@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - .rspec
79
+ - .rvmrc
80
+ - .travis.yml
81
+ - Gemfile
82
+ - Gemfile.lock
83
+ - Guardfile
84
+ - LICENSE
85
+ - README.rdoc
86
+ - Rakefile
87
+ - cereal_box.gemspec
88
+ - lib/cereal_box.rb
89
+ - lib/cereal_box/version.rb
90
+ - spec/cereal_box_spec.rb
91
+ - spec/spec_helper.rb
92
+ has_rdoc: true
93
+ homepage: https://github.com/cmeiklejohn/cereal_box
94
+ licenses: []
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ segments:
106
+ - 0
107
+ hash: 2025275564822566330
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ segments:
115
+ - 0
116
+ hash: 2025275564822566330
117
+ requirements: []
118
+ rubyforge_project: cereal_box
119
+ rubygems_version: 1.6.2
120
+ signing_key:
121
+ specification_version: 3
122
+ summary: Serialization filters for active record.
123
+ test_files: []