hstore_attribute_support 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/{README → README.md} +22 -12
- data/hstore_attribute_support.gemspec +2 -2
- data/lib/hstore_attribute_support/base.rb +17 -12
- metadata +5 -5
data/{README → README.md}
RENAMED
@@ -1,21 +1,31 @@
|
|
1
|
-
|
1
|
+
hstore_attribute_support
|
2
|
+
========================
|
2
3
|
|
3
|
-
|
4
|
-
|
4
|
+
Enables AR models to set up virtual attributes stored in hstore columns.
|
5
|
+
The main problem with a naked hstore solution in rails is, that defaults
|
6
|
+
are not supported (no schema.rb that tells rails, what the default is) and the
|
7
|
+
types get lost when loading the serialized hstore data into a hash.
|
8
|
+
With hstore attribute support, you can wire up your models in a rails like
|
9
|
+
fashion very easily.
|
5
10
|
|
11
|
+
### What it can do for you
|
6
12
|
Consider a Person class with two hstore columns named "work_details" and
|
7
13
|
"address". This class will, upon activation of the hstore attribute support, get
|
8
|
-
new class methods: work_details_hstore_accessor and address_hstore_accessor.
|
14
|
+
two new class methods: work_details_hstore_accessor and address_hstore_accessor.
|
15
|
+
Activating the support is done by just inserting the method call
|
16
|
+
"has_hstore_columns" into your AR class.
|
9
17
|
|
10
|
-
These methods can be used on class level
|
11
|
-
|
12
|
-
|
13
|
-
Because of that, new instances will be able to use an "after_initialize"
|
14
|
-
callback to set up their hstore'd attributes to the default value which was
|
15
|
-
provided, as well as enabling the getter methods to return values typecasted
|
16
|
-
(hstore data looses its type and was returned as a string otherwise...)
|
18
|
+
These methods can now be used (on AR class level, just like attr_accessor) to
|
19
|
+
set up getter/setter methods for virtual attributes that are stored in the
|
20
|
+
hstore columns.
|
17
21
|
|
18
|
-
|
22
|
+
Additionally you are able to give a type hint and default values along.
|
23
|
+
Because of that, new instances will be able (using an "after_initialize"
|
24
|
+
callback) to set up their hstore'd attributes to the default value you provided.
|
25
|
+
On top of this, the model obtains getter methods to return values typecasted
|
26
|
+
(hstore data looses its type and was returned as a string otherwise...).
|
27
|
+
|
28
|
+
### Example
|
19
29
|
|
20
30
|
# Schema for User:
|
21
31
|
# - id: int
|
@@ -1,7 +1,7 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'hstore_attribute_support'
|
3
|
-
s.version = '0.0.
|
4
|
-
s.date = '2011-11-
|
3
|
+
s.version = '0.0.4'
|
4
|
+
s.date = '2011-11-16'
|
5
5
|
s.summary = "Adds AR Attributes support for Postgres hstore columns "
|
6
6
|
s.description = "Adds AR Attributes support for Postgres hstore columns "
|
7
7
|
s.authors = ["Andreas Schwarzkopf"]
|
@@ -19,7 +19,7 @@ module HstoreAttributeSupport
|
|
19
19
|
# be used to set up this models hstore'd attributes more rails like.
|
20
20
|
# A Person class with a data hstore column could setup its name like this:
|
21
21
|
#
|
22
|
-
# data_hstore_accessor :name, :string, ''
|
22
|
+
# data_hstore_accessor :name, :cast => :string, :default => ''
|
23
23
|
#
|
24
24
|
# Which will set up accessors to the virtual attribute "name", defaults to
|
25
25
|
# an empty string and is stored in the data column.
|
@@ -28,8 +28,8 @@ module HstoreAttributeSupport
|
|
28
28
|
self.columns.each do |column|
|
29
29
|
next unless column.type == :hstore
|
30
30
|
class_eval %Q{
|
31
|
-
def self.#{column.name}_hstore_accessor(attribute_name,
|
32
|
-
return hstore_attr_accessor(attribute_name, "#{column.name}",
|
31
|
+
def self.#{column.name}_hstore_accessor(attribute_name, options = {})
|
32
|
+
return hstore_attr_accessor(attribute_name, "#{column.name}", options)
|
33
33
|
end
|
34
34
|
}
|
35
35
|
end
|
@@ -38,7 +38,7 @@ module HstoreAttributeSupport
|
|
38
38
|
# instance to set up default values for its hstore'd attributes.
|
39
39
|
after_initialize do
|
40
40
|
# iterate over the class instance variable "hstore_attributes", which
|
41
|
-
# contains
|
41
|
+
# contains casts & defaults for virtual hstore attributes and apply them
|
42
42
|
(self.class.hstore_attributes || {}).each do |attr_name, settings|
|
43
43
|
column = settings[:column]
|
44
44
|
default = settings[:default]
|
@@ -57,13 +57,18 @@ module HstoreAttributeSupport
|
|
57
57
|
# is lost by the hstore and was a pure string otherwise
|
58
58
|
# 2. it provides an easy way to set up defaults, as rails AR mechanism
|
59
59
|
# fail here (no database defaults available for virtual attributes)
|
60
|
-
def hstore_attr_accessor(attribute_name, hstore_column,
|
60
|
+
def hstore_attr_accessor(attribute_name, hstore_column, options = {})
|
61
|
+
options = {
|
62
|
+
:cast => nil,
|
63
|
+
:default => nil
|
64
|
+
}.merge(options).with_indifferent_access
|
65
|
+
|
61
66
|
self.hstore_attributes ||= {}
|
62
67
|
|
63
68
|
self.hstore_attributes[attribute_name.to_s] = {
|
64
69
|
:column => hstore_column.to_s,
|
65
|
-
:
|
66
|
-
:default => default
|
70
|
+
:cast => options[:cast],
|
71
|
+
:default => options[:default]
|
67
72
|
}
|
68
73
|
|
69
74
|
class_eval %Q{
|
@@ -86,16 +91,16 @@ module HstoreAttributeSupport
|
|
86
91
|
|
87
92
|
def read_hstore_attribute(attribute_name)
|
88
93
|
ha_column = self.class.hstore_attributes[attribute_name][:column]
|
89
|
-
|
94
|
+
ha_cast = self.class.hstore_attributes[attribute_name][:cast]
|
90
95
|
hstore_data = self.send(:"#{ha_column}").try(:with_indifferent_access)
|
91
96
|
|
92
97
|
return nil unless hstore_data
|
93
98
|
|
94
99
|
value = hstore_data[attribute_name]
|
95
100
|
|
96
|
-
return value unless
|
101
|
+
return value unless ha_cast
|
97
102
|
|
98
|
-
case
|
103
|
+
case ha_cast
|
99
104
|
when :integer
|
100
105
|
return value.to_i
|
101
106
|
when :float
|
@@ -115,9 +120,9 @@ module HstoreAttributeSupport
|
|
115
120
|
else
|
116
121
|
end
|
117
122
|
|
118
|
-
return
|
123
|
+
return ha_cast.call(value) if ha_cast.is_a?(Proc)
|
119
124
|
|
120
|
-
raise "read_hstore_attribute failed when trying to cast the type \"#{
|
125
|
+
raise "read_hstore_attribute failed when trying to cast the type \"#{ha_cast.inspect}\". Please define the type as one of [:integer, :float, :decimal, :boolean, :string, :datetime, :date] or pass in a lambda with one parameter to perform custom casts."
|
121
126
|
end
|
122
127
|
|
123
128
|
def write_hstore_attribute(attribute_name, value)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hstore_attribute_support
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-11-
|
12
|
+
date: 2011-11-16 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord-postgres-hstore
|
16
|
-
requirement: &
|
16
|
+
requirement: &70308241492240 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: 0.1.2
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70308241492240
|
25
25
|
description: ! 'Adds AR Attributes support for Postgres hstore columns '
|
26
26
|
email: asmailbox@gmx.de
|
27
27
|
executables: []
|
@@ -29,7 +29,7 @@ extensions: []
|
|
29
29
|
extra_rdoc_files: []
|
30
30
|
files:
|
31
31
|
- .gitignore
|
32
|
-
- README
|
32
|
+
- README.md
|
33
33
|
- hstore_attribute_support.gemspec
|
34
34
|
- lib/hstore_attribute_support.rb
|
35
35
|
- lib/hstore_attribute_support/base.rb
|