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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 69241d017dd21a5caab76128a03a095e2aaad60d
4
- data.tar.gz: d1743e957dbf71127ea025cd2b12e0d24baf2589
3
+ metadata.gz: 100d3814b35d079e9ddd05300ea69aeb01d13ddd
4
+ data.tar.gz: 8432a699262798dc61effbe9f5994fe63d9f8829
5
5
  SHA512:
6
- metadata.gz: 27d0d6b81498985305fa5914d88e2b058c29e3072a9b12b450005847c972cd21e42c72c39b87811dde4bf9cb716baff79378a49f7a84135666f4887007061fd1
7
- data.tar.gz: b83476170f7834133a34d624ce23f880f9e5b6134c08f45227432248f5d8267e0a8a092e32dc0924a69630d5fbfdfbd0437bc05844442b64de289d635a9cad92
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 output.
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
- Suppose the table `users`:
23
+ There is a table `users`:
24
24
  ```ruby
25
- t.string :name, null: false
26
- t.string :password_digest, null: false
25
+ t.string :name
27
26
  t.string :email
28
27
  ```
29
28
 
30
- Model:
29
+ And a table `books`:
30
+
31
31
  ```ruby
32
- class User
33
- # YES, all you have to do is write this line
34
- active_serialize rmv: %i[ password_digest ]
35
- end
32
+ t.bigint :user_id
33
+ t.string :name
36
34
  ```
37
35
 
38
- Console:
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
- $ User.all.to_h # on records (active relation)
41
- => [
42
- { "id" => 1, "name" => "zhandao", "email" => "xxxx" },
43
- { "id" => 2, "name" => "ikkiuchi", "email" => "xxxx" }
44
- ]
45
- $ User.last.to_h # on a record
46
- => { "id" => 2, "name" => "ikkiuchi", "email" => "xxxx" }
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
- Explain:
50
-
51
- 1. Basic principle: the supporter know all the fields name through `column_names` (a db mapping func),
52
- so you just have to declare which fields do not need to output by passing 'rvm' param.
53
- 2. You can also `add` something to the output JSON, but make sure that the field name you add needs to correspond to the model instance methods.
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
- TODO
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
- active_serialize_add :sub_categories_info, when: :get_nested_list
71
- active_serialize_add :base_category_info, name: :base_category, when: -> { base_category.present? }
72
- active_serialize_map a: :b
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveSerialize
4
- VERSION = '2.0.0'
4
+ VERSION = '2.0.1'
5
5
  end
@@ -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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_serialize
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - zhandao