lp-serializable 0.2.2 → 0.2.3

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
  SHA256:
3
- metadata.gz: ebe7636f819ea8ef0a8d767c433ea4cf31c4d318faa565805c6fd2cde0eb2935
4
- data.tar.gz: a5a79b4a4ecf642bf42dab71f1bb02f776e0a588c4056589509c4a5967846cd0
3
+ metadata.gz: 89210875f4b075f10a9f32cd2aa6f8835338e6870b474c3e6ac43cb07ef9e032
4
+ data.tar.gz: dbc7937ef61666166931ca2897feb58c257a194bf216eb602303c32fa6a9fb5c
5
5
  SHA512:
6
- metadata.gz: 28f964bd06efa651464ce7b8de98583669a27cb2d5e1f26568f5e037e0dbe60b3a0fdac57493b825ad3a0d0a27b14cf86fc39fd734c0c73c227b88fcb616dfb9
7
- data.tar.gz: d8efaa562ce377fc1ca186307dbb254c3ee00e1426858fd64bdbadf61e51daab5f19ea76aaff1388a97e58a84483f4b5ad536caeacc48956736835e12c250a3b
6
+ metadata.gz: bc82ab5cafd065632241e73a015c0e12f01753a201426e5929f10f6f726e6ccc5dad8c3c90523f3cf7a0dcdab6f0353ff8c1f0f66786922cf8196fa7a16cc61a
7
+ data.tar.gz: edba6bfea1317854dfa1c49ab205fb0296762cfd711276abfb03a89b9d69f81a1858d37369f59c5d840b8b58b212f49710d5a9ca3a354ffa62869dc4ccc4291f
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lp-serializable (0.2.2)
4
+ lp-serializable (0.2.3)
5
5
  activesupport (>= 4.2)
6
6
  fast_jsonapi (>= 1.3)
7
7
 
@@ -52,7 +52,7 @@ GEM
52
52
  crass (1.0.4)
53
53
  diff-lcs (1.3)
54
54
  erubi (1.8.0)
55
- fast_jsonapi (1.3)
55
+ fast_jsonapi (1.5)
56
56
  activesupport (>= 4.2)
57
57
  i18n (1.6.0)
58
58
  concurrent-ruby (~> 1.0)
data/README.md CHANGED
@@ -41,7 +41,7 @@ class MoviesController < ApplicationController
41
41
  def show
42
42
  movie = Movie.find(params[:id])
43
43
  movie_hash = serializable(movie)
44
- render json: movie
44
+ render json: movie_hash
45
45
  end
46
46
  end
