agile_serializer 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,6 +1,5 @@
1
1
  require 'rake'
2
2
  require 'rake/testtask'
3
- require 'rake/rdoctask'
4
3
 
5
4
  desc 'Default: run unit tests.'
6
5
  task :default => :test
@@ -12,12 +11,3 @@ Rake::TestTask.new(:test) do |t|
12
11
  t.pattern = 'test/**/*_test.rb'
13
12
  t.verbose = true
14
13
  end
15
-
16
- desc 'Generate documentation for the serialize_with_options plugin.'
17
- Rake::RDocTask.new(:rdoc) do |rdoc|
18
- rdoc.rdoc_dir = 'rdoc'
19
- rdoc.title = 'SerializeWithOptions'
20
- rdoc.options << '--line-numbers' << '--inline-source'
21
- rdoc.rdoc_files.include('README')
22
- rdoc.rdoc_files.include('lib/**/*.rb')
23
- end
@@ -1,3 +1,3 @@
1
1
  module AgileSerializer
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -4,20 +4,25 @@ module AgileSerializer
4
4
  require 'agile_serializer/railtie'
5
5
  end
6
6
 
7
+ def self.extended(base)
8
+ base.class_attribute(:serializer_configuration)
9
+ base.class_attribute(:serializer_options)
10
+ end
11
+
7
12
  def serialize_with_options(set = :default, &block)
8
- configuration = read_inheritable_attribute(:configuration) || {}
9
- options = read_inheritable_attribute(:options) || {}
13
+ configuration = self.serializer_configuration.try(:dup) || {}
14
+ options = self.serializer_options.try(:dup) || {}
10
15
 
11
16
  configuration[set] = Config.new(configuration).instance_eval(&block)
12
17
 
13
- write_inheritable_attribute :configuration, configuration
14
- write_inheritable_attribute :options, options
18
+ self.serializer_configuration = configuration
19
+ self.serializer_options = options
15
20
 
16
21
  include InstanceMethods
17
22
  end
18
23
 
19
24
  def serialization_configuration(set)
20
- configuration = read_inheritable_attribute(:configuration)
25
+ configuration = self.serializer_configuration
21
26
  conf = if configuration
22
27
  configuration[set] || configuration[:default]
23
28
  end
@@ -26,7 +31,8 @@ module AgileSerializer
26
31
  end
27
32
 
28
33
  def serialization_options(set)
29
- options = read_inheritable_attribute(:options)
34
+ options = self.serializer_options
35
+
30
36
  options[set] ||= serialization_configuration(set).tap do |opts|
31
37
  includes = opts.delete(:includes)
32
38
 
@@ -47,7 +53,8 @@ module AgileSerializer
47
53
  end
48
54
  end
49
55
  end
50
- write_inheritable_attribute :options, options
56
+
57
+ self.serializer_options = options
51
58
  options[set]
52
59
  end
53
60
 
@@ -69,7 +76,7 @@ module AgileSerializer
69
76
  raise "Not known configuration!" unless @conf[set]
70
77
  @data = @conf[set].dup
71
78
  end
72
-
79
+
73
80
  end
74
81
 
75
82
  module InstanceMethods
@@ -4,7 +4,7 @@ class User < ActiveRecord::Base
4
4
  has_many :posts
5
5
  has_many :blog_posts
6
6
  has_many :check_ins
7
-
7
+
8
8
  serialize_with_options do
9
9
  methods :post_count
10
10
  includes :posts
@@ -13,6 +13,7 @@ class User < ActiveRecord::Base
13
13
 
14
14
  serialize_with_options(:deep) do
15
15
  includes :check_ins
16
+ methods :post_count2
16
17
  end
17
18
 
18
19
  serialize_with_options(:with_email) do
@@ -23,7 +24,7 @@ class User < ActiveRecord::Base
23
24
  serialize_with_options(:with_comments) do
24
25
  includes :posts => { :include => :comments }
25
26
  end
26
-
27
+
27
28
  serialize_with_options(:with_check_ins) do
28
29
  includes :check_ins
29
30
  dasherize false
@@ -33,6 +34,10 @@ class User < ActiveRecord::Base
33
34
  def post_count
34
35
  self.posts.count
35
36
  end
37
+
38
+ def post_count2
39
+ self.posts.count
40
+ end
36
41
  end
37
42
 
