serialist 0.5.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.2
1
+ 1.0.0
@@ -18,6 +18,8 @@ module Serialist
18
18
  include Serialist::InstanceMethods
19
19
  else
20
20
  @serialist_options.each do |field|
21
+ cols = self.columns.map{|c|c.name.to_s}
22
+ raise Exception.new("Column #{field} already exist for #{self.name}") if cols.include?(field.to_s)
21
23
  define_method field.to_s do
22
24
  return nil unless (slug = self.send(serialist_field))
23
25
  slug[field.to_sym]
@@ -46,17 +48,19 @@ module Serialist
46
48
  end
47
49
 
48
50
  module InstanceMethods
49
-
50
51
  def attributes=(new_attributes, guard_protected_attributes = true)
51
- return if new_attributes.nil?
52
+ return if new_attributes.nil?
52
53
  attributes = new_attributes.dup
53
54
  attributes.stringify_keys!
54
55
  attributes.each do |k, v|
55
- unless k.include?("(") || respond_to?(:"#{k}=")
56
+ unless k.include?("(") || respond_to?(:"#{k}")
56
57
  self.class.send(:define_method, :"#{k}=") do |param|
57
58
  self.send(self.class.serialist_field.to_s + "=", Hash.new) unless self.send(self.class.serialist_field)
58
59
  self.send(self.class.serialist_field)[k.to_sym] = param
59
60
  end
61
+ self.class.send(:define_method, :"#{k}") do
62
+ self.send(self.class.serialist_field)[k.to_sym]
63
+ end
60
64
  end
61
65
  end
62
66
  super
data/serialist.gemspec CHANGED
@@ -1,15 +1,15 @@
1
1
  # Generated by jeweler
2
- # DO NOT EDIT THIS FILE
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{serialist}
8
- s.version = "0.5.2"
8
+ s.version = "1.0.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Benoit B\303\251n\303\251zech"]
12
- s.date = %q{2009-09-17}
12
+ s.date = %q{2009-10-27}
13
13
  s.description = %q{Serialize any data, set and fetch it like any column attributes}
14
14
  s.email = %q{benoit.benezech@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -32,18 +32,15 @@ Gem::Specification.new do |s|
32
32
  "rails/init.rb",
33
33
  "serialist.gemspec",
34
34
  "tasks/acts_as_serializable_tasks.rake",
35
- "test/acts_as_serializable_test.rb",
36
- "test/test_helper.rb",
37
35
  "uninstall.rb"
38
36
  ]
