simple_object_serializer 0.1.1 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1fcee44d17c35dc6d640675528deb7d27443596c6a0a988590a018445a3848e9
4
- data.tar.gz: 99829ab8919be045ab031e0d647d689d47de492d227dc2a132eafc5b7e1a9db4
3
+ metadata.gz: 15922d9be301e15c4fb01d960126ca5fa107b598a1850c812de306fdfd27a490
4
+ data.tar.gz: 37f398f4813c09317b4a6191096642849b8f4418dd33f16f4f91f53e51233bb9
5
5
  SHA512:
6
- metadata.gz: 1261cac7f3278c9fbb035888255e42671eff84cc985d452814a09682f2cd16e425caedd58b73c521019e33c6ed132ce99a8d8b37fce91e7e39d48c2eafef013e
7
- data.tar.gz: c7c1619d7741d5141c2bbf671f37c0ea4ba7c06e4e8ecd6948b9ce14d6813fe603885d086c69754d29345abf30bddff6256504ab32a81871ec2b91755c844cd5
6
+ metadata.gz: ea26178b168e341e650d5041515d9775052f0fcced4c25a8c440c945b1ff784b4db021389e9de79d9b84600e9f701a02b3e136bd9e1ac91ca213da9a6acd7e91
7
+ data.tar.gz: 3b5b8601174a60cad0fd1d66c32b89c35b6465ace74f11b73579a783f9ce9d116a85c5d1dabd320fefbcac0e9b5e36b15417f3cf23001fa157e908d695216ffc
data/README.MD ADDED
@@ -0,0 +1,153 @@
1
+ # Simple Object Serializer
2
+
3
+ This gem transforms a sequence of objects into one by selecting only the desired properties of them
4
+
5
+ ## Instalation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'simple_object_serializer', git: 'https://github.com/almirpask/simple_object_serializer', branch: 'master'
11
+ ```
12
+ Then run the `bunlde` command
13
+
14
+ ## Usage
15
+ For now this gem only provides to you a view helper which you can use to merge your objects eg:
16
+ ```ruby
17
+ @user = User.find(2)
18
+ @product = Product.first
19
+ my_car = {
20
+ brand: 'Ford',
21
+ model: 'Mustang'
22
+ fabrication_yar: '2010'
23
+ }
24
+ serialize_objects({user: @user, product: @product, my_car: my_car}, {user: [:name], product: [:name, :price], my_car: [:brand, :model]})
25
+
26
+ # =>
27
+ {
28
+ user: {
29
+ name: 'Nick Fury'
30
+ },
31
+ product: {
32
+ name: 'Chair',
33
+ price: '40.00'
34
+ },
35
+ my_car: {
36
+ brand: 'Ford',
37
+ model: 'Mustang'
38
+ }
39
+ }
40
+ ```
41
+
42
+ ### Passing full objects
43
+ To pass full objecets you need to pass a object parameter empty like this:
44
+
45
+ ```ruby
46
+ @user = User.find(2)
47
+ @product = Product.first
48
+ my_car = {
49
+ brand: 'Ford',
50
+ model: 'Mustang'
51
+ fabrication_yar: '2010'
52
+ }
53
+ serialize_objects({user: @user, product: @product, my_car: my_car}, {user: [:name], product: [:name, :price], my_car: []})
54
+
55
+ # =>
56
+ {
57
+ user: {
58
+ name: 'Nick Fury'
59
+ },
60
+ product: {
61
+ name: 'Chair',
62
+ price: '40.00'
63
+ },
64
+ my_car: {
65
+ brand: 'Ford',
66
+ model: 'Mustang'
67
+ fabrication_yar: '2010'
68
+ }
69
+ }
70
+ ```
71
+
72
+ ### Passing a simple array
73
+
74
+ ```ruby
75
+ @user = User.find(2)
76
+ @product = Product.first
77
+ animals = ['dog', 'cat', 'dolphin']
78
+ serialize_objects({user: @user, product: @product, animals: animals}, {user: [:name], product: [:name, :material], animals: []})
79
+
80
+ # =>
81
+ {
82
+ user: {
83
+ name: 'Nick Fury'
84
+ },
85
+ product: {
86
+ name: 'Chair',
87
+ price: '40.00'
88
+ },
89
+ animals: ['dog', 'cat', 'dolphin']
90
+ }
91
+ ```
92
+
93
+ ### Passing a array of objects
94
+
95
+ ```ruby
96
+ users = User.all
97
+
98
+ serialize_objects({users: users}, {users: [:name]})
99
+
100
+ # =>
101
+ {
102
+ users: [
103
+ {
104
+ name: 'Tonny Stark'
105
+ },
106
+ {
107
+ name: 'Nick Fury'
108
+ },
109
+ {
110
+ name: 'Steve Rogers'
111
+ }
112
+ ],
113
+ }
114
+
115
+ serialize_objects({users: users}, {users: []})
116
+
117
+
118
+ # =>
119
+ {
120
+ users: [
121
+
122
+ {
123
+ id: 1
124
+ name: 'Tonny Stark',
125
+ email: 'tonny.stark@avengers.com'
126
+ },
127
+ {
128
+ id: 2
129
+ name: 'Nick Fury',
130
+ email: 'nick.fury@avengers.com'
131
+ },
132
+ {
133
+ id: 3
134
+ name: 'Steve Rogers',
135
+ email: 'steve.rogers@avengers.com'
136
+ }
137
+ ],
138
+ }
139
+ ```
140
+
141
+
142
+ ## TODO
143
+ 1. Make this gem run with Active Record relationships
144
+ 2. Extend the functionalities to controllers
145
+ 3. Allow to pass parameters without having to specify the properties (if exist a parameter without the property specification just show this entire object on results)
146
+
147
+ ## Contributing
148
+
149
+ Bug reports and pull requests are welcome on GitHub at https://github.com/almirpask/simple_object_serializer. 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.
150
+
151
+ ## License
152
+
153
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,11 @@
1
+ require 'rails'
2
+
3
+ module SimpleObjectSerializer
4
+ class Railtie < ::Rails::Railtie
5
+ initializer 'simple_object_serializer.setup_view_helpers', after: :load_config_initializers, group: :all do
6
+ ActiveSupport.on_load(:action_view) do
7
+ include ::SimpleObjectSerializer::ViewHelper
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,30 @@
1
+ module SimpleObjectSerializer
2
+ module ViewHelper
3
+ def serialize_objects(objects, parameters)
4
+ new_object = {}
5
+ parameters.each do |parameter_index, parameter|
6
+ if parameter.empty?
7
+ new_object[parameter_index] = objects[parameter_index]
8
+ else
9
+ if objects[parameter_index].kind_of?(Array)
10
+ new_object[parameter_index] = []
11
+ objects[parameter_index].each do |object|
12
+ array_object = {}
13
+ parameter.each do |key|
14
+ array_object[parameter_index] = {} if array_object[parameter_index].nil?
15
+ array_object[parameter_index][key] = object[key]
16
+ end
17
+ new_object[parameter_index] << array_object[parameter_index]
18
+ end
19
+ else
20
+ parameter.each do |key|
21
+ new_object[parameter_index] = {} if new_object[parameter_index].nil?
22
+ new_object[parameter_index][key] = objects[parameter_index][key]
23
+ end
24
+ end
25
+ end
26
+ end
27
+ new_object
28
+ end
29
+ end
30
+ end
@@ -1,2 +1,2 @@
1
- require 'simple_object_serializer/railtie'
2
1
  require 'simple_object_serializer/view_helper'
2
+ require 'simple_object_serializer/railtie'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_object_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - almirpask
@@ -30,7 +30,10 @@ executables: []
30
30
  extensions: []
31
31
  extra_rdoc_files: []
32
32
  files:
33
+ - README.MD
33
34
  - lib/simple_object_serializer.rb
35
+ - lib/simple_object_serializer/railtie.rb
36
+ - lib/simple_object_serializer/view_helper.rb
34
37
  homepage: https://github.com/almirpask/simple_object_serializer
35
38
  licenses:
36
39
  - MIT