serialist 0.1.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/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,85 @@
1
+ = Serialist
2
+
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.
5
+
6
+ === Before :
7
+
8
+ class Article
9
+ serialize :preferences
10
+ end
11
+
12
+ >> a = Article.new
13
+ >> a.preferences = {}
14
+ >> a.preferences[:key] = "value"
15
+ >> a.preferences[:key]
16
+ => "value"
17
+
18
+ === After :
19
+
20
+ class Article
21
+ serialist :preferences, [:key, :other_key, :yet_another_key]
22
+ end
23
+
24
+ >> a = Article.new
25
+ >> a.key = "value"
26
+ >> a.key
27
+ => "value"
28
+
29
+ == Install the gem!
30
+
31
+ $ sudo gem install serialist
32
+
33
+ == Try the demo!
34
+
35
+ $ sudo gem install sqlite3-ruby
36
+ $ rails -m http://gist.github.com/183689.txt serialist-example
37
+ $ cd serialist-example
38
+ $ ./script/server
39
+ $ GoTo localhost:3000 and create an article
40
+
41
+ == Serialist comes in 2 flavors:
42
+
43
+ === Specific declaration (safe, use define_method)
44
+
45
+ serialist :my_serializer_attribute, [:foo, :bar]
46
+
47
+ Allows you to serialize only the desired keys. ex :
48
+
49
+ $ ./script/console
50
+ >> a = Article.new
51
+ => #<Article id: nil, title: nil, created_at: nil, updated_at: nil, slug: nil>
52
+ >> a.foo = "hello"
53
+ => "hello"
54
+ >> a.taz = "hello"
55
+ => NoMethodError: undefined method `taz=' ...
56
+ >> a
57
+ => #<Article id: XX, title: nil, created_at: "..", updated_at: "..", slug: {:foo=>"hello"}>
58
+
59
+ === Catch-all with method_missing
60
+
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)
62
+
63
+ # in your model. my_serializer_attribute is the one you specify in the migration
64
+
65
+ serialist :my_serializer_attribute
66
+
67
+ Allows you to serialize anything. ex :
68
+
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"}>
80
+
81
+ == Word of caution:
82
+
83
+ 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!
84
+
85
+ Copyright (c) 2009 Benoit Bénézech, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,38 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the acts_as_serializable plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the acts_as_serializable plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'ActsAsSerializable'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
24
+
25
+
26
+ begin
27
+ require 'jeweler'
28
+ Jeweler::Tasks.new do |gemspec|
29
+ gemspec.name = "serialist"
30
+ gemspec.summary = "Serialize any data, set and fetch it like any column attributes"
31
+ gemspec.description = "Serialize any data, set and fetch it like any column attributes"
32
+ gemspec.email = "benoit.benezech@gmail.com"
33
+ gemspec.homepage = "http://github.com/bbenezech/serialist"
34
+ gemspec.authors = ["Benoit Bénézech"]
35
+ end
36
+ rescue LoadError
37
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
38
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/rails/init.rb"
data/install.rb ADDED
File without changes
data/lib/serialist.rb ADDED
@@ -0,0 +1,79 @@
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
+
File without changes
@@ -0,0 +1,8 @@
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
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: serialist
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - "Benoit B\xC3\xA9n\xC3\xA9zech"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-09 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Serialize any data, set and fetch it like any column attributes
17
+ email: benoit.benezech@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - MIT-LICENSE
26
+ - README.rdoc
27
+ - Rakefile
28
+ - init.rb
29
+ - install.rb
30
+ - lib/serialist.rb
31
+ - tasks/acts_as_serializable_tasks.rake
32
+ - test/acts_as_serializable_test.rb
33
+ - test/test_helper.rb
34
+ - uninstall.rb
35
+ has_rdoc: true
36
+ homepage: http://github.com/bbenezech/serialist
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options:
41
+ - --charset=UTF-8
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.3.4
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Serialize any data, set and fetch it like any column attributes
63
+ test_files:
64
+ - test/acts_as_serializable_test.rb
65
+ - test/test_helper.rb