38
43
  class Post < ActiveRecord::Base
@@ -41,7 +46,7 @@ class Post < ActiveRecord::Base
41
46
 
42
47
  serialize_with_options do
43
48
  only :title
44
- includes :user, :comments
49
+ includes :comments
45
50
  end
46
51
 
47
52
  serialize_with_options(:deep) do
@@ -57,6 +62,8 @@ class BlogPost < Post
57
62
  serialize_with_options(:with_email) do
58
63
  includes :user
59
64
  end
65
+
66
+
60
67
  end
61
68
 
62
69
  class Comment < ActiveRecord::Base
@@ -77,65 +84,66 @@ class CheckIn < ActiveRecord::Base
77
84
  end
78
85
 
79
86
  class SerializeWithOptionsTest < Test::Unit::TestCase
80
- def self.should_serialize_with_options
81
- should "include active_record attributes" do
82
- assert_equal @user.name, @user_hash["name"]
83
- end
87
+ def json(obj, opts={})
88
+ obj.as_json(opts).with_indifferent_access
89
+ end
84
90
 
85
- should "include specified methods" do
86
- assert_equal @user.post_count, @user_hash["post_count"]
87
- end
91
+ context "Serialize with options simple test" do
92
+ setup do
93
+ @user = User.create(:name => "John User", :email => "john@example.com")
94
+ @post = @user.posts.create(:title => "Hello World!", :content => "Welcome to my blog.")
95
+ @blog_post = @user.blog_posts.create(:title => "Hello World!", :content => "Welcome to my blog.")
96
+ @checkin = @user.check_ins.create(:code_name => "Natasa")
88
97
 
89
- should "exclude specified attributes" do
90
- assert_equal nil, @user_hash["email"]
98
+ [@user, @post, @blog_post, @checkin].map(&:reload)
91
99
  end
92
100
 
93
- should "exclude attributes not in :only list" do
94
- assert_equal nil, @post_hash["content"]
95
- end
101
+ context "#default" do
96
102
 
97
- should "include specified associations" do
98
- assert_equal @post.title, @user_hash["posts"].first["title"]
99
- end
100
-
101
- should "be identical in inherited model" do
102
- assert_equal @post_hash["title"], @blog_post_hash["title"]
103
- end
104
-
105
- should "include specified methods on associations" do
106
- assert_equal @user.post_count, @post_hash["user"]["post_count"]
107
- end
103
+ setup do
104
+ @user_hash = json( @user )
105
+ @post_hash = json( @post )
106
+ @blog_post_hash = json( @blog_post )
107
+ end
108
108
 
109
- should "exclude specified methods on associations" do
110
- assert_equal nil, @post_hash["user"]["email"]
111
- end
109
+ should "include active_record attributes" do
110
+ assert_equal @user.name, @user_hash["name"]
111
+ end
112
112
 
113
- should "not include associations of associations" do
114
- assert_equal nil, @user_hash["posts"].first["comments"]
115
- end
113
+ should "include specified methods" do
114
+ assert_equal @user.post_count, @user_hash["post_count"]
115
+ end
116
116
 
117
- should "include association without serialization options properly" do
118
- assert_equal @comment.content, @post_hash["comments"].first["content"]
119
- end
120
-
121
- should "override sets on inherited models" do
122
- assert_equal nil, @blog_post_hash["comments"].first
123
- end
124
- end
117
+ should "exclude specified attributes" do
118
+ assert_equal nil, @user_hash["email"]
119
+ end
125
120
 
126
- context "propagate includes" do
127
- setup do
128
- @user = User.create(:name => "John User", :email => "john@example.com")
129
- @post = @user.posts.create(:title => "Hello World!", :content => "Welcome to my blog.")
130
- @checkin = @user.check_ins.create(:code_name => "Natasa")
121
+ should "exclude attributes not in :only list" do
122
+ assert_equal nil, @post_hash["content"]
123
+ end
124
+
125
+ should "include specified associations" do
126
+ assert_equal @post.title, @user_hash["posts"].first["title"]
127
+ end
128
+
129
+ should "be identical in inherited model" do
130
+ assert_equal @post_hash["title"], @blog_post_hash["title"]
131
+ end
131
132
  end
132
133
 
