active_record_hstore_serializer 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/LICENSE +22 -0
- data/active_record_hstore_serializer.gemspec +23 -0
- data/lib/active_record_hstore_serializer.rb +3 -0
- data/lib/active_record_hstore_serializer/hash.rb +13 -0
- data/lib/active_record_hstore_serializer/hstore_serializer.rb +15 -0
- data/lib/active_record_hstore_serializer/string.rb +35 -0
- metadata +8 -1
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
(The MIT License)
|
2
|
+
|
3
|
+
Copyright (c) 2011 Cody Caughlan.
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
'Software'), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
19
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
20
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
21
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
22
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.[
|
@@ -0,0 +1,23 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'active_record_hstore_serializer'
|
3
|
+
s.version = '0.0.2'
|
4
|
+
s.date = '2011-11-19'
|
5
|
+
s.summary = "Serialize attributes using HStore in ActiveRecord / Rails 3.1+"
|
6
|
+
s.description = "Leverage Postgres HStore for awesome attribute serialization."
|
7
|
+
s.authors = ["Cody Caughlan"]
|
8
|
+
s.email = 'toolbag@gmail.com'
|
9
|
+
s.files = ["lib/active_record_hstore_serializer.rb"]
|
10
|
+
s.homepage = 'https://github.com/ruckus/active_record_hstore_serializer'
|
11
|
+
s.files = [
|
12
|
+
".gitignore",
|
13
|
+
"LICENSE",
|
14
|
+
"README",
|
15
|
+
"lib/active_record_hstore_serializer.rb",
|
16
|
+
"lib/active_record_hstore_serializer/hash.rb",
|
17
|
+
"lib/active_record_hstore_serializer/string.rb",
|
18
|
+
"lib/active_record_hstore_serializer/hstore_serializer.rb",
|
19
|
+
"active_record_hstore_serializer.gemspec"
|
20
|
+
]
|
21
|
+
s.require_paths = ['lib']
|
22
|
+
s.has_rdoc = false
|
23
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
class String
|
2
|
+
|
3
|
+
# If the value os a column is already a String and it calls to_hstore, it
|
4
|
+
# just returns self. Validation occurs afterwards.
|
5
|
+
def to_hstore
|
6
|
+
self
|
7
|
+
end
|
8
|
+
|
9
|
+
# Validates the hstore format. Valid formats are:
|
10
|
+
# * An empty string
|
11
|
+
# * A string like %("foo"=>"bar"). I'll call it a "double quoted hstore format".
|
12
|
+
# * A string like %('foo'=>'bar'). I'll call it a "single quoted hstore format".
|
13
|
+
def valid_hstore?
|
14
|
+
return true if empty? || self == "''"
|
15
|
+
# This is what comes from the database
|
16
|
+
dbl_quotes_re = /"([^"]+)"=>"([^"]+)"/
|
17
|
+
# TODO
|
18
|
+
# This is what comes from the plugin
|
19
|
+
# this is a big problem, 'cause regexes does not know how to count...
|
20
|
+
# how should i very values quoted with two single quotes? using .+ sux.
|
21
|
+
sngl_quotes_re = /'(.+)'=>'(.+)'/
|
22
|
+
self.match(dbl_quotes_re) || self.match(sngl_quotes_re)
|
23
|
+
end
|
24
|
+
|
25
|
+
# Creates a hash from a valid double quoted hstore format, 'cause this is the format
|
26
|
+
# that postgresql spits out.
|
27
|
+
def from_hstore
|
28
|
+
Hash[ scan(/"([^"]+)"=>"([^"]+)"/) ]
|
29
|
+
end
|
30
|
+
|
31
|
+
def escape_quotes
|
32
|
+
self.gsub(/'/,"''")
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record_hstore_serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -17,7 +17,14 @@ executables: []
|
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
|
+
- .gitignore
|
21
|
+
- LICENSE
|
20
22
|
- README
|
23
|
+
- lib/active_record_hstore_serializer.rb
|
24
|
+
- lib/active_record_hstore_serializer/hash.rb
|
25
|
+
- lib/active_record_hstore_serializer/string.rb
|
26
|
+
- lib/active_record_hstore_serializer/hstore_serializer.rb
|
27
|
+
- active_record_hstore_serializer.gemspec
|
21
28
|
homepage: https://github.com/ruckus/active_record_hstore_serializer
|
22
29
|
licenses: []
|
23
30
|
post_install_message:
|