dm-is-serialized 0.9.10
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/LICENSE +20 -0
- data/README.textile +29 -0
- data/Rakefile +54 -0
- data/TODO +8 -0
- data/dm-is-serialized.gemspec +22 -0
- data/lib/dm-is-serialized.rb +17 -0
- data/lib/dm-is-serialized/is/filters.rb +66 -0
- data/lib/dm-is-serialized/is/serialized.rb +68 -0
- data/lib/dm-is-serialized/is/version.rb +7 -0
- data/spec/dm-is-serialized/serialized_spec.rb +120 -0
- data/spec/spec.opts +5 -0
- data/spec/spec_helper.rb +10 -0
- metadata +74 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Jakub Stastny aka Botanicus
|
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.textile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
h1. About
|
2
|
+
|
3
|
+
This plugin helps you to serialize your records into very short strings for storing in cookies.
|
4
|
+
|
5
|
+
h1. Initialization
|
6
|
+
|
7
|
+
<pre>
|
8
|
+
class Product
|
9
|
+
include DataMapper::Resource
|
10
|
+
property :id, Serial
|
11
|
+
property :name, String
|
12
|
+
end
|
13
|
+
|
14
|
+
class OrderItem
|
15
|
+
include DataMapper::Resource
|
16
|
+
is :serialized
|
17
|
+
has 1, :product
|
18
|
+
property :id, Serial
|
19
|
+
property :name, String
|
20
|
+
property :count, Integer, :default => 1
|
21
|
+
property :used, Boolean, :default => false
|
22
|
+
property :note, Text
|
23
|
+
serialize_properties :count, :note # GeneralSerializeFilter is default value
|
24
|
+
serialize_property :product, ModelSerializeFilter
|
25
|
+
serialize_property :used, BooleanSerializeFilter
|
26
|
+
end
|
27
|
+
</pre>
|
28
|
+
|
29
|
+
h1. Filters
|
data/Rakefile
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake/gempackagetask'
|
3
|
+
require 'lib/dm-is-serialized/is/version'
|
4
|
+
|
5
|
+
GEM_NAME = "dm-is-serialized"
|
6
|
+
AUTHOR = "Jakub Stastny aka Botanicus"
|
7
|
+
EMAIL = "knava.bestvinensis@gmail.com"
|
8
|
+
HOMEPAGE = "http://101Ideas.cz/"
|
9
|
+
SUMMARY = "This plugin helps you to serialize your records into very short strings for storing in cookies"
|
10
|
+
GEM_VERSION = DataMapper::Is::Serialized::VERSION
|
11
|
+
|
12
|
+
spec = Gem::Specification.new do |s|
|
13
|
+
s.name = GEM_NAME
|
14
|
+
s.version = GEM_VERSION
|
15
|
+
s.platform = Gem::Platform::RUBY
|
16
|
+
s.has_rdoc = true
|
17
|
+
s.extra_rdoc_files = ["README.textile", "LICENSE"]
|
18
|
+
s.summary = SUMMARY
|
19
|
+
s.description = s.summary
|
20
|
+
s.author = AUTHOR
|
21
|
+
s.email = EMAIL
|
22
|
+
s.homepage = HOMEPAGE
|
23
|
+
s.require_path = 'lib'
|
24
|
+
s.files = %w(LICENSE README.textile Rakefile) + Dir.glob("{lib,spec,app,public,stubs}/**/*")
|
25
|
+
end
|
26
|
+
|
27
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
28
|
+
pkg.gem_spec = spec
|
29
|
+
end
|
30
|
+
|
31
|
+
desc "Create a gemspec file"
|
32
|
+
task :gemspec do
|
33
|
+
File.open("#{GEM_NAME}.gemspec", "w") do |file|
|
34
|
+
file.puts spec.to_ruby
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def sudo_gem(cmd)
|
39
|
+
sh "#{SUDO} #{RUBY} -S gem #{cmd}", :verbose => false
|
40
|
+
end
|
41
|
+
|
42
|
+
desc "Install #{GEM_NAME} #{GEM_VERSION}"
|
43
|
+
task :install => [ :package ] do
|
44
|
+
sudo_gem "install --local pkg/#{GEM_NAME}-#{GEM_VERSION} --no-update-sources"
|
45
|
+
end
|
46
|
+
|
47
|
+
desc "Uninstall #{GEM_NAME} #{GEM_VERSION}"
|
48
|
+
task :uninstall => [ :clobber ] do
|
49
|
+
sudo_gem "uninstall #{GEM_NAME} -v#{GEM_VERSION} -Ix"
|
50
|
+
end
|
51
|
+
|
52
|
+
require 'spec/rake/spectask'
|
53
|
+
desc 'Default: run spec examples'
|
54
|
+
task :default => 'spec'
|
data/TODO
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
begin
|
2
|
+
require "rubygems/specification"
|
3
|
+
rescue SecurityError
|
4
|
+
# http://gems.github.com
|
5
|
+
end
|
6
|
+
|
7
|
+
VERSION = "0.9.10"
|
8
|
+
SPECIFICATION = ::Gem::Specification.new do |s|
|
9
|
+
s.name = "dm-is-serialized"
|
10
|
+
s.version = VERSION
|
11
|
+
s.authors = ["Jakub Šťastný aka Botanicus"]
|
12
|
+
s.homepage = "http://github.com/botanicus/dm-is-serialized"
|
13
|
+
s.summary = "This plugin helps you to serialize your records into very short strings for storing in cookies."
|
14
|
+
# s.description = "" # TODO: long description
|
15
|
+
s.cert_chain = nil
|
16
|
+
s.email = ["knava.bestvinensis", "gmail.com"].join("@")
|
17
|
+
s.files = Dir.glob("**/*") - Dir.glob("pkg/*")
|
18
|
+
s.add_dependency "dm-core"
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
# s.required_ruby_version = ::Gem::Requirement.new(">= 1.9.1")
|
21
|
+
# s.rubyforge_project = "rango"
|
22
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# Needed to import datamapper and other gems
|
2
|
+
require 'rubygems'
|
3
|
+
require 'pathname'
|
4
|
+
|
5
|
+
# Add all external dependencies for the plugin here
|
6
|
+
gem 'dm-core'
|
7
|
+
require 'dm-core'
|
8
|
+
|
9
|
+
# Require plugin-files
|
10
|
+
require Pathname(__FILE__).dirname.expand_path/'dm-is-serialized'/'is'/'serialized'
|
11
|
+
|
12
|
+
# Include the plugin in Resource
|
13
|
+
module DataMapper
|
14
|
+
module Model
|
15
|
+
include DataMapper::Is::Serialized
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module DataMapper
|
2
|
+
module Is
|
3
|
+
module Serialized
|
4
|
+
module Filters
|
5
|
+
# superclass for all filter
|
6
|
+
class SerializeFilter
|
7
|
+
# record: object of model (just in serialize)
|
8
|
+
# name: symbol with property name
|
9
|
+
attr_accessor :record, :name
|
10
|
+
def property
|
11
|
+
@record.class.properties[@name]
|
12
|
+
end
|
13
|
+
|
14
|
+
def relationship(name)
|
15
|
+
@record.class.relationships[@name]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# serialize: true => 1, false => 0
|
20
|
+
class BooleanSerializeFilter < SerializeFilter
|
21
|
+
def serialize(data)
|
22
|
+
data ? 1 : 0
|
23
|
+
end
|
24
|
+
|
25
|
+
def deserialize(data)
|
26
|
+
data.to_i == 1
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# serialize: :product => product_id
|
31
|
+
class ModelSerializeFilter < SerializeFilter
|
32
|
+
# serialize(:product)
|
33
|
+
def serialize(record)
|
34
|
+
keys = model.key.map { |property| property.name }
|
35
|
+
raise "Record can't be nil (#{model} with keys #{keys})" if record.nil?
|
36
|
+
values = keys.map { |key| record.send(key) }
|
37
|
+
values.length.eql?(1) ? values.first : values.join("|")
|
38
|
+
end
|
39
|
+
|
40
|
+
# product_id => id
|
41
|
+
def deserialize(id)
|
42
|
+
ids = id.split("|")
|
43
|
+
model.get(*ids)
|
44
|
+
end
|
45
|
+
|
46
|
+
def model
|
47
|
+
Object.const_get(@name.to_s.camel_case)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# called when any filter found
|
52
|
+
class GeneralSerializeFilter < SerializeFilter
|
53
|
+
def serialize(data)
|
54
|
+
raise "Data too long" if data.to_s.length > 20
|
55
|
+
return data
|
56
|
+
end
|
57
|
+
|
58
|
+
def deserialize(data)
|
59
|
+
# "" => nil
|
60
|
+
data.empty? ? nil : data
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require Pathname(__FILE__).dirname.expand_path/'filters'
|
2
|
+
|
3
|
+
module DataMapper
|
4
|
+
module Is
|
5
|
+
module Serialized
|
6
|
+
def self.included(base)
|
7
|
+
end
|
8
|
+
|
9
|
+
def is_serialized(options = Hash.new)
|
10
|
+
# Add class-methods
|
11
|
+
extend DataMapper::Is::Serialized::ClassMethods
|
12
|
+
# Add instance-methods
|
13
|
+
include DataMapper::Is::Serialized::InstanceMethods
|
14
|
+
include DataMapper::Is::Serialized::Filters
|
15
|
+
self.serialized_properties = Array.new # otherwise it will be nil and it will cause NoMethodError
|
16
|
+
end
|
17
|
+
|
18
|
+
module ClassMethods
|
19
|
+
include DataMapper::Is::Serialized::Filters
|
20
|
+
attr_accessor :serialized_properties
|
21
|
+
|
22
|
+
# serialize_property :size, BooleanSerializedFilter
|
23
|
+
def serialize_property(property, filter = GeneralSerializeFilter)
|
24
|
+
self.serialized_properties ||= Array.new
|
25
|
+
self.serialized_properties.push([property, filter])
|
26
|
+
self.serialized_properties.uniq!
|
27
|
+
end
|
28
|
+
|
29
|
+
# serialize_properties :product, :count
|
30
|
+
# serialize_properties :product, :count, GeneralSerializeFilter
|
31
|
+
def serialize_properties(*properties)
|
32
|
+
self.serialized_properties ||= Array.new
|
33
|
+
filter = properties.last.class.eql?(Class) ? properties.pop : GeneralSerializeFilter
|
34
|
+
properties.each do |property|
|
35
|
+
self.serialized_properties.push([property, filter])
|
36
|
+
end
|
37
|
+
self.serialized_properties.uniq!
|
38
|
+
end
|
39
|
+
|
40
|
+
def deserialize(data)
|
41
|
+
params = Hash.new
|
42
|
+
data.split(",").each_with_index do |data, index|
|
43
|
+
property_name, filter_class = self.serialized_properties[index]
|
44
|
+
filter = filter_class.new
|
45
|
+
filter.name = property_name
|
46
|
+
value = filter.deserialize(data)
|
47
|
+
#value.nil? ? raise(TypeError) : value
|
48
|
+
params[property_name] = value
|
49
|
+
end
|
50
|
+
return self.new(params)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
module InstanceMethods
|
55
|
+
# @product.serialize => #<Product ...>
|
56
|
+
def serialize
|
57
|
+
self.class.serialized_properties.map do |property_name, filter_class|
|
58
|
+
filter = filter_class.new
|
59
|
+
filter.record = self
|
60
|
+
filter.name = property_name
|
61
|
+
data = self.send(property_name)
|
62
|
+
data.nil? ? String.new : filter.serialize(data)
|
63
|
+
end.join(",")
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', "spec_helper")
|
2
|
+
require "dm-is-serialized"
|
3
|
+
|
4
|
+
include DataMapper::Is::Serialized::Filters
|
5
|
+
|
6
|
+
class Product
|
7
|
+
include DataMapper::Resource
|
8
|
+
property :id, Serial
|
9
|
+
property :name, String
|
10
|
+
end
|
11
|
+
|
12
|
+
class OrderItem
|
13
|
+
include DataMapper::Resource
|
14
|
+
is :serialized
|
15
|
+
has 1, :product
|
16
|
+
property :id, Serial
|
17
|
+
property :name, String
|
18
|
+
property :count, Integer, :default => 1
|
19
|
+
property :used, Boolean, :default => false
|
20
|
+
property :note, Text
|
21
|
+
end
|
22
|
+
|
23
|
+
describe DataMapper::Is::Serialized do
|
24
|
+
def serialize_property(property, filter)
|
25
|
+
OrderItem.serialized_properties.clear if OrderItem.serialized_properties
|
26
|
+
OrderItem.serialize_property property, filter
|
27
|
+
end
|
28
|
+
|
29
|
+
before do
|
30
|
+
# Do not save it, it is the reason why all the required
|
31
|
+
# properties are saved in cookies - that the record
|
32
|
+
# isn't saved yet. In shopping cart for example.
|
33
|
+
@item = OrderItem.new
|
34
|
+
end
|
35
|
+
|
36
|
+
describe BooleanSerializeFilter do
|
37
|
+
before do
|
38
|
+
serialize_property :used, BooleanSerializeFilter
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "false" do
|
42
|
+
it "should serialize false value" do
|
43
|
+
@item.used = false
|
44
|
+
@item.serialize.should eql("0")
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should deserialize false value" do
|
48
|
+
item = OrderItem.deserialize("0")
|
49
|
+
item.used.should be_false
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "true" do
|
54
|
+
it "should serialize true value" do
|
55
|
+
@item.used = true
|
56
|
+
@item.serialize.should eql("1")
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should deserialize true value" do
|
60
|
+
item = OrderItem.deserialize("1")
|
61
|
+
item.used.should be_true
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe GeneralSerializeFilter do
|
67
|
+
describe "integers" do
|
68
|
+
before do
|
69
|
+
@item.count = 5
|
70
|
+
serialize_property :count, GeneralSerializeFilter
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should serialize integers" do
|
74
|
+
@item.serialize.should eql("5")
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should deserialize integers" do
|
78
|
+
item = OrderItem.deserialize("5")
|
79
|
+
item.count.should eql(5)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe "strings" do
|
84
|
+
before do
|
85
|
+
@item.note = "quickly pls!"
|
86
|
+
serialize_property :note, GeneralSerializeFilter
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should serialize strings" do
|
90
|
+
@item.serialize.should eql("quickly pls!")
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should raise exception if string is too long" do
|
94
|
+
lambda { @item.serialize.should eql("too much long string") }.should raise_error
|
95
|
+
end
|
96
|
+
|
97
|
+
it "should deserialize strings" do
|
98
|
+
item = OrderItem.deserialize("quickly pls!")
|
99
|
+
item.note.should eql("quickly pls!")
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
describe ModelSerializeFilter do
|
105
|
+
before do
|
106
|
+
@product = Product.create
|
107
|
+
@item.product = @product
|
108
|
+
serialize_property :product, ModelSerializeFilter
|
109
|
+
end
|
110
|
+
|
111
|
+
it "should serialize boolean values" do
|
112
|
+
@item.serialize.should eql(@product.id.to_s)
|
113
|
+
end
|
114
|
+
|
115
|
+
it "should deserialize boolean values" do
|
116
|
+
item = OrderItem.deserialize(@product.id.to_s)
|
117
|
+
item.product.should eql(@product)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dm-is-serialized
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.10
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- "Jakub \xC5\xA0\xC5\xA5astn\xC3\xBD aka Botanicus"
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain:
|
11
|
+
date: 2009-11-26 00:00:00 +00:00
|
12
|
+
default_executable:
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: dm-core
|
16
|
+
type: :runtime
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: "0"
|
23
|
+
version:
|
24
|
+
description:
|
25
|
+
email: knava.bestvinensis@gmail.com
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files: []
|
31
|
+
|
32
|
+
files:
|
33
|
+
- dm-is-serialized.gemspec
|
34
|
+
- lib/dm-is-serialized/is/filters.rb
|
35
|
+
- lib/dm-is-serialized/is/serialized.rb
|
36
|
+
- lib/dm-is-serialized/is/version.rb
|
37
|
+
- lib/dm-is-serialized.rb
|
38
|
+
- LICENSE
|
39
|
+
- Rakefile
|
40
|
+
- README.textile
|
41
|
+
- spec/dm-is-serialized/serialized_spec.rb
|
42
|
+
- spec/spec.opts
|
43
|
+
- spec/spec_helper.rb
|
44
|
+
- TODO
|
45
|
+
has_rdoc: true
|
46
|
+
homepage: http://github.com/botanicus/dm-is-serialized
|
47
|
+
licenses: []
|
48
|
+
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
version:
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
version:
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.3.5
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: This plugin helps you to serialize your records into very short strings for storing in cookies.
|
73
|
+
test_files: []
|
74
|
+
|