47
47
  ```
@@ -52,7 +52,13 @@ end
52
52
  class MovieSerializer
53
53
  include FastJsonapi::ObjectSerializer
54
54
 
55
- attributes :name, :year
55
+ attributes :name
56
+
57
+ attribute :year, if: Proc.new { |object| object.year.present? }
58
+
59
+ attribute :last_updated do |object|
60
+ object.updated_at
61
+ end
56
62
 
57
63
  has_many :actors
58
64
  belongs_to :owner
@@ -97,7 +103,7 @@ hash = serialize_and_flatten(movie)
97
103
  "id": "3",
98
104
  "type": "movie",
99
105
  "name": "test movie",
100
- "year": null,
106
+ "last_updated": "2019-04-26 18:55:46 UTC",
101
107
  "actors": [
102
108
  {
103
109
  "id": "1",
@@ -157,7 +163,8 @@ Attribute `:actors` will trigger `ActorSerializer` to serialize the actors colle
157
163
  Supported options include:
158
164
 
159
165
  - `:fields` ([Sparse Fieldsets](https://github.com/Netflix/fast_jsonapi#sparse-fieldsets))
160
- - `:params` ([Params / Conditional Attributes](https://github.com/Netflix/fast_jsonapi#params))
166
+ - `:params` ([Params](https://github.com/Netflix/fast_jsonapi#params))
167
+ - [Conditional Attributes](https://github.com/Netflix/fast_jsonapi#conditional-attributes)
161
168
 
162
169
  Other options are "supported" but may yeild unexpected results, as Serializable's hash flattening prioritizes deeply nested data structures.
163
170
 
@@ -178,7 +185,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
178
185
 
179
186
  ## Contributing
180
187
 
181
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/lp-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.
188
+ Bug reports and pull requests are welcome on GitHub at https://github.com/LaunchPadLab/lp-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.
182
189
 
183
190
  ## License
184
191
 
@@ -186,4 +193,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
186
193
 
187
194
  ## Code of Conduct
188
195
 
189
- Everyone interacting in the Lp::Serializable project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/lp-serializable/blob/master/CODE_OF_CONDUCT.md).
196
+ Everyone interacting in the Lp::Serializable project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/LaunchPadLab/lp-serializable/blob/master/CODE_OF_CONDUCT.md).
@@ -4,6 +4,7 @@ require 'active_support/core_ext/object'
4
4
  require 'active_support/concern'
5
5
  require 'active_support/inflector'
6
6
  require 'fast_jsonapi/serialization_core'
7
+ require 'fast_jsonapi/attribute'
7
8
 
8
9
  module FastJsonapi
9
10
  module ObjectSerializer
@@ -149,11 +150,17 @@ module FastJsonapi
149
150
 
150
151
  def attributes(*attributes_list, &block)
151
152
  attributes_list = attributes_list.first if attributes_list.first.class.is_a?(Array)
153
+ options = attributes_list.last.is_a?(Hash) ? attributes_list.pop : {}
152
154
  self.attributes_to_serialize = {} if self.attributes_to_serialize.nil?
155
+
153
156
  attributes_list.each do |attr_name|
154
157
  method_name = attr_name
155
158
  key = run_key_transform(method_name)
156
- attributes_to_serialize[key] = block || method_name
159
+ attributes_to_serialize[key] = Attribute.new(
160
+ key: key,
161
+ method: block || method_name,
162
+ options: options
163
+ )
157
164
  end
158
165
  end
159
166
 
@@ -73,12 +73,9 @@ module FastJsonapi
73
73
  end
74
74
 
75
75
  def attributes_hash(record, params = {})
76
- attributes_to_serialize.each_with_object({}) do |(key, method), attr_hash|
77
- attr_hash[key] = if method.is_a?(Proc)
78
- method.arity == 1 ? method.call(record) : method.call(record, params)
79
- else
80
- record.public_send(method)
81
- end
76
+ attributes = attributes_to_serialize
77
+ attributes.each_with_object({}) do |(_k, attribute), hash|
78
+ attribute.serialize(record, params, hash)
82
79
  end
83
80
  end
84
81
 
@@ -1,5 +1,5 @@
1
1
  module Lp
2
2
  module Serializable
3
- VERSION = "0.2.2"
3
+ VERSION = "0.2.3"
4
4
  end
5
- end
5
+ end
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["mrjonesbot"]
10
10
  spec.email = ["nate@mrjones.io"]
11
11
 
12
- spec.summary = "Serialize with Fast JSON API, flatten like AWS"
12
+ spec.summary = "Serialize with Fast JSON API, flatten like AMS"
13
13
  spec.description = "JSON API(jsonapi.org) serializer wrapper methods that work with Rails and can be used to serialize any kind of ruby object with AMS style output."
14
14
  spec.homepage = "https://www.github.com/launchpadlab/lp-serializable"
15
15
  spec.license = "MIT"
@@ -46,4 +46,4 @@ Gem::Specification.new do |spec|
46
46
  spec.add_development_dependency(%q<byebug>, [">= 0"])
47
47
 
48
48
  spec.add_dependency "fast_jsonapi", '>= 1.3'
49
- end
49
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lp-serializable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - mrjonesbot
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-04-12 00:00:00.000000000 Z
11
+ date: 2019-11-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: appraisal
@@ -273,5 +273,5 @@ rubyforge_project:
273
273
  rubygems_version: 2.7.3
274
274
  signing_key:
275
275
  specification_version: 4
276
- summary: Serialize with Fast JSON API, flatten like AWS
276
+ summary: Serialize with Fast JSON API, flatten like AMS
277
277
  test_files: []