hstore_accessor 0.1.0 → 0.1.2
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 +4 -4
- data/README.md +115 -1
- data/lib/hstore_accessor/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a479c49a994305a373abfb2bef072aa39eadd1d4
|
4
|
+
data.tar.gz: 6a213cac35074d7b5e8e1d3b760c7e3dcdb5c40e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 264c327667ef20d85443754756deb911e5f4e2cd434f98d19c60f12c8394bf4a5ff7a3f55361bc59df701be9b3788670f97710ddc7fb09de11298b158f538483
|
7
|
+
data.tar.gz: e2c11dbf7feaf586543b4bb7a6a746ab99d1fa207a10ad80aa008da787f95916cc0659188e3809e45fca8d0a367942b3f53632a7e190b9070b6d87aaf7277c82
|
data/README.md
CHANGED
@@ -22,17 +22,131 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
## Usage
|
24
24
|
|
25
|
+
### Setup
|
26
|
+
|
27
|
+
The `hstore_accessor` method accepts the name of the hstore column you'd
|
28
|
+
like to use and a hash with keys representing fields and values
|
29
|
+
indicating the type to be stored in that field. The available types
|
30
|
+
are: `string`, `integer`, `float`, `array`, and `hash`.
|
31
|
+
|
25
32
|
```ruby
|
26
33
|
class Product < ActiveRecord::Base
|
27
34
|
|
28
35
|
hstore_accessor :options,
|
29
36
|
color: :string,
|
30
37
|
weight: :integer,
|
31
|
-
|
38
|
+
price: :float,
|
39
|
+
tags: :array,
|
40
|
+
ratings: :hash
|
41
|
+
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
Now you can interact with the fields stored in the hstore directly.
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
p = Product.new
|
49
|
+
p.color = "green"
|
50
|
+
p.weight = 34
|
51
|
+
p.price = 99.95
|
52
|
+
p.tags = ["housewares", "kitchen"]
|
53
|
+
p.ratings = { user_a: 3, user_b: 4 }
|
54
|
+
```
|
55
|
+
|
56
|
+
Reading these fields works as well.
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
p.color #=> "green
|
60
|
+
p.tags #=> ["housewares", "kitchen"]
|
61
|
+
```
|
62
|
+
|
63
|
+
### Scopes
|
64
|
+
|
65
|
+
The `hstore_accessor` macro also creates scopes for `string`, `integer`,
|
66
|
+
`float`, and `array` fields.
|
67
|
+
|
68
|
+
For `string` types, a `with_<key>` scope is created which checks for
|
69
|
+
equality.
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
Product.with_color("green")
|
73
|
+
```
|
74
|
+
|
75
|
+
For `integer` and `float` types five scopes are created:
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
Product.price_lt(240.00) # price less than
|
79
|
+
Product.price_lte(240.00) # price less than or equal to
|
80
|
+
Product.price_eq(240.00) # price equal to
|
81
|
+
Product.price_gte(240.00) # price greater than or equal to
|
82
|
+
Product.price_gt(240.00) # price greater than
|
83
|
+
```
|
84
|
+
|
85
|
+
For `array` types, two scopes are created:
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
Product.tags_eq(["housewares", "kitchen"]) # tags equaling
|
89
|
+
Product.tags_contains("kitchen") # tags containing a single value
|
90
|
+
Product.tags_contains(["housewares", "kitchen"]) # tags containing a number of values
|
91
|
+
```
|
32
92
|
|
93
|
+
### Single-table Inheritance
|
94
|
+
|
95
|
+
One of the big issues with `ActiveRecord` single-table inheritance (STI)
|
96
|
+
is sparse columns. Essentially, as sub-types of the original table
|
97
|
+
diverge further from their parent more columns are left empty in a given
|
98
|
+
table. Postgres' `hstore` type provides part of the solution in that
|
99
|
+
the values in an `hstore` column does not impose a structure - different
|
100
|
+
rows can have different values.
|
101
|
+
|
102
|
+
We set up our table with an hstore field:
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
# db/migration/<timestamp>_create_players_table.rb
|
106
|
+
class CreateVehiclesTable < ActiveRecord::Migration
|
107
|
+
def change
|
108
|
+
create_table :vehicles do |t|
|
109
|
+
t.string :make
|
110
|
+
t.string :model
|
111
|
+
t.integer :model_year
|
112
|
+
t.string :type
|
113
|
+
t.hstore :data
|
114
|
+
end
|
115
|
+
end
|
33
116
|
end
|
34
117
|
```
|
35
118
|
|
119
|
+
And for our models:
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
# app/models/vehicle.rb
|
123
|
+
class Player < ActiveRecord::Base
|
124
|
+
end
|
125
|
+
|
126
|
+
# app/models/vehicles/automobile.rb
|
127
|
+
class Automobile < Vehicle
|
128
|
+
hstore_accessor :data,
|
129
|
+
axle_count: :integer,
|
130
|
+
weight: :float,
|
131
|
+
engine_details: :hash
|
132
|
+
end
|
133
|
+
|
134
|
+
# app/models/vehicles/airplane.rb
|
135
|
+
class Airplane < Vehicle
|
136
|
+
hstore_accessor :data,
|
137
|
+
engine_type: :string,
|
138
|
+
safety_rating: :integer,
|
139
|
+
features: :hash
|
140
|
+
end
|
141
|
+
```
|
142
|
+
|
143
|
+
From here any attributes specific to any sub-class can be stored in the
|
144
|
+
`hstore` column avoiding sparse data. Indices can also be created on
|
145
|
+
individual fields in an `hstore` column.
|
146
|
+
|
147
|
+
This approach was originally concieved by Joe Hirn in [this blog
|
148
|
+
post](http://www.devmynd.com/blog/2013-3-single-table-inheritance-hstore-lovely-combination).
|
149
|
+
|
36
150
|
## Contributing
|
37
151
|
|
38
152
|
1. Fork it
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hstore_accessor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joe Hirn
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-06-
|
13
|
+
date: 2013-06-10 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: pg
|