serialist 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,12 +1,15 @@
1
1
  = Serialist
2
2
 
3
3
  Serialize anything. Why waste time migrating your table for yet another dumb attribute you won't search on?
4
- Add one serializable field in your table, and let serialist do the rest.
4
+ Add one serialization field in your table, and let serialist do the rest : validate and mass_assign all your serialized stuff, transparently.
5
+ Now in version 1.0.0, fully tested.
5
6
 
6
7
  === Before!
7
8
 
8
9
  class Article < ActiveRecord::Base
9
10
  serialize :preferences
11
+ # impossible...
12
+ # validates_presence_of :key
10
13
  end
11
14
 
12
15
  >> a = Article.new
@@ -14,17 +17,25 @@ Add one serializable field in your table, and let serialist do the rest.
14
17
  >> a.preferences[:key] = "value"
15
18
  >> a.preferences[:key]
16
19
  => "value"
20
+ >> Article.create({:preferences => {:key => "value"}})
21
+ => #<Article id: ##, preferences: {:key=>"value"}>
17
22
 
18
23
  === After!
19
24
 
20
25
  class Article < ActiveRecord::Base
21
26
  serialist :preferences, [:key, :other_key, :yet_another_key]
27
+ validates_presence_of :key
22
28
  end
23
29
 
24
30
  >> a = Article.new
25
31
  >> a.key = "value"
26
32
  >> a.key
27
33
  => "value"
34
+ >> Article.create!
35
+ => "key cannot be blank"
36
+ >> Article.create({:key => "value"})
37
+ => #<Article id: ##, preferences: {:key=>"value"}>
38
+
28
39
 
29
40
  == Install the gem!
30
41
 
@@ -58,15 +69,21 @@ Then hook Serialist into your ActiveRecord model :
58
69
  validates_presence_of :bar
59
70
  # etc.
60
71
 
61
- == Serialist comes in TWO flavors!
72
+ == Serialist comes in 2 flavors!
62
73
 
63
- === Specific declaration (safe, use define_method)
74
+ === Declarative, use define_method as your model load.
64
75
 
65
- serialist :slug, [:foo, :bar]
76
+ class Article
77
+ serialist :slug, [:foo, :bar]
78
+ validates_presence_of :foo
79
+ end
80
+
66
81
 
67
82
  Allows you to serialize only the desired keys. ex :
68
83
 
69
84
  $ ./script/console
85
+ >> Article.create!
86
+ => 'foo cannot be blank'
70
87
  >> a = Article.new
71
88
  => #<Article id: nil, title: nil, created_at: nil, updated_at: nil, slug: nil>
72
89
  >> a.foo?
@@ -82,17 +99,18 @@ Allows you to serialize only the desired keys. ex :
82
99
  >> a
83
100
  => #<Article id: XX, title: nil, created_at: "..", updated_at: "..", slug: {:foo=>"hello"}>
84
101
 
85
- === Catch-all with method_missing
86
-
87
- You should probably choose to load serialist after your other plugins/gems, because ActiveRecord won't fire NoMethodError anymore on Serialisted models, and some plugin may want to catch it to implement their own auto-magic (aka find_all_by_title_and_description magic)
102
+ === Catch-all, use define_method lazily at access time (hooked in your model method_missing)
88
103
 
89
- # in your model. my_serializer_attribute is the one you specify in the migration
90
-
91
- serialist :slug
104
+ class Article
105
+ serialist :slug
106
+ validates_presence_of :foo
107
+ end
92
108
 
93
109
  Allows you to serialize anything. ex :
94
110
 
95
111
  $ ./script/console
112
+ >> Article.create!
113
+ => 'foo cannot be blank'
96
114
  >> a = Article.new
97
115
  => #<Article id: nil, title: nil, created_at: nil, updated_at: nil, slug: nil>
98
116
  >> a.foo
@@ -114,14 +132,14 @@ Allows you to serialize anything. ex :
114
132
  >> a
115
133
  => #<Article id: XX, title: nil, created_at: "..", updated_at: "..", slug: {:foo=>"hello"}>
116
134
 
117
- == Beware!
135
+ === But be aware...
118
136
 
119
137
  # Don't use method#2 with Serialist loaded before other ActiveRecord "automagicians" plugins
120
138
 
121
- # Watch out for conflict with existing attributes and methods!
122
-
123
139
  # And of course don't serialize attributes you may want to search on, or index on, or use with any other database related stuff.
124
140
 
141
+ # run the tests with your version of ActiveRecord (tested with rails 2.3.4) (go to the unpacked gem and simply run 'rake', watch for errors. Send a bug report if any, specifying you ActiveRecord or rails version, thanks)
142
+
125
143
  Copyright (c) 2009 Benoit Bénézech, released under the MIT license
126
144
 
127
145
  http://rubyonrails.org/images/rails.png
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.1.0
@@ -20,26 +20,31 @@ module Serialist
20
20
  @serialist_options.each do |field|
21
21
  cols = self.columns.map{|c|c.name.to_s}
22
22
  raise Exception.new("Column #{field} already exist for #{self.name}") if cols.include?(field.to_s)
23
- define_method field.to_s do
24
- return nil unless (slug = self.send(serialist_field))
25
- slug[field.to_sym]
26
- end
27
- define_method field.to_s + "?" do |*param|
28
- return false unless (slug = self.send(serialist_field))
29
- if param.empty?
30
- ![nil, false, "false", :false].include?(slug[field.to_sym])
31
- else
32
- slug[field.to_sym] == param.first
33
- end
34
- end
35
- define_method field.to_s + "=" do |param|
36
- self.send(serialist_field.to_s + "=", Hash.new) unless self.send(serialist_field)
37
- self.send(serialist_field)[field.to_sym] = param
38
- end
23
+ define_access_methods(field)
39
24
  end
