pg_serializable 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9ef02b6457601f2d201dc81241644ceeda7914df
4
- data.tar.gz: a19b84c078527d28c20e387665efac2830f41c5c
3
+ metadata.gz: 618fc576fe1ec96d02b3b6745406aaa0921297a3
4
+ data.tar.gz: 10fcb8a2866501c48f4126cbbb907f76c64c3b26
5
5
  SHA512:
6
- metadata.gz: e0093725bd655c8f1e9f96f4e3576d639925e52af60402c11d549147b0bfe999a9d2453fb30ecab65300669e3fdaf6b0b60ad20f8f57626cbb8e506b2e951d6a
7
- data.tar.gz: 952b1b05a28ce3a1706d8ec7f2f201d6e2d24d025913d75687824279f3c58867e55a7d70c6ca6b402a8d1d50f26d39da01101f1180549ed7827cb854664c3564
6
+ metadata.gz: c252b339e93bb7e0f544e3bdc80639086a6c2378df48a1d6c7fbf8646c45774048bd0aae5f6177f9485fd7235c7bdedb3ee9c6cd785903b1d044960188ade606
7
+ data.tar.gz: 0f4d3bd51fcb1255f54cd206477eba8eeeeaddc1bc33194fb5538406233a291d499990f5854f2833fa7599eeff5fe6e694c52c706701c6ec73f39d388bb433d2
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # PgSerializable
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/pg_serializable`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ This is experimental.
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ Serialize json directly from postgres (9.4+).
6
6
 
7
7
  ## Installation
8
8
 
@@ -22,17 +22,251 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- TODO: Write usage instructions here
25
+ In your model:
26
+ ```ruby
27
+ require 'pg_serializable'
26
28
 
27
- ## Development
29
+ class Product < ApplicationRecord
30
+ include PgSerializable
31
+
32
+ serializable do
33
+ attributes :name, :id
34
+ attribute :name, label: :test_name
35
+ end
36
+ end
37
+ ```
38
+
39
+ You can also include it in your `ApplicationRecord` so all models will be serializable.
40
+
41
+ In your controller:
42
+ ```ruby
43
+ render json: Product.limit(200).order(updated_at: :desc).json
44
+ ```
45
+
46
+ It works with single records:
47
+ ```ruby
48
+ render json: Product.find(10).json
49
+ ```
50
+
51
+ ### Attributes
52
+ List attributes:
53
+ ```ruby
54
+ attributes :name, :id
55
+ ```
56
+ results in:
57
+ ```json
58
+ [
59
+ {
60
+ "id": 503,
61
+ "name": "Direct Viewer"
62
+ },
63
+ {
64
+ "id": 502,
65
+ "name": "Side Disc Bracket"
66
+ }
67
+ ]
68
+ ```
69
+ Re-label individual attributes:
70
+ ```ruby
71
+ attributes :id
72
+ attribute :name, label: :different_name
73
+ ```
74
+ ```json
75
+ [
76
+ {
77
+ "id": 503,
78
+ "different_name": "Direct Viewer"
79
+ },
80
+ {
81
+ "id": 502,
82
+ "different_name": "Side Disc Bracket"
83
+ }
84
+ ]
85
+ ```
86
+
87
+ Wrap attributes in custom sql
88
+ ```ruby
89
+ serializable do
90
+ attributes :id
91
+ attribute :active, label: :deleted { |v| "NOT #{v}" }
92
+ end
93
+ ```
94
+ ```json
95
+ [
96
+ {
97
+ "id": 503,
98
+ "deleted": true
99
+ },
100
+ {
101
+ "id": 502,
102
+ "deleted": true
103
+ }
104
+ ]
105
+ ```
106
+
107
+
108
+ ### Associations
109
+ Supported associations:
110
+ - `belongs_to`
111
+ - `has_many`
112
+ - `has_many :through`
113
+ - `has_one`
28
114
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
115
+ #### belongs_to
116
+ ```ruby
117
+ serializable do
118
+ attributes :id, :name
119
+ belongs_to: :label
120
+ end
121
+ ```
122
+ ```json
123
+ [
124
+ {
125
+ "id": 503,
126
+ "label": {
127
+ "name": "Piper",
128
+ "id": 106
129
+ }
130
+ },
131
+ {
132
+ "id": 502,
133
+ "label": {
134
+ "name": "Sebrina",
135
+ "id": 77
136
+ }
137
+ }
138
+ ]
139
+ ```
140
+
141
+ #### has_many
142
+ Works for nested relationships
143
+ ```ruby
144
+ class Product < ApplicationRecord
145
+ serializable do
146
+ attributes :id, :name
147
+ has_many: :variations
148
+ end
149
+ end
150
+
151
+ class Variation < ApplicationRecord
152
+ serializable do
153
+ attributes :id, :hex
154
+ belongs_to: :color
155
+ end
156
+ end
157
+
158
+ class Color < ApplicationRecord
159
+ serializable do
160
+ attributes :id, :hex
161
+ end
162
+ end
163
+ ```
164
+ ```json
165
+ [
166
+ {
167
+ "id": 503,
168
+ "variations": [
169
+ {
170
+ "name": "Cormier",
171
+ "id": 2272,
172
+ "color": {
173
+ "id": 5,
174
+ "hex": "f4b9c8"
175
+ }
176
+ },
177
+ {
178
+ "name": "Spencer",
179
+ "id": 2271,
180
+ "color": {
181
+ "id": 586,
182
+ "hex": "2e0719"
183
+ }
184
+ }
185
+ ]
186
+ },
187
+ {
188
+ "id": 502,
189
+ "variations": [
190
+ {
191
+ "name": "DuBuque",
192
+ "id": 2270,
193
+ "color": {
194
+ "id": 593,
195
+ "hex": "0b288f"
196
+ }
197
+ },
198
+ {
199
+ "name": "Berge",
200
+ "id": 2269,
201
+ "color": {
202
+ "id": 536,
203
+ "hex": "b2bfee"
204
+ }
205
+ }
206
+ ]
207
+ }
208
+ ]
209
+ ```
210
+
211
+ #### has_many :through
212
+ ```ruby
213
+ class Product < ApplicationRecord
214
+ has_many :categories_products
215
+ has_many :categories, through: :categories_products
216
+
217
+ serializable do
218
+ attributes :id
219
+ has_many :categories
220
+ end
221
+ end
222
+
223
+ class Category < ApplicationRecord
224
+ serializable do
225
+ attributes :name, :id
226
+ end
227
+ end
228
+ ```
229
+
230
+ ```json
231
+ [
232
+ {
233
+ "id": 503,
234
+ "categories": [
235
+ {
236
+ "name": "Juliann",
237
+ "id": 13
238
+ },
239
+ {
240
+ "name": "Teressa",
241
+ "id": 176
242
+ },
243
+ {
244
+ "name": "Garret",
245
+ "id": 294
246
+ }
247
+ ]
248
+ },
249
+ {
250
+ "id": 502,
251
+ "categories": [
252
+ {
253
+ "name": "Rossana",
254
+ "id": 254
255
+ }
256
+ ]
257
+ }
258
+ ]
259
+ ```
260
+ #### has_one
261
+ TODO: write examples
262
+
263
+ ## Development
30
264
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
265
+ TODO
32
266
 
33
267
  ## Contributing
34
268
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/pg_serializable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
269
+ Bug reports and pull requests are welcome on GitHub at https://github.com/matthewjf/pg_serializable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
270
 
37
271
  ## License
38
272
 
@@ -1,13 +1,24 @@
1
1
  module PgSerializable
2
2
  module Nodes
3
3
  class Attribute < Base
4
- def initialize(column_name, label: nil)
4
+ def initialize(column_name, label: nil, &prc)
5
5
  @column_name = column_name
6
6
  @label = label || column_name
7
+ @prc = prc if block_given?
7
8
  end
8
9
 
9
10
  def to_sql(table_alias=nil)
10
- ["\'#{@label}\'", "#{table_alias ? table_alias + '.' : ''}#{@column_name}"].join(',')
11
+ [key, value(table_alias)].join(',')
12
+ end
13
+
14
+ private
15
+ def key
16
+ "\'#{@label}\'"
17
+ end
18
+
19
+ def value(tbl)
20
+ val = "#{tbl && "#{tbl}."}#{@column_name}"
21
+ @prc ? @prc.call(val) : val
11
22
  end
12
23
  end
13
24
  end
@@ -13,8 +13,8 @@ module PgSerializable
13
13
  end
14
14
  end
15
15
 
16
- def attribute(column_name, label: nil)
17
- @attributes << Nodes::Attribute.new(column_name, label: label) if column_exists?(column_name)
16
+ def attribute(column_name, label: nil, &blk)
17
+ @attributes << Nodes::Attribute.new(column_name, label: label, &blk) if column_exists?(column_name)
18
18
  end
19
19
 
20
20
  def has_many(association, label: nil)
@@ -1,3 +1,3 @@
1
1
  module PgSerializable
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_serializable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - matthewjf