133
- should "Include deep relations" do
134
- post_hash = JSON.parse @post.to_json(:flavor => :deep)
135
- assert_equal @user.email, post_hash["user"]["email"]
136
- assert_equal @checkin.code_name, post_hash["user"]["check_ins"].first['code_name']
134
+ context "#deep" do
135
+ setup do
136
+ @post_hash = json( @post, :flavor => :deep )
137
+ end
138
+
139
+ should "include specified methods on associations" do
140
+ assert_equal @user.post_count2, @post_hash["user"]["post_count2"]
141
+ end
142
+
143
+ should "include specified associations of associations" do
144
+ assert_equal @checkin.code_name, @post_hash["user"]["check_ins"].first["code_name"]
145
+ end
137
146
  end
138
-
139
147
  end
140
148
 
141
149
  end
data/test/test_helper.rb CHANGED
@@ -1,17 +1,23 @@
1
- require 'rubygems'
1
+ require 'bundler/setup'
2
+
3
+ require 'logger'
2
4
  require 'active_record'
3
- require 'test/unit'
5
+ require 'turn'
4
6
  require 'shoulda'
5
7
  require 'json'
8
+ require 'ap'
6
9
 
7
- $:.unshift(File.dirname(__FILE__) + '/../lib')
8
- require 'serialize_with_options'
9
- require File.dirname(__FILE__) + "/../init"
10
+ lib = File.expand_path('../../lib/', __FILE__)
11
+ $:.unshift lib unless $:.include?(lib)
12
+ require 'agile_serializer'
13
+ require 'agile_serializer/active_record'
10
14
 
11
15
  ActiveRecord::Base.establish_connection(
12
16
  :adapter => 'sqlite3',
13
17
  :database => 'test.db'
14
18
  )
19
+ ActiveRecord::Base.include_root_in_json = false
20
+ ActiveRecord::Base.logger = Logger.new('test.log')
15
21
 
16
22
  [:users, :posts, :comments, :check_ins, :reviews].each do |table|
17
23
  ActiveRecord::Base.connection.drop_table table rescue nil
metadata CHANGED
@@ -1,70 +1,77 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: agile_serializer
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 0
8
- - 6
9
- version: 0.0.6
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.7
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Christos Trochalakis
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2011-04-10 00:00:00 +03:00
18
- default_executable:
19
- dependencies: []
20
-
21
- description: A fork of serializer_with_options enabling deep serializer and other features
12
+ date: 2012-09-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: &34381420 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *34381420
25
+ - !ruby/object:Gem::Dependency
26
+ name: railties
27
+ requirement: &34380920 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '3.0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *34380920
36
+ description: A fork of serializer_with_options enabling deep serializer and other
37
+ features
22
38
  email: yatiohi@ideopolis.gr
23
39
  executables: []
24
-
25
40
  extensions: []
26
-
27
41
  extra_rdoc_files: []
28
-
29
- files:
42
+ files:
30
43
  - README.markdown
31
44
  - Rakefile
32
45
  - MIT-LICENSE
33
46
  - lib/agile_serializer.rb
34
- - lib/agile_serializer/version.rb
35
47
  - lib/agile_serializer/railtie.rb
48
+ - lib/agile_serializer/version.rb
49
+ - lib/agile_serializer/active_record.rb
36
50
  - test/test_helper.rb
37
51
  - test/serialize_with_options_test.rb
38
- - rails/init.rb
39
- has_rdoc: true
40
52
  homepage: http://github.com/ctrochalakis/agile_serializer
41
53
  licenses: []
42
-
43
54
  post_install_message:
44
55
  rdoc_options: []
45
-
46
- require_paths:
56
+ require_paths:
47
57
  - lib
48
- required_ruby_version: !ruby/object:Gem::Requirement
49
- requirements:
50
- - - ">="
51
- - !ruby/object:Gem::Version
52
- segments:
53
- - 0
54
- version: "0"
55
- required_rubygems_version: !ruby/object:Gem::Requirement
56
- requirements:
57
- - - ">="
58
- - !ruby/object:Gem::Version
59
- segments:
60
- - 0
61
- version: "0"
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
62
70
  requirements: []
63
-
64
71
  rubyforge_project:
65
- rubygems_version: 1.3.6
72
+ rubygems_version: 1.8.11
66
73
  signing_key:
67
74
  specification_version: 3
68
75
  summary: Enhanced serialize options for rails, forked from serialize_with_options
69
76
  test_files: []
70
-
77
+ has_rdoc: false