39
37
  s.homepage = %q{http://github.com/bbenezech/serialist}
40
38
  s.rdoc_options = ["--charset=UTF-8"]
41
39
  s.require_paths = ["lib"]
42
- s.rubygems_version = %q{1.3.4}
40
+ s.rubygems_version = %q{1.3.5}
43
41
  s.summary = %q{Serialize any data, set and fetch it like any column attributes}
44
42
  s.test_files = [
45
- "test/acts_as_serializable_test.rb",
46
- "test/test_helper.rb"
43
+ "test/serialist_test.rb"
47
44
  ]
48
45
 
49
46
  if s.respond_to? :specification_version then
@@ -56,3 +53,4 @@ Gem::Specification.new do |s|
56
53
  else
57
54
  end
58
55
  end
56
+
@@ -0,0 +1,178 @@
1
+ require 'test/unit'
2
+
3
+ require 'rubygems'
4
+ gem 'activerecord'
5
+ require 'active_record'
6
+ RAILS_ROOT = File.dirname(__FILE__)
7
+ $: << File.join(File.dirname(__FILE__), '../lib')
8
+ require File.join(File.dirname(__FILE__), '../init')
9
+
10
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => "acts_as_url.sqlite3")
11
+
12
+ ActiveRecord::Migration.verbose = false
13
+ ActiveRecord::Schema.define(:version => 1) do
14
+ create_table :serialisted_table, :force => true do |t|
15
+ t.string :title
16
+ t.string :type
17
+ t.text :slug
18
+ t.text :slug2
19
+ end
20
+ end
21
+ ActiveRecord::Migration.verbose = true
22
+
23
+ class SerialistedLazy < ActiveRecord::Base
24
+ def self.table_name
25
+ :serialisted_table
26
+ end
27
+ serialist :slug
28
+ validates_presence_of :foo
29
+ end
30
+
31
+ class SerialistedDeclarative < ActiveRecord::Base
32
+ def self.table_name
33
+ :serialisted_table
34
+ end
35
+ serialist :slug, [:foo, :bar]
36
+ validates_presence_of :foo
37
+ end
38
+
39
+ class SubSerialistedLazy1 < SerialistedLazy
40
+ end
41
+ class SubSerialistedLazy2 < SerialistedLazy
42
+ serialist :slug, [:foo, :bar]
43
+ end
44
+
45
+ class SubSerialistedDeclarative1 < SerialistedDeclarative
46
+ end
47
+ class SubSerialistedDeclarative2 < SerialistedDeclarative
48
+ serialist :slug, [:foo, :baz]
49
+ end
50
+
51
+
52
+ class LazyRegressionTestClass < ActiveRecord::Base
53
+ def self.table_name
54
+ :serialisted_table
55
+ end
56
+ validates_presence_of :title
57
+ serialist :slug
58
+
59
+ def title_fun
60
+ title.reverse
61
+ end
62
+
63
+ end
64
+
65
+ class DeclarativeRegressionTestClass < ActiveRecord::Base
66
+ def self.table_name
67
+ :serialisted_table
68
+ end
69
+ validates_presence_of :title
70
+ serialist :slug, [:foo, :bar]
71
+
72
+ def title_fun
73
+ title.reverse
74
+ end
75
+ end
76
+
77
+
78
+ class SerialistTest < Test::Unit::TestCase
79
+ # lazy
80
+ def test_should_serialize_lazily
81
+ @serialisted = SerialistedLazy.create!({:foo => "foo", :bar => "bar"})
82
+ assert_equal @serialisted.slug[:foo], "foo"
83
+ assert_equal @serialisted.foo, "foo"
84
+ assert_equal @serialisted.foo?, true
85
+ @serialisted.foo = "foo2"
86
+ @serialisted.save
87
+ @serialisted.reload
88
+ assert_equal @serialisted.foo, "foo2"
89
+ @serialisted.bar = nil
90
+ @serialisted.save
91
+ @serialisted.reload
92
+ assert_equal @serialisted.bar?, false
93
+ end
94
+
95
+ # declarative
96
+ def test_should_be_serialisted_declaratively
97
+ @serialisted = SerialistedDeclarative.create!({:foo => "foo", :bar => "bar"})
98
+ assert_equal @serialisted.slug[:foo], "foo"
99
+ assert_equal @serialisted.foo, "foo"
100
+ assert_equal @serialisted.foo?, true
101
+ @serialisted.foo = "foo2"
102
+ @serialisted.save
103
+ @serialisted.reload
104
+ assert_equal @serialisted.foo, "foo2"
105
+ @serialisted.bar = nil
106
+ @serialisted.save
107
+ @serialisted.reload
108
+ assert_equal @serialisted.bar?, false
109
+ end
110
+
111
+ def test_should_not_serialize_unknown_attributes_when_serialisted_declaratively
112
+ assert_raise ActiveRecord::UnknownAttributeError do
113
+ SerialistedDeclarative.create({:foo => "legit", :baz => "not legit"})
114
+ end
115
+ @serialisted = SerialistedDeclarative.create!({:foo => "legit"})
116
+ assert_raise NoMethodError do
117
+ @serialisted.baz = "not legit"
118
+ end
119
+ end
120
+
121
+ # STI tests.
122
+
123
+ # test {:foo, :bar} + {} => {:foo, :bar}
124
+ def test_STI_classes_should_inherit_serialist_declarative_declaration
125
+ @serialisted = SubSerialistedDeclarative1.create!({:foo => "foo", :bar => "bar"})
126
+ assert_equal @serialisted.foo, "foo"
127
+ end
128
+
129
+ # test {:foo, :bar} + {:foo, :baz} => {:foo, :bar, :baz}
130
+ def test_declarative_serialisted_STI_classes_should_inherit_serialist_declarative_declaration
131
+ @serialisted = SubSerialistedDeclarative2.create!({:foo => "foo", :bar => "bar", :baz => "baz"})
132
+ assert_equal @serialisted.foo, "foo"
133
+ assert_equal @serialisted.bar, "bar"
134
+ assert_equal @serialisted.baz, "baz"
135
+ # but anyway :
136
+ assert_raise ActiveRecord::UnknownAttributeError do
137
+ SubSerialistedDeclarative2.create!({:catz => "dogz"})
138
+ end
139
+ assert_raise NoMethodError do
140
+ @serialisted.catz
141
+ end
142
+ end
143
+
144
+ # test {:all} + {} => {:all}
145
+ def test_STI_classes_should_inherit_serialist_lazy_declaration
146
+ @serialisted = SubSerialistedLazy1.create!({:foo => "foo", :bar => "bar"})
147
+ assert_equal @serialisted.foo, "foo"
148
+ end
149
+
150
+ # test {:all} + {:foo, :bar} => {:all}
151
+ def test_declarative_serialisted_STI_classes_should_not_override_serialist_lazy_declaration
152
+ @serialisted = SubSerialistedLazy2.create!({:foo => "foo", :bar => "bar", :baz => "baz"})
153
+ assert_equal @serialisted.baz, "baz"
154
+ end
155
+
156
+ # non-regression test
157
+ def test_mass_assignement_to_lazily_serialisted_should_not_create_record_if_record_is_invalid
158
+ @serialisted = LazyRegressionTestClass.create({:foo => "foo", :bar => "bar"})
159
+ assert_equal @serialisted.new_record?, true
160
+ assert_nil @serialisted.id
161
+ end
162
+
163
+ def test_mass_assignement_to_declarative_serialisted_should_not_create_record_if_record_is_invalid
164
+ @serialisted = DeclarativeRegressionTestClass.create({:foo => "foo", :bar => "bar"})
165
+ assert_equal @serialisted.new_record?, true
166
+ assert_nil @serialisted.id
167
+ end
168
+
169
+ def test_lazily_serialist_should_not_try_to_override_existing_methods_and_columns
170
+ assert_raise ActiveRecord::UnknownAttributeError do
171
+ LazyRegressionTestClass.create!({:foo => "foo", :bar => "bar", :title => "hoho", :title_fun => "haha"})
172
+ end
173
+ @serialisted = LazyRegressionTestClass.create!({:foo => "foo", :bar => "bar", :title => "hoho"})
174
+ assert_nil @serialisted.slug[:title]
175
+ assert_equal @serialisted.title, "hoho"
176
+ assert_equal @serialisted.title_fun, "ohoh"
177
+ end
178
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serialist
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Benoit B\xC3\xA9n\xC3\xA9zech"
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-17 00:00:00 +02:00
12
+ date: 2009-10-27 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -38,8 +38,6 @@ files:
38
38
  - rails/init.rb
39
39
  - serialist.gemspec
40
40
  - tasks/acts_as_serializable_tasks.rake
41
- - test/acts_as_serializable_test.rb
42
- - test/test_helper.rb
43
41
  - uninstall.rb
44
42
  has_rdoc: true
45
43
  homepage: http://github.com/bbenezech/serialist
@@ -65,10 +63,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
65
63
  requirements: []
66
64
 
67
65
  rubyforge_project:
68
- rubygems_version: 1.3.4
66
+ rubygems_version: 1.3.5
69
67
  signing_key:
70
68
  specification_version: 3
71
69
  summary: Serialize any data, set and fetch it like any column attributes
72
70
  test_files:
73
- - test/acts_as_serializable_test.rb
74
- - test/test_helper.rb
71
+ - test/serialist_test.rb
@@ -1,8 +0,0 @@
1
- require 'test_helper'
2
-
3
- class ActsAsSerializableTest < ActiveSupport::TestCase
4
- # Replace this with your real tests.
5
- test "the truth" do
6
- assert true
7
- end
8
- end
data/test/test_helper.rb DELETED
@@ -1,3 +0,0 @@
1
- require 'rubygems'
2
- require 'active_support'
3
- require 'active_support/test_case'