Permalink
Browse files

First gem release

  • Loading branch information...
BENEZECH
BENEZECH committed Sep 9, 2009
1 parent 41773e7 commit d4a902f924e999e3016f41f68e2370f0acb0424d
View
@@ -1 +1,85 @@
+= Serialist
+
+Serialize anything. Why waste time migrating your table for yet another dumb attribute you won't search on?
+Add one serializable field in your table, and let serialist do the rest.
+
+=== Before :
+
+ class Article
+ serialize :preferences
+ end
+
+ >> a = Article.new
+ >> a.preferences = {}
+ >> a.preferences[:key] = "value"
+ >> a.preferences[:key]
+ => "value"
+
+=== After :
+
+ class Article
+ serialist :preferences, [:key, :other_key, :yet_another_key]
+ end
+
+ >> a = Article.new
+ >> a.key = "value"
+ >> a.key
+ => "value"
+
+== Install the gem!
+
+ $ sudo gem install serialist
+
+== Try the demo!
+
+ $ sudo gem install sqlite3-ruby
+ $ rails -m http://gist.github.com/183689.txt serialist-example
+ $ cd serialist-example
+ $ ./script/server
+ $ GoTo localhost:3000 and create an article
+
+== Serialist comes in 2 flavors:
+
+=== Specific declaration (safe, use define_method)
+
+ serialist :my_serializer_attribute, [:foo, :bar]
+
+Allows you to serialize only the desired keys. ex :
+
+ $ ./script/console
+ >> a = Article.new
+ => #<Article id: nil, title: nil, created_at: nil, updated_at: nil, slug: nil>
+ >> a.foo = "hello"
+ => "hello"
+ >> a.taz = "hello"
+ => NoMethodError: undefined method `taz=' ...
+ >> a
+ => #<Article id: XX, title: nil, created_at: "..", updated_at: "..", slug: {:foo=>"hello"}>
+
+=== Catch-all with method_missing
+
+ 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)
+
+ # in your model. my_serializer_attribute is the one you specify in the migration
+
+ serialist :my_serializer_attribute
+
+ Allows you to serialize anything. ex :
+
+ $ ./script/console
+ >> a = Article.new
+ => #<Article id: nil, title: nil, created_at: nil, updated_at: nil, slug: nil>
+ >> a.foo = "hello"
+ => "hello"
+ >> a.foo?("hello")
+ => true
+ >> a.foo
+ => "hello"
+ >> a
+ => #<Article id: XX, title: nil, created_at: "..", updated_at: "..", slug: {:foo=>"hello"}>
+
+== Word of caution:
+
+Other than the specific notice about not using method1 with Serialist loaded before other ActiveRecord "automagicians" plugins, watch out for conflict with existing attributes and methods!
+
Copyright (c) 2009 Benoit Bénézech, released under the MIT license
View
@@ -21,3 +21,18 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end
+
+
+begin
+ require 'jeweler'
+ Jeweler::Tasks.new do |gemspec|
+ gemspec.name = "serialist"
+ gemspec.summary = "Serialize any data, set and fetch it like any column attributes"
+ gemspec.description = "Serialize any data, set and fetch it like any column attributes"
+ gemspec.email = "benoit.benezech@gmail.com"
+ gemspec.homepage = "http://github.com/bbenezech/serialist"
+ gemspec.authors = ["Benoit Bénézech"]
+ end
+rescue LoadError
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
+end
View
@@ -0,0 +1 @@
+* tests
View
@@ -0,0 +1 @@
+0.1.0
View
@@ -1,108 +0,0 @@
-#run "echo TODO > README"
-
-plugin "serialist", :git => "http://github.com/bbenezech/serialist.git"
-generate :scaffold, "article title:string"
-generate :serialist, "SerialistMigration", :articles, :slug
-rake "db:migrate"
-
-git :init
-file ".gitignore", <<-END
-.DS_Store
-log/*.log
-tmp/**/*
-config/database.yml
-db/*.sqlite3
-END
-
-file "app/views/articles/new.html.erb", <<-END
-<h1>New article</h1>
-
-<% form_for(@article) do |f| %>
- <%= f.error_messages %>
- <%= render :partial => "form"
-<% end %>
-
-<%= link_to 'Back', articles_path %>
-END
-
-file "app/views/articles/edit.html.erb", <<-END
-<h1>Editing article</h1>
-
-<% form_for(@article) do |f| %>
- <%= f.error_messages %>
- <%= render :partial => "form"
-<% end %>
-
-<%= link_to 'Show', @article %> |
-<%= link_to 'Back', articles_path %>
-END
-
-
-file "app/views/articles/_form.html.erb", <<-END
- <p>
- <%= f.label :title %><br />
- <%= f.text_field :title %>
- </p>
- <p>
- <%= f.label :string_test %><br />
- <%= f.text_field :string_test %>
- </p>
- <p>
- <%= f.label :text_test %><br />
- <%= f.text_area :text_test %>
- </p>
- <p>
- <%= f.label :select_test %><br />
- <%= f.select :select_test %>
- </p>
- <p>
- <%= f.label :checkbox_test %><br />
- <%= f.checkbox :checkbox_test %>
- </p>
- <p>
- <%= f.submit 'Update' %>
- </p>
-END
-
-
-file "app/views/articles/show.html.erb", <<-END
-<p>
- <b>Title:</b>
- <%=h @article.title %>
-</p>
-<p>
- <b>string_test:</b>
- <%=h @article.string_test %>
-</p>
-<p>
- <b>text_test:</b>
- <%=h @article.text_test %>
-</p>
-<p>
- <b>select_test:</b>
- <%=h @article.select_test %>
-</p>
-<p>
- <b>checkbox_test:</b>
- <%=h @article.checkbox_test %>
-</p>
-
-
-<%= link_to 'Edit', edit_article_path(@article) %> |
-<%= link_to 'Back', articles_path %>
-END
-
-
-
-file "app/models/article.rb", <<-END
- class Article < ActiveRecord::Base
- serialist :slug
- end
-END
-run "rm public/index.html"
-route "map.root :controller => 'articles'"
-
-git :add => ".", :commit => "-m 'initial commit'"
-
-run "echo Application ready"
-
@@ -0,0 +1,18 @@
+class SerialistGenerator < Rails::Generator::NamedBase
+ attr_accessor :class_name, :migration_name, :migrated_table, :serialist_attribute
+
+ def initialize(args, options = {})
+ super
+ @class_name = args[0]
+ @migrated_table = args[1]
+ @serialist_attribute = args[2]
+ end
+
+ def manifest
+ @migration_name = file_name.camelize
+ record do |m|
+ # Migration creation
+ m.migration_template "migrate/serialist_migration.rb.erb", "db/migrate", :migration_file_name => migration_name.underscore
+ end
+ end
+end
@@ -0,0 +1,3 @@
+script/generate serialist MigrationName MyModel my_serialist_attribute
+
+This will create a migration to use serialist on MyModel with :my_serialist_attribute
@@ -0,0 +1,9 @@
+class <%= migration_name %> < ActiveRecord::Migration
+ def self.up
+ add_column :<%= migrated_table.to_s.underscore.pluralize %>, :<%= serialist_attribute.to_s %>, :text
+ end
+
+ def self.down
+ remove_column :<%= migrated_table.to_s.underscore.pluralize %>, :<%= serialist_attribute.to_s %>
+ end
+end
View
@@ -1,3 +1 @@
-# Include hook code here
-require 'serialist'
-ActiveRecord::Base.send(:include, Serialist)
+require File.dirname(__FILE__) + "/rails/init.rb"
View
@@ -1 +0,0 @@
-# Install hook code here
View
@@ -0,0 +1,70 @@
+gem 'serialist'
+
+generate :scaffold, "article title:string"
+generate :serialist, "SerialistMigration", "Article", "slug"
+
+rake "db:migrate"
+
+file "app/models/article.rb", <<-END
+ class Article < ActiveRecord::Base
+ serialist :slug, [:required_serialized_value, :another_serialized_value]
+ validates_presence_of :title
+ validates_presence_of :required_serialized_value
+ end
+END
+
+file "app/views/articles/edit.html.erb", <<-END
+<h1>Editing article</h1>
+<%= render :partial => "form" %>
+<%= link_to 'Show', @article %> |
+<%= link_to 'Back', articles_path %>
+END
+
+file "app/views/articles/new.html.erb", <<-END
+<h1>New article</h1>
+<%= render :partial => "form" %>
+<%= link_to 'Back', articles_path %>
+END
+
+file "app/views/articles/_form.html.erb", <<-END
+<% form_for(@article) do |f| %>
+ <%= f.error_messages %>
+ <p>
+ <%= f.label :title %><br />
+ <%= f.text_field :title %>
+ </p>
+ <p>
+ <%= f.label :required_serialized_value %><br />
+ <%= f.text_field :required_serialized_value %>
+ </p>
+ <p>
+ <%= f.label :another_serialized_value %><br />
+ <%= f.text_field :another_serialized_value %>
+ </p>
+ <p>
+ <%= f.submit 'Send' %>
+ </p>
+<% end %>
+END
+
+file "app/views/articles/show.html.erb", <<-END
+<p>
+ <b>Title:</b>
+ <%=h @article.title %>
+</p>
+<p>
+ <b>Required serialized value:</b>
+ <%=h @article.required_serialized_value %>
+</p>
+<p>
+ <b>Another serialized value:</b>
+ <%=h @article.another_serialized_value %>
+</p>
+
+<%= link_to 'Edit', edit_article_path(@article) %> |
+<%= link_to 'Back', articles_path %>
+END
+
+run "rm app/views/layouts/articles.html.erb"
+run "rm public/index.html"
+route "map.root :controller => 'articles'"
Oops, something went wrong.

0 comments on commit d4a902f

Please sign in to comment.