super_serialize 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.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +97 -0
- data/Rakefile +27 -0
- data/lib/super_serialize/version.rb +3 -0
- data/lib/super_serialize.rb +155 -0
- data/lib/tasks/super_serialize_tasks.rake +4 -0
- metadata +93 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 49276a8cd980eecd175a71086a6b864818148eb2
|
4
|
+
data.tar.gz: cac85ec1846157b96b1821e4dbf8bb9015b00f7a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ff53b0e707c04c0e894ca9021644603237341d7cde910879b9fcd45fa0a9646e6a836efd2b38e929b5ddc03f98fcb15691b21fa7762acc1a673d8ba09a4e8904
|
7
|
+
data.tar.gz: 37f8c8ebdee0a8a1c1a4d9915b2501ce61914aa4e5329b7e1c42e089f091f4f2391c330dbdf649450b38d9f89144002c4cb0fe9b1ee6bf61cfd6d68facc75e74
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2014 YOURNAME
|
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.md
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
# SuperSerialize
|
2
|
+
|
3
|
+
A super, simple way to serialize anything from fixnums and floats to arrays and hashes.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'super_serialize'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install super_serialize
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
**In your model**
|
22
|
+
|
23
|
+
class SomeModel < ActiveRecord::Base
|
24
|
+
super_serialize :varied_attr_type, :other_varied_attr_type
|
25
|
+
|
26
|
+
...
|
27
|
+
end
|
28
|
+
|
29
|
+
**Examples**:
|
30
|
+
|
31
|
+
> sm = SomeModel.new
|
32
|
+
> sm.varied_attr_type = 3
|
33
|
+
> sm.varied_attr_type
|
34
|
+
=> 3
|
35
|
+
|
36
|
+
> sm.varied_attr_type.class
|
37
|
+
=> Fixnum
|
38
|
+
|
39
|
+
> sm.varied_attr_type = "3"
|
40
|
+
> sm.varied_attr_type
|
41
|
+
=> 3
|
42
|
+
|
43
|
+
> sm.varied_attr_type.class
|
44
|
+
=> Fixnum
|
45
|
+
|
46
|
+
> sm.varied_attr_type = "3.0"
|
47
|
+
> sm.varied_attr_type
|
48
|
+
=> 3.0
|
49
|
+
|
50
|
+
> sm.varied_attr_type.class
|
51
|
+
=> Float
|
52
|
+
|
53
|
+
> sm.varied_attr_type = "some string"
|
54
|
+
> sm.varied_attr_type
|
55
|
+
=> "some string"
|
56
|
+
|
57
|
+
> sm.varied_attr_type.class
|
58
|
+
=> String
|
59
|
+
|
60
|
+
> sm.varied_attr_type = [1,2]
|
61
|
+
> sm.varied_attr_type
|
62
|
+
=> [1,2]
|
63
|
+
|
64
|
+
> sm.varied_attr_type.class
|
65
|
+
=> Array
|
66
|
+
|
67
|
+
> sm.varied_attr_type = "[1,2,3]"
|
68
|
+
> sm.varied_attr_type
|
69
|
+
=> [1,2]
|
70
|
+
|
71
|
+
> sm.varied_attr_type.class
|
72
|
+
=> Array
|
73
|
+
|
74
|
+
> sm.varied_attr_type = "{key: 'some value'}"
|
75
|
+
> sm.varied_attr_type
|
76
|
+
=> {"key" => 'some value'}
|
77
|
+
|
78
|
+
> sm.varied_attr_type.class
|
79
|
+
=> ActiveSupport::HashWithIndifferentAccess
|
80
|
+
|
81
|
+
> sm.varied_attr_type = {key: 'some value'}
|
82
|
+
> sm.varied_attr_type
|
83
|
+
=> {"key" => 'some value'}
|
84
|
+
|
85
|
+
> sm.varied_attr_type.class
|
86
|
+
=> ActiveSupport::HashWithIndifferentAccess
|
87
|
+
|
88
|
+
|
89
|
+
## Contributing
|
90
|
+
|
91
|
+
1. Fork it ( http://github.com/ricardo-quinones/super_serialize/fork )
|
92
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
93
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
94
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
95
|
+
5. Create new Pull Request
|
96
|
+
|
97
|
+
This project rocks and uses MIT-LICENSE.
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'SuperSerialize'
|
18
|
+
rdoc.options << '--line-numbers'
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
Bundler::GemHelper.install_tasks
|
27
|
+
|
@@ -0,0 +1,155 @@
|
|
1
|
+
module SuperSerialize
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
# Validations for super serialized columns
|
6
|
+
validate :check_if_super_serialized_attributes_are_valid_yaml
|
7
|
+
|
8
|
+
# Callback to yamlize super serialized columngs just prior to saving to the database
|
9
|
+
before_save :yamlize_super_serialized_attributes
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
def super_serialize(*attr_names)
|
14
|
+
# === Arguments
|
15
|
+
# +attr_names+:: the name of the of the attributes to be super_serialized
|
16
|
+
# === Example
|
17
|
+
# class SomeModel < ActiveRecord::Base
|
18
|
+
# super_serialize :varied_attr_type, :other_varied_attr_type
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# Attr type and class dynamically set based on the detected class type.
|
22
|
+
# Value can be set with objects or with strings that gets serialized to
|
23
|
+
# appropriate object via the YAML library
|
24
|
+
#
|
25
|
+
# === Example usage
|
26
|
+
# > sm = SomeModel.new
|
27
|
+
# > sm.varied_attr_type = 3
|
28
|
+
# > sm.varied_attr_type
|
29
|
+
# => 3
|
30
|
+
# > sm.varied_attr_type.class
|
31
|
+
# => Fixnum
|
32
|
+
# > sm.varied_attr_type = "3"
|
33
|
+
# > sm.varied_attr_type
|
34
|
+
# => 3
|
35
|
+
# > sm.varied_attr_type.class
|
36
|
+
# => Fixnum
|
37
|
+
# > sm.varied_attr_type = "3.0"
|
38
|
+
# > sm.varied_attr_type
|
39
|
+
# => 3.0
|
40
|
+
# > sm.varied_attr_type.class
|
41
|
+
# => Float
|
42
|
+
# > sm.varied_attr_type = "some string"
|
43
|
+
# > sm.varied_attr_type
|
44
|
+
# => "some string"
|
45
|
+
# > sm.varied_attr_type.class
|
46
|
+
# => String
|
47
|
+
# > sm.varied_attr_type = [1,2]
|
48
|
+
# > sm.varied_attr_type
|
49
|
+
# => [1,2]
|
50
|
+
# > sm.varied_attr_type.class
|
51
|
+
# => Array
|
52
|
+
# > sm.varied_attr_type = "[1,2,3]"
|
53
|
+
# > sm.varied_attr_type
|
54
|
+
# => [1,2]
|
55
|
+
# > sm.varied_attr_type.class
|
56
|
+
# => Array
|
57
|
+
# > sm.varied_attr_type = "{key: 'some value'}"
|
58
|
+
# > sm.varied_attr_type
|
59
|
+
# => {"key" => 'some value'}
|
60
|
+
# > sm.varied_attr_type.class
|
61
|
+
# => ActiveSupport::HashWithIndifferentAccess
|
62
|
+
# > sm.varied_attr_type = {key: 'some value'}
|
63
|
+
# > sm.varied_attr_type
|
64
|
+
# => {"key" => 'some value'}
|
65
|
+
# > sm.varied_attr_type.class
|
66
|
+
# => ActiveSupport::HashWithIndifferentAccess
|
67
|
+
|
68
|
+
attr_names.each do |attr_name|
|
69
|
+
unless column_names.include?(attr_name.to_s)
|
70
|
+
raise ArgumentError, ":#{attr_name} is not a valid column name for #{name}."
|
71
|
+
end
|
72
|
+
|
73
|
+
unless attr_name.is_a? Symbol
|
74
|
+
raise ArgumentError, "Please pass in symbols as arguments."
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
attr_names.each do |attr_name|
|
79
|
+
class_eval <<-RUBY, __FILE__, __LINE__+1
|
80
|
+
def #{attr_name}
|
81
|
+
value = super
|
82
|
+
return value.with_indifferent_access if value.is_a?(Hash)
|
83
|
+
return value unless is_valid_yaml?(value)
|
84
|
+
|
85
|
+
object = YAML::load(value)
|
86
|
+
object.is_a?(Hash) ? object.with_indifferent_access : object
|
87
|
+
end
|
88
|
+
|
89
|
+
def #{attr_name}=(value)
|
90
|
+
current_changed_attributes = changed_attributes.dup
|
91
|
+
return_value = super(value)
|
92
|
+
|
93
|
+
if #{attr_name} == #{attr_name}_was
|
94
|
+
@changed_attributes = current_changed_attributes
|
95
|
+
end
|
96
|
+
|
97
|
+
return_value
|
98
|
+
end
|
99
|
+
|
100
|
+
def #{attr_name}_was
|
101
|
+
value = super
|
102
|
+
|
103
|
+
if persisted? && is_valid_yaml?(value)
|
104
|
+
YAML::load(value)
|
105
|
+
else
|
106
|
+
value
|
107
|
+
end
|
108
|
+
end
|
109
|
+
RUBY
|
110
|
+
end
|
111
|
+
|
112
|
+
class_eval do
|
113
|
+
unless const_defined?('SUPER_SERIALIZED_ATTRIBUTES')
|
114
|
+
const_set('SUPER_SERIALIZED_ATTRIBUTES', attr_names)
|
115
|
+
end
|
116
|
+
|
117
|
+
private
|
118
|
+
|
119
|
+
def is_valid_yaml?(value)
|
120
|
+
begin
|
121
|
+
!!YAML.load(value)
|
122
|
+
rescue
|
123
|
+
false
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def yamlize_super_serialized_attributes
|
128
|
+
self.class.const_get('SUPER_SERIALIZED_ATTRIBUTES').each do |attr_name|
|
129
|
+
next unless send(attr_name)
|
130
|
+
self.send("#{attr_name}=", send(attr_name).to_yaml)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
def super_serialized_attr_as_string_or_yaml(attr_name)
|
135
|
+
if read_attribute(attr_name).is_a?(String)
|
136
|
+
read_attribute(attr_name)
|
137
|
+
else
|
138
|
+
send(attr_name).to_yaml
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
def check_if_super_serialized_attributes_are_valid_yaml
|
143
|
+
self.class.const_get('SUPER_SERIALIZED_ATTRIBUTES').each do |attr_name|
|
144
|
+
next unless send(attr_name)
|
145
|
+
unless is_valid_yaml?(super_serialized_attr_as_string_or_yaml(attr_name))
|
146
|
+
errors.add(attr_name, 'could not be properly serialized. Typical serializations are numbers, strings, arrays, or hashes.')
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
ActiveRecord::Base.send :include, SuperSerialize
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: super_serialize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ricardo Quiñones
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.2.18
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.2.18
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sqlite3
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec-rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: A super, simple way to serialize anything from fixnums and floats to
|
56
|
+
arrays and hashes.
|
57
|
+
email:
|
58
|
+
- r.a.quinones@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- MIT-LICENSE
|
64
|
+
- README.md
|
65
|
+
- Rakefile
|
66
|
+
- lib/super_serialize.rb
|
67
|
+
- lib/super_serialize/version.rb
|
68
|
+
- lib/tasks/super_serialize_tasks.rake
|
69
|
+
homepage: ''
|
70
|
+
licenses: []
|
71
|
+
metadata: {}
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options: []
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
requirements: []
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 2.2.2
|
89
|
+
signing_key:
|
90
|
+
specification_version: 4
|
91
|
+
summary: Allows for dynamically serializing fixnums, floats, arrays, hashes, and,
|
92
|
+
of course, strings.
|
93
|
+
test_files: []
|