blueprinter 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +13 -0
- data/README.md +7 -1
- data/lib/blueprinter/base.rb +5 -4
- data/lib/blueprinter/serializers/association_serializer.rb +4 -6
- data/lib/blueprinter/version.rb +1 -1
- metadata +4 -5
- data/lib/blueprinter/optimizer.rb +0 -30
- data/lib/rabbit_painter.rb +0 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6e7d939f775eceed008783088ae2e73481798aaf
|
4
|
+
data.tar.gz: 9c013582657eef9da0e37533e933058253803109
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7f1947a5d7d6155aabe3fa0a2388f3e24bd2c447d78d1bd3b6ce1d639831b4eff95047851dff5226854fce1eaa0a6e0f99bfbd7b1467f0a6cc450ee11af4b61a
|
7
|
+
data.tar.gz: bf51f3e711eb499beaf4bc343a03dcd7401159487528d22211502ea388dbb60498fc50b9de9ec51df23e3eaa6319fd018658d6be699766d6f960eb7252120605
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
## 0.2.0 - 2018/01/22
|
2
|
+
|
3
|
+
Breaking Changes. To upgrade, ensure that any associated objects have a blueprint. For example:
|
4
|
+
```
|
5
|
+
association :comments, blueprint: CommentsBlueprint
|
6
|
+
```
|
7
|
+
|
8
|
+
* [BUGFIX] Remove Optimizer class. See #61.
|
9
|
+
* [BUGFIX] Require associated objects to have a Blueprint, so that objects will always serialize properly. See #60.
|
10
|
+
|
11
|
+
## 0.1.0 - 2018/01/17
|
12
|
+
|
13
|
+
* [FEATURE] Initial release of Blueprinter
|
data/README.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
[![CircleCI](https://circleci.com/gh/procore/blueprinter.svg?style=svg)](https://circleci.com/gh/procore/blueprinter)
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/blueprinter.svg)](https://badge.fury.io/rb/blueprinter)
|
2
3
|
|
3
4
|
# Blueprinter
|
4
5
|
Blueprinter is a JSON Object Presenter for Ruby that takes business objects and breaks them down into simple hashes and serializes them to JSON. It can be used in Rails in place of other serializers (like JBuilder or ActiveModelSerializers). It is designed to be simple, direct, and performant.
|
@@ -76,13 +77,18 @@ Output:
|
|
76
77
|
### Associations
|
77
78
|
You may include associated objects. Say for example, a user has projects:
|
78
79
|
```ruby
|
80
|
+
class ProjectBlueprint < Blueprinter::Base
|
81
|
+
identifier :uuid
|
82
|
+
field :name
|
83
|
+
end
|
84
|
+
|
79
85
|
class UserBlueprint < Blueprinter::Base
|
80
86
|
identifier :uuid
|
81
87
|
field :email, name: :login
|
82
88
|
|
83
89
|
view :normal do
|
84
90
|
fields :first_name, :last_name
|
85
|
-
association :projects
|
91
|
+
association :projects, blueprint: ProjectBlueprint
|
86
92
|
end
|
87
93
|
end
|
88
94
|
```
|
data/lib/blueprinter/base.rb
CHANGED
@@ -9,7 +9,6 @@ require_relative 'serializers/public_send_serializer'
|
|
9
9
|
require_relative 'field'
|
10
10
|
require_relative 'view'
|
11
11
|
require_relative 'view_collection'
|
12
|
-
require_relative 'optimizer'
|
13
12
|
|
14
13
|
module Blueprinter
|
15
14
|
class Base
|
@@ -87,6 +86,8 @@ module Blueprinter
|
|
87
86
|
#
|
88
87
|
# @param method [Symbol] the association name
|
89
88
|
# @param options [Hash] options to overide defaults.
|
89
|
+
# @option options [Symbol] :blueprint Required. Use this to specify the
|
90
|
+
# blueprint to use for the associated object.
|
90
91
|
# @option options [Symbol] :name Use this to rename the association in the
|
91
92
|
# JSON output.
|
92
93
|
# @option options [Symbol] :view Specify the view to use or fall back to
|
@@ -95,12 +96,13 @@ module Blueprinter
|
|
95
96
|
# @example Specifying an association
|
96
97
|
# class UserBlueprint < Blueprinter::Base
|
97
98
|
# # code
|
98
|
-
# association :vehicles, view: :extended
|
99
|
+
# association :vehicles, view: :extended, blueprint: VehiclesBlueprint
|
99
100
|
# # code
|
100
101
|
# end
|
101
102
|
#
|
102
103
|
# @return [Field] A Field object
|
103
104
|
def self.association(method, options = {})
|
105
|
+
raise BlueprinterError, 'blueprint required' unless options[:blueprint]
|
104
106
|
name = options.delete(:name) || method
|
105
107
|
current_view << Field.new(method,
|
106
108
|
name,
|
@@ -141,8 +143,7 @@ module Blueprinter
|
|
141
143
|
raise BlueprinterError, "View '#{view_name}' is not defined"
|
142
144
|
end
|
143
145
|
fields = view_collection.fields_for(view_name)
|
144
|
-
prepared_object =
|
145
|
-
prepared_object = include_associations(prepared_object, view_name: view_name)
|
146
|
+
prepared_object = include_associations(object, view_name: view_name)
|
146
147
|
if array_like?(object)
|
147
148
|
prepared_object.map do |obj|
|
148
149
|
object_to_hash(obj,
|
@@ -1,10 +1,8 @@
|
|
1
1
|
class Blueprinter::AssociationSerializer < Blueprinter::Serializer
|
2
2
|
def serialize(association_name, object, local_options, options={})
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
object.public_send(association_name)
|
8
|
-
end
|
3
|
+
value = object.public_send(association_name)
|
4
|
+
return value if value.nil?
|
5
|
+
view = options[:view] || :default
|
6
|
+
options[:blueprint].prepare(value, view_name: view, local_options: local_options)
|
9
7
|
end
|
10
8
|
end
|
data/lib/blueprinter/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: blueprinter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Hess
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2018-01-
|
12
|
+
date: 2018-01-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: oj
|
@@ -105,6 +105,7 @@ executables: []
|
|
105
105
|
extensions: []
|
106
106
|
extra_rdoc_files: []
|
107
107
|
files:
|
108
|
+
- CHANGELOG.md
|
108
109
|
- MIT-LICENSE
|
109
110
|
- README.md
|
110
111
|
- Rakefile
|
@@ -114,7 +115,6 @@ files:
|
|
114
115
|
- lib/blueprinter/configuration.rb
|
115
116
|
- lib/blueprinter/field.rb
|
116
117
|
- lib/blueprinter/helpers/active_record_helpers.rb
|
117
|
-
- lib/blueprinter/optimizer.rb
|
118
118
|
- lib/blueprinter/serializer.rb
|
119
119
|
- lib/blueprinter/serializers/association_serializer.rb
|
120
120
|
- lib/blueprinter/serializers/auto_serializer.rb
|
@@ -124,7 +124,6 @@ files:
|
|
124
124
|
- lib/blueprinter/version.rb
|
125
125
|
- lib/blueprinter/view.rb
|
126
126
|
- lib/blueprinter/view_collection.rb
|
127
|
-
- lib/rabbit_painter.rb
|
128
127
|
- lib/tasks/blueprinter_tasks.rake
|
129
128
|
homepage: https://github.com/procore/blueprinter
|
130
129
|
licenses:
|
@@ -146,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
146
145
|
version: '0'
|
147
146
|
requirements: []
|
148
147
|
rubyforge_project:
|
149
|
-
rubygems_version: 2.
|
148
|
+
rubygems_version: 2.6.11
|
150
149
|
signing_key:
|
151
150
|
specification_version: 4
|
152
151
|
summary: Simple Fast Declarative Serialization Library
|
@@ -1,30 +0,0 @@
|
|
1
|
-
module Blueprinter
|
2
|
-
# @api private
|
3
|
-
class Optimizer
|
4
|
-
include ActiveRecordHelpers
|
5
|
-
class << self
|
6
|
-
def optimize(object, fields:)
|
7
|
-
return object unless active_record_relation?(object)
|
8
|
-
select_columns = (active_record_attributes_for(object) &
|
9
|
-
fields.map(&:method)) +
|
10
|
-
required_lookup_attributes_for(object)
|
11
|
-
object.select(*select_columns)
|
12
|
-
end
|
13
|
-
|
14
|
-
private
|
15
|
-
|
16
|
-
def active_record_attributes_for(object)
|
17
|
-
object.klass.column_names.map(&:to_sym)
|
18
|
-
end
|
19
|
-
|
20
|
-
def required_lookup_attributes_for(object)
|
21
|
-
# TODO: We may not need all four of these
|
22
|
-
lookup_values = (object.includes_values +
|
23
|
-
object.preload_values +
|
24
|
-
object.joins_values +
|
25
|
-
object.eager_load_values).uniq
|
26
|
-
lookup_values.map {|value| object.reflections[value.to_s].foreign_key}
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
data/lib/rabbit_painter.rb
DELETED