serialist 0.1.0 → 0.2.0

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/README.rdoc CHANGED
@@ -5,7 +5,7 @@ Add one serializable field in your table, and let serialist do the rest.
5
5
 
6
6
  === Before :
7
7
 
8
- class Article
8
+ class Article < ActiveRecord::Base
9
9
  serialize :preferences
10
10
  end
11
11
 
@@ -17,7 +17,7 @@ Add one serializable field in your table, and let serialist do the rest.
17
17
 
18
18
  === After :
19
19
 
20
- class Article
20
+ class Article < ActiveRecord::Base
21
21
  serialist :preferences, [:key, :other_key, :yet_another_key]
22
22
  end
23
23
 
@@ -38,6 +38,24 @@ Add one serializable field in your table, and let serialist do the rest.
38
38
  $ ./script/server
39
39
  $ GoTo localhost:3000 and create an article
40
40
 
41
+ == Or generate a migration for your existing programm
42
+
43
+ ./script/generate serialist SerialistMigration MyModel my_serialist_attribute
44
+ Ex :
45
+
46
+ ./script/generate serialist SerialistMigration Article slug
47
+
48
+ Then hook Serialist into your ActiveRecord model :
49
+
50
+ serialist :my_serialist_attribute, [:foo, :bar]
51
+ # OR
52
+ serialist :my_serialist_attribute
53
+ # See below
54
+
55
+ # Add validation as you normally would :
56
+ validates_presence_of :bar
57
+ # etc.
58
+
41
59
  == Serialist comes in 2 flavors:
42
60
 
43
61
  === Specific declaration (safe, use define_method)
@@ -58,25 +76,25 @@ Allows you to serialize only the desired keys. ex :
58
76
 
59
77
  === Catch-all with method_missing
60
78
 
61
- 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 (=== find_all_by_title_and_description kind of magic)
79
+ 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 (=== find_all_by_title_and_description kind of magic)
62
80
 
63
- # in your model. my_serializer_attribute is the one you specify in the migration
81
+ # in your model. my_serializer_attribute is the one you specify in the migration
64
82
 
65
- serialist :my_serializer_attribute
83
+ serialist :my_serializer_attribute
66
84
 
67
- Allows you to serialize anything. ex :
85
+ Allows you to serialize anything. ex :
68
86
 
69
- $ ./script/console
70
- >> a = Article.new
71
- => #<Article id: nil, title: nil, created_at: nil, updated_at: nil, slug: nil>
72
- >> a.foo = "hello"
73
- => "hello"
74
- >> a.foo?("hello")
75
- => true
76
- >> a.foo
77
- => "hello"
78
- >> a
79
- => #<Article id: XX, title: nil, created_at: "..", updated_at: "..", slug: {:foo=>"hello"}>
87
+ $ ./script/console
88
+ >> a = Article.new
89
+ => #<Article id: nil, title: nil, created_at: nil, updated_at: nil, slug: nil>
90
+ >> a.foo = "hello"
91
+ => "hello"
92
+ >> a.foo?("hello")
93
+ => true
94
+ >> a.foo
95
+ => "hello"
96
+ >> a
97
+ => #<Article id: XX, title: nil, created_at: "..", updated_at: "..", slug: {:foo=>"hello"}>
80
98
 
81
99
  == Word of caution:
82
100
 
