serializer 0.0.1
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/Gemfile +3 -0
- data/LICENSE +20 -0
- data/README.rdoc +52 -0
- data/Rakefile +2 -0
- data/lib/serializer/initializer.rb +11 -0
- data/lib/serializer/railtie.rb +14 -0
- data/lib/serializer/version.rb +3 -0
- data/lib/serializer.rb +61 -0
- metadata +54 -0
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 - 2011 Kevin Sylvestre
|
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,52 @@
|
|
1
|
+
= Serializer
|
2
|
+
|
3
|
+
Serializer is a Ruby on Rails tool for adding accessor to serialized attributes with support for types and defaults.
|
4
|
+
|
5
|
+
== Requirements
|
6
|
+
|
7
|
+
The gem is tested with Ruby 1.9.2 and Rails 3.1.0 but may well work with other versions of Ruby and Rails.
|
8
|
+
|
9
|
+
== Installation
|
10
|
+
|
11
|
+
gem install serializer
|
12
|
+
|
13
|
+
== Examples
|
14
|
+
|
15
|
+
Migration:
|
16
|
+
|
17
|
+
rails g model user name:string email:string settings:text
|
18
|
+
|
19
|
+
Model:
|
20
|
+
|
21
|
+
class User < ActiveRecord::Base
|
22
|
+
|
23
|
+
has_serialized :settings do |settings|
|
24
|
+
settings.define :tw_share, type: :boolean, default: true
|
25
|
+
settings.define :fb_share, type: :boolean, default: true
|
26
|
+
settings.define :completion, type: :float, default: 50.0
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
Form:
|
32
|
+
|
33
|
+
<%= form_for @user do |form| %>
|
34
|
+
<%= form.text_field :name %>
|
35
|
+
<%= form.email_field :email %>
|
36
|
+
<%= form.checkbox_box :tw_share %>
|
37
|
+
<%= form.checkbox_box :fb_share %>
|
38
|
+
<% end %>
|
39
|
+
|
40
|
+
View:
|
41
|
+
|
42
|
+
<%- if @user.tw_share? -%>
|
43
|
+
...
|
44
|
+
<%- end -%>
|
45
|
+
|
46
|
+
<%- if @user.fb_share? -%>
|
47
|
+
...
|
48
|
+
<%- end -%>
|
49
|
+
|
50
|
+
== Copyright
|
51
|
+
|
52
|
+
Copyright (c) 2010 - 2011 Kevin Sylvestre. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'serializer'
|
2
|
+
require 'rails'
|
3
|
+
|
4
|
+
module Serializer
|
5
|
+
class Railtie < Rails::Railtie
|
6
|
+
|
7
|
+
initializer 'serializer.initialize' do
|
8
|
+
ActiveSupport.on_load(:active_record) do
|
9
|
+
ActiveRecord::Base.send :include, Serializer
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
data/lib/serializer.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'serializer/railtie'
|
2
|
+
require 'serializer/initializer'
|
3
|
+
|
4
|
+
module Serializer
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.extend ClassMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
|
12
|
+
# Add serializer to a class.
|
13
|
+
#
|
14
|
+
# Usage:
|
15
|
+
#
|
16
|
+
# has_serialized :settings do |settings|
|
17
|
+
# settings.define :tw_share, default: true, type: :boolean
|
18
|
+
# settings.define :fb_share, default: true, type: :boolean
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
|
22
|
+
def has_serialized(name, &block)
|
23
|
+
serialize name, Hash
|
24
|
+
|
25
|
+
initializer = Serializer::Initializer.new
|
26
|
+
block.call(initializer)
|
27
|
+
|
28
|
+
initializer.each do |method, options|
|
29
|
+
|
30
|
+
define_method "#{method}" do
|
31
|
+
result = send(name)[method.intern]
|
32
|
+
return options[:default] if result.nil?
|
33
|
+
return result
|
34
|
+
end
|
35
|
+
|
36
|
+
define_method "#{method}?" do
|
37
|
+
result = send(name)[method.intern]
|
38
|
+
return options[:default] if result.nil?
|
39
|
+
return result
|
40
|
+
end
|
41
|
+
|
42
|
+
define_method "#{method}=" do |value|
|
43
|
+
if options[:type] and value
|
44
|
+
case options[:type].intern
|
45
|
+
when :float then value = value.to_f if value.respond_to? :to_f
|
46
|
+
when :integer then value = value.to_i if value.respond_to? :to_i
|
47
|
+
when :string then value = value.to_str if value.respond_to? :to_str
|
48
|
+
when :symbol then value = value.to_sym if value.respond_to? :to_sym
|
49
|
+
when :boolean then value = !value.to_i.zero? if value.respond_to? :to_i
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
send(name)[method.intern] = value
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: serializer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kevin Sylvestre
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-09-28 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: Serializer is a Ruby on Rails tool for adding accessor to serialized
|
15
|
+
attributes with support for types and defaults.
|
16
|
+
email:
|
17
|
+
- kevin@ksylvest.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- lib/serializer/initializer.rb
|
23
|
+
- lib/serializer/railtie.rb
|
24
|
+
- lib/serializer/version.rb
|
25
|
+
- lib/serializer.rb
|
26
|
+
- README.rdoc
|
27
|
+
- LICENSE
|
28
|
+
- Gemfile
|
29
|
+
- Rakefile
|
30
|
+
homepage: http://github.com/ksylvest/serializer
|
31
|
+
licenses: []
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirements: []
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.8.10
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: A serialized attribute accessor gem with support for types and defaults
|
54
|
+
test_files: []
|