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 ADDED
@@ -0,0 +1,3 @@
1
+ .DS_Store
2
+ test/tmp
3
+ *.gem
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,3 @@
1
+ require 'active_record_hstore_serializer/hash'
2
+ require 'active_record_hstore_serializer/string'
3
+ require 'active_record_hstore_serializer/hstore_serializer'
@@ -0,0 +1,13 @@
1
+ class Hash
2
+
3
+ def to_hstore
4
+ return "" if empty?
5
+ map {|idx, val| %Q{"#{idx.to_s}"=>"#{val.to_s}"} }.join(', ')
6
+ end
7
+
8
+ # If the method from_hstore is called in a Hash, it just returns self.
9
+ def from_hstore
10
+ self
11
+ end
12
+
13
+ end
@@ -0,0 +1,15 @@
1
+ class HstoreSerializer
2
+
3
+ def self.load(text)
4
+ return unless text
5
+ text.from_hstore
6
+ end
7
+
8
+ def self.dump(text)
9
+ text.to_hstore
10
+ end
11
+ end
12
+
13
+
14
+
15
+
@@ -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.1
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: