active_serialize 2.0.0 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +78 -37
- data/lib/active_serialize/class_methods.rb +2 -2
- data/lib/active_serialize/version.rb +1 -1
- data/lib/active_serialize.rb +3 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 100d3814b35d079e9ddd05300ea69aeb01d13ddd
|
4
|
+
data.tar.gz: 8432a699262798dc61effbe9f5994fe63d9f8829
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0bc27fb205558158804e0e264518342aed2183858abf3515c74cf67c0e5e6bb073fbb27bdd6c44ee19ee4143c5d89f75402a15d3475b6cccf44a56918b092e30
|
7
|
+
data.tar.gz: d76c8deca5278e237bd88f3b1abfe91c5a9956ba226c9673d976ba4fe5b7cc24a9d4ab8727c057c927ee673dd609160c06ab77f1e03e0a90d7f73936a0c245de
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# ActiveSerialize
|
2
2
|
|
3
|
-
Provide a very simple way to transform ActiveRecord data into Hash
|
3
|
+
Provide a very simple way to transform ActiveRecord data into Hash.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -18,60 +18,101 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
$ gem install active_serialize
|
20
20
|
|
21
|
-
## Usage
|
21
|
+
## Basic Usage
|
22
22
|
|
23
|
-
|
23
|
+
There is a table `users`:
|
24
24
|
```ruby
|
25
|
-
t.string :name
|
26
|
-
t.string :password_digest, null: false
|
25
|
+
t.string :name
|
27
26
|
t.string :email
|
28
27
|
```
|
29
28
|
|
30
|
-
|
29
|
+
And a table `books`:
|
30
|
+
|
31
31
|
```ruby
|
32
|
-
|
33
|
-
|
34
|
-
active_serialize rmv: %i[ password_digest ]
|
35
|
-
end
|
32
|
+
t.bigint :user_id
|
33
|
+
t.string :name
|
36
34
|
```
|
37
35
|
|
38
|
-
|
36
|
+
Declaration in model:
|
37
|
+
```ruby
|
38
|
+
class User < ActiveRecord::Base
|
39
|
+
active_serialize
|
40
|
+
has_many :books
|
41
|
+
|
42
|
+
def love
|
43
|
+
'Ruby'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
class Book < ActiveRecord::Base
|
48
|
+
active_serialize rmv: :user_id
|
49
|
+
belongs_to :user
|
50
|
+
end
|
39
51
|
```
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
52
|
+
|
53
|
+
Then:
|
54
|
+
```ruby
|
55
|
+
User.last.to_h
|
56
|
+
# => { "id" => 2, "name" => "ikkiuchi", "email" => "xxxx" }
|
57
|
+
|
58
|
+
User.where(id: [1, 2]).to_ha # means "to hash array"
|
59
|
+
# => [
|
60
|
+
# { "id" => 1, "name" => "zhandao", "email" => "xxxx" },
|
61
|
+
# { "id" => 2, "name" => "ikkiuchi", "email" => "xxxx" }
|
62
|
+
# ]
|
47
63
|
```
|
48
64
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
The following example will generate `{ ..., "addtion_field" => "value' }`
|
55
|
-
```ruby
|
56
|
-
class User
|
57
|
-
active_serialize add: :addition_field
|
58
|
-
|
59
|
-
def addition_field
|
60
|
-
'value'
|
61
|
-
end
|
62
|
-
end
|
63
|
-
```
|
65
|
+
The basic usage just looks like `attributes` method.
|
66
|
+
|
67
|
+
## How is it work?
|
68
|
+
|
69
|
+
ActiveRecord class method `column_names` (which is called by this gem) shows that the filed names by loading database schema.
|
64
70
|
|
65
71
|
## Advanced Usage
|
66
72
|
|
67
|
-
|
73
|
+
### Except (remove) keys
|
74
|
+
|
75
|
+
1. remove by default: `active_serialize rmv: [:email]` (you can also use `active_serialize_rmv`)
|
76
|
+
2. remove when calling `to_h`: `to_h(rmv: [:email])`
|
77
|
+
|
78
|
+
`=> { "id" => 2, "name" => "ikkiuchi" }`
|
68
79
|
|
80
|
+
### Add keys
|
81
|
+
|
82
|
+
1. add it by default: `active_serialize add: [:love]` (you can also use `active_serialize_add`)
|
83
|
+
2. add when calling `to_h`: `to_h(add: [:love])`
|
84
|
+
|
85
|
+
`=> { "id" => 2, "name" => "ikkiuchi", "email" => "xxxx", "love" => "Ruby" }`
|
86
|
+
|
87
|
+
* Values of addition keys will be the result of calling `public_send`
|
88
|
+
|
89
|
+
### Add recursive attributes
|
90
|
+
|
91
|
+
* recursive? —— calls `to_h` recursively (/ nested)
|
92
|
+
|
93
|
+
See below:
|
69
94
|
```ruby
|
70
|
-
|
71
|
-
|
72
|
-
|
95
|
+
User.first.books.to_ha
|
96
|
+
# => [{ "name" => "Rails Guide" }]
|
97
|
+
|
98
|
+
# declaration in User
|
99
|
+
active_serialize_add :books, recursive: true
|
100
|
+
# `active_serialize recursive: :books` is OK, but notice `active_serialize` should only be called once.
|
101
|
+
|
102
|
+
# then ...
|
103
|
+
User.first.to_h
|
104
|
+
# => { "id" => 2, "name" => "ikkiuchi", "email" => "xxxx", "books" => [{ "name" => "Rails Guide" }] }
|
73
105
|
```
|
74
106
|
|
107
|
+
### Transform key names
|
108
|
+
|
109
|
+
Choose one of the following ways:
|
110
|
+
|
111
|
+
1. `active_serialize_map love: :looove`
|
112
|
+
2. `active_serialize_add :love, named: :looove`
|
113
|
+
|
114
|
+
`=> { "id" => 2, "name" => "ikkiuchi", "email" => "xxxx", "looove" => "Ruby" }`
|
115
|
+
|
75
116
|
## Development
|
76
117
|
|
77
118
|
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.
|
@@ -19,13 +19,13 @@ module ActiveSerialize
|
|
19
19
|
_active_serialize[recursive ? :recursive : :add].concat attrs.map(&:to_sym)
|
20
20
|
end
|
21
21
|
|
22
|
-
def active_serialize_map settings
|
22
|
+
def active_serialize_map **settings
|
23
23
|
_active_serialize[:map].merge! settings
|
24
24
|
end
|
25
25
|
|
26
26
|
def active_serialize_keys(rmv: [ ], add: [ ])
|
27
27
|
_active_serialize[:final] ||= column_names.map(&:to_sym) - _active_serialize[:rmv] + _active_serialize[:add]
|
28
|
-
_active_serialize[:final] - rmv + add
|
28
|
+
_active_serialize[:final] - Array(rmv) + Array(add)
|
29
29
|
end
|
30
30
|
end
|
31
31
|
end
|
data/lib/active_serialize.rb
CHANGED
@@ -15,9 +15,9 @@ module ActiveSerialize
|
|
15
15
|
include ToH
|
16
16
|
delegate :active_serialize_keys, :_active_serialize, to: self
|
17
17
|
|
18
|
-
active_serialize_rmv *rmv
|
19
|
-
active_serialize_add *add
|
20
|
-
active_serialize_add *recursive, recursive: true
|
18
|
+
active_serialize_rmv *Array(rmv)
|
19
|
+
active_serialize_add *Array(add)
|
20
|
+
active_serialize_add *Array(recursive), recursive: true
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|