data/TODO.txt ADDED
@@ -0,0 +1 @@
1
+ * tests
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.0
@@ -0,0 +1,18 @@
1
+ class SerialistGenerator < Rails::Generator::NamedBase
2
+ attr_accessor :class_name, :migration_name, :migrated_table, :serialist_attribute
3
+
4
+ def initialize(args, options = {})
5
+ super
6
+ @class_name = args[0]
7
+ @migrated_table = args[1]
8
+ @serialist_attribute = args[2]
9
+ end
10
+
11
+ def manifest
12
+ @migration_name = file_name.camelize
13
+ record do |m|
14
+ # Migration creation
15
+ m.migration_template "migrate/serialist_migration.rb.erb", "db/migrate", :migration_file_name => migration_name.underscore
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ script/generate serialist MigrationName MyModel my_serialist_attribute
2
+
3
+ This will create a migration to use serialist on MyModel with :my_serialist_attribute
@@ -0,0 +1,9 @@
1
+ class <%= migration_name %> < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :<%= migrated_table.to_s.underscore.pluralize %>, :<%= serialist_attribute.to_s %>, :text
4
+ end
5
+
6
+ def self.down
7
+ remove_column :<%= migrated_table.to_s.underscore.pluralize %>, :<%= serialist_attribute.to_s %>
8
+ end
9
+ end
@@ -0,0 +1,70 @@
1
+ gem 'serialist'
2
+
3
+ generate :scaffold, "article title:string"
4
+ generate :serialist, "SerialistMigration", "Article", "slug"
5
+
6
+ rake "db:migrate"
7
+
8
+ file "app/models/article.rb", <<-END
9
+ class Article < ActiveRecord::Base
10
+ serialist :slug, [:required_serialized_value, :another_serialized_value]
11
+ validates_presence_of :title
12
+ validates_presence_of :required_serialized_value
13
+ end
14
+ END
15
+
16
+ file "app/views/articles/edit.html.erb", <<-END
17
+ <h1>Editing article</h1>
18
+ <%= render :partial => "form" %>
19
+ <%= link_to 'Show', @article %> |
20
+ <%= link_to 'Back', articles_path %>
21
+ END
22
+
23
+ file "app/views/articles/new.html.erb", <<-END
24
+ <h1>New article</h1>
25
+ <%= render :partial => "form" %>
26
+ <%= link_to 'Back', articles_path %>
27
+ END
28
+
29
+ file "app/views/articles/_form.html.erb", <<-END
30
+ <% form_for(@article) do |f| %>
31
+ <%= f.error_messages %>
32
+ <p>
33
+ <%= f.label :title %><br />
34
+ <%= f.text_field :title %>
35
+ </p>
36
+ <p>
37
+ <%= f.label :required_serialized_value %><br />
38
+ <%= f.text_field :required_serialized_value %>
39
+ </p>
40
+ <p>
41
+ <%= f.label :another_serialized_value %><br />
42
+ <%= f.text_field :another_serialized_value %>
43
+ </p>
44
+ <p>
45
+ <%= f.submit 'Send' %>
46
+ </p>
47
+ <% end %>
48
+ END
49
+
50
+ file "app/views/articles/show.html.erb", <<-END
51
+ <p>
52
+ <b>Title:</b>
53
+ <%=h @article.title %>
54
+ </p>
55
+ <p>
56
+ <b>Required serialized value:</b>
57
+ <%=h @article.required_serialized_value %>
58
+ </p>
59
+ <p>
60
+ <b>Another serialized value:</b>
61
+ <%=h @article.another_serialized_value %>
62
+ </p>
63
+
64
+ <%= link_to 'Edit', edit_article_path(@article) %> |
65
+ <%= link_to 'Back', articles_path %>
66
+ END
67
+
68
+ run "rm app/views/layouts/articles.html.erb"
69
+ run "rm public/index.html"
70
+ route "map.root :controller => 'articles'"
data/lib/serialist.rb CHANGED
@@ -1,79 +1,2 @@
1
- # ActsAsSerializable
2
- module Serialist
3
-
4
- def self.included(base)
5
- base.extend ClassMethods
6
- end
7
-
8
- module ClassMethods
9
- attr_accessor :serialist_options
10
- attr_accessor :serialist_field
11
-
12
- def serialist(serialist_field, serialist_options = [])
13
-
14
-
15
- @serialist_field ||= serialist_field
16
- @serialist_options ||= []
17
- @serialist_options = (@serialist_options + serialist_options).uniq
18
- serialize(@serialist_field, Hash)
19
-
20
- if serialist_options.empty?
21
- include Serialist::InstanceMethods
22
- else
23
- @serialist_options.each do |field|
24
- define_method field.to_s do
25
- return nil unless (slug = self.send(serialist_field))
26
- slug[field]
27
- end
28
- define_method field.to_s + "?" do |*param|
29
- return false unless (slug = self.send(serialist_field))
30
- if param.empty?
31
- ![nil, false, "false", :false].include?(slug[field])
32
- else
33
- slug[field] == param.first
34
- end
35
- end
36
- define_method field.to_s + "=" do |param|
37
- update_attribute(serialist_field, Hash.new) unless self.send(serialist_field)
38
- self.send(serialist_field)[field] = param
39
- end
40
- end
41
- end
42
- end
43
-
44
- def inherited(subclass)
45
- super
46
- subclass.instance_variable_set("@serialist_field", @serialist_field)
47
- subclass.instance_variable_set("@serialist_options", @serialist_options)
48
- end
49
- end
50
-
51
- module InstanceMethods
52
- def method_missing(method, *args, &block)
53
- begin
54
- super
55
- rescue NoMethodError
56
- slug = self.send(self.class.serialist_field)
57
-
58
- case method.to_s.last
59
- when "?"
60
- slug && slug[method.to_s[0..-2].to_sym] == (args && args.first || "true")
61
-
62
- if args.empty?
63
- slug && ![nil, false, "false", :false].include?(slug[method.to_s[0..-2].to_sym])
64
- else
65
- slug && (slug[method.to_s[0..-2].to_sym] == args.first)
66
- end
67
-
68
- when "="
69
- update_attribute(self.class.serialist_field, Hash.new) unless slug
70
- self.send(self.class.serialist_field)[method.to_s[0..-2].to_sym] = args.first
71
- else
72
- slug && slug[method]
73
- end
74
- end
75
- end
76
- end
77
- end
78
-
79
-
1
+ require 'serialist/serialist_module'
2
+ ActiveRecord::Base.send(:include, Serialist)
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'serialist'
data/serialist.gemspec ADDED
@@ -0,0 +1,57 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{serialist}
8
+ s.version = "0.2.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Benoit B\303\251n\303\251zech"]
12
+ s.date = %q{2009-09-09}
13
+ s.description = %q{Serialize any data, set and fetch it like any column attributes}
14
+ s.email = %q{benoit.benezech@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ "MIT-LICENSE",
20
+ "README.rdoc",
21
+ "Rakefile",
22
+ "TODO.txt",
23
+ "VERSION",
24
+ "generators/serialist/serialist_generator.rb",
25
+ "generators/serialist/templates/USAGE",
26
+ "generators/serialist/templates/migrate/serialist_migration.rb.erb",
27
+ "init.rb",
28
+ "install.rb",
29
+ "installation-template.txt",
30
+ "lib/serialist.rb",
31
+ "rails/init.rb",
32
+ "serialist.gemspec",
33
+ "tasks/acts_as_serializable_tasks.rake",
34
+ "test/acts_as_serializable_test.rb",
35
+ "test/test_helper.rb",
36
+ "uninstall.rb"
37
+ ]
38
+ s.homepage = %q{http://github.com/bbenezech/serialist}
39
+ s.rdoc_options = ["--charset=UTF-8"]
40
+ s.require_paths = ["lib"]
41
+ s.rubygems_version = %q{1.3.4}
42
+ s.summary = %q{Serialize any data, set and fetch it like any column attributes}
43
+ s.test_files = [
44
+ "test/acts_as_serializable_test.rb",
45
+ "test/test_helper.rb"
46
+ ]
47
+
48
+ if s.respond_to? :specification_version then
49
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
50
+ s.specification_version = 3
51
+
52
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
53
+ else
54
+ end
55
+ else
56
+ end
57
+ 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.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Benoit B\xC3\xA9n\xC3\xA9zech"
@@ -25,9 +25,17 @@ files:
25
25
  - MIT-LICENSE
26
26
  - README.rdoc
27
27
  - Rakefile
28
+ - TODO.txt
29
+ - VERSION
30
+ - generators/serialist/serialist_generator.rb
31
+ - generators/serialist/templates/USAGE
32
+ - generators/serialist/templates/migrate/serialist_migration.rb.erb
28
33
  - init.rb
29
34
  - install.rb
35
+ - installation-template.txt
30
36
  - lib/serialist.rb
37
+ - rails/init.rb
38
+ - serialist.gemspec
31
39
  - tasks/acts_as_serializable_tasks.rake
32
40
  - test/acts_as_serializable_test.rb
33
41
  - test/test_helper.rb