40
25
  end
41
26
  end
42
27
 
28
+ def define_access_methods(field)
29
+ serialist_field = self.serialist_field
30
+ define_method field.to_s do
31
+ return nil unless (slug = self.send(serialist_field))
32
+ slug[field.to_sym]
33
+ end
34
+ define_method field.to_s + "?" do |*param|
35
+ return false unless (slug = self.send(serialist_field))
36
+ if param.empty?
37
+ ![nil, false, "false", :false, "0"].include?(slug[field.to_sym])
38
+ else
39
+ slug[field.to_sym] == param.first
40
+ end
41
+ end
42
+ define_method field.to_s + "=" do |param|
43
+ self.send(serialist_field.to_s + "=", Hash.new) unless self.send(serialist_field)
44
+ self.send(serialist_field)[field.to_sym] = param
45
+ end
46
+ end
47
+
43
48
  def inherited(subclass)
44
49
  super
45
50
  subclass.instance_variable_set("@serialist_field", @serialist_field)
@@ -53,14 +58,8 @@ module Serialist
53
58
  attributes = new_attributes.dup
54
59
  attributes.stringify_keys!
55
60
  attributes.each do |k, v|
56
- unless k.include?("(") || respond_to?(:"#{k}")
57
- self.class.send(:define_method, :"#{k}=") do |param|
58
- self.send(self.class.serialist_field.to_s + "=", Hash.new) unless self.send(self.class.serialist_field)
59
- self.send(self.class.serialist_field)[k.to_sym] = param
60
- end
61
- self.class.send(:define_method, :"#{k}") do
62
- self.send(self.class.serialist_field)[k.to_sym]
63
- end
61
+ unless k.include?("(") || respond_to?(k)
62
+ self.class.define_access_methods(k)
64
63
  end
65
64
  end
66
65
  super
@@ -70,20 +69,8 @@ module Serialist
70
69
  begin
71
70
  super
72
71
  rescue NoMethodError
73
- slug = self.send(self.class.serialist_field)
74
- case method.to_s.last
75
- when "?"
76
- if args.empty?
77
- slug && ![nil, false, "false", :false].include?(slug[method.to_s[0..-2].to_sym])
78
- else
79
- slug && (slug[method.to_s[0..-2].to_sym] == args.first)
80
- end
81
- when "="
82
- self.send(self.class.serialist_field.to_s + "=", Hash.new) unless slug
83
- self.send(self.class.serialist_field)[method.to_s[0..-2].to_sym] = args.first
84
- else
85
- slug && slug[method.to_sym]
86
- end
72
+ self.class.define_access_methods(method.to_s.chomp("=").chomp("?"))
73
+ self.send(method, *args, &block)
87
74
  end
88
75
  end
89
76
  end
data/serialist.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{serialist}
8
- s.version = "1.0.0"
8
+ s.version = "1.1.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-10-27}
12
+ s.date = %q{2009-10-29}
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 = [
@@ -19,7 +19,6 @@ Gem::Specification.new do |s|
19
19
  "MIT-LICENSE",
20
20
  "README.rdoc",
21
21
  "Rakefile",
22
- "TODO.txt",
23
22
  "VERSION",
24
23
  "generators/serialist/serialist_generator.rb",
25
24
  "generators/serialist/templates/USAGE",
@@ -32,6 +31,7 @@ Gem::Specification.new do |s|
32
31
  "rails/init.rb",
33
32
  "serialist.gemspec",
34
33
  "tasks/acts_as_serializable_tasks.rake",
34
+ "test/serialist_test.rb",
35
35
  "uninstall.rb"
36
36
  ]
37
37
  s.homepage = %q{http://github.com/bbenezech/serialist}
@@ -86,6 +86,13 @@ class SerialistTest < Test::Unit::TestCase
86
86
  @serialisted.save
87
87
  @serialisted.reload
88
88
  assert_equal @serialisted.foo, "foo2"
89
+ @serialisted.baz = "baz"
90
+ @serialisted.save
91
+ @serialisted.reload
92
+ assert_equal @serialisted.baz, "baz"
93
+ assert_equal @serialisted.baz?, true
94
+ assert_equal @serialisted.baz?("baz"), true
95
+ assert_equal @serialisted.baz?("bar"), false
89
96
  @serialisted.bar = nil
90
97
  @serialisted.save
91
98
  @serialisted.reload
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: 1.0.0
4
+ version: 1.1.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-10-27 00:00:00 +01:00
12
+ date: 2009-10-29 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -25,7 +25,6 @@ files:
25
25
  - MIT-LICENSE
26
26
  - README.rdoc
27
27
  - Rakefile
28
- - TODO.txt
29
28
  - VERSION
30
29
  - generators/serialist/serialist_generator.rb
31
30
  - generators/serialist/templates/USAGE
@@ -38,6 +37,7 @@ files:
38
37
  - rails/init.rb
39
38
  - serialist.gemspec
40
39
  - tasks/acts_as_serializable_tasks.rake
40
+ - test/serialist_test.rb
41
41
  - uninstall.rb
42
42
  has_rdoc: true
43
43
  homepage: http://github.com/bbenezech/serialist
data/TODO.txt DELETED
@@ -1 +0,0 @@
1
- * tests