blueprinter 0.2.0 → 0.3.0

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
- SHA1:
3
- metadata.gz: 6e7d939f775eceed008783088ae2e73481798aaf
4
- data.tar.gz: 9c013582657eef9da0e37533e933058253803109
2
+ SHA256:
3
+ metadata.gz: 2924df0d9994662334f417cc9cda5b05863152bad025bb7193e19f95ec6d448b
4
+ data.tar.gz: 8eb870216b6535ed26793b97d2b3cc0aaeec224894dbbf4e0582e9369a70a7e7
5
5
  SHA512:
6
- metadata.gz: 7f1947a5d7d6155aabe3fa0a2388f3e24bd2c447d78d1bd3b6ce1d639831b4eff95047851dff5226854fce1eaa0a6e0f99bfbd7b1467f0a6cc450ee11af4b61a
7
- data.tar.gz: bf51f3e711eb499beaf4bc343a03dcd7401159487528d22211502ea388dbb60498fc50b9de9ec51df23e3eaa6319fd018658d6be699766d6f960eb7252120605
6
+ metadata.gz: ff5834c72d06aca41d2f2cf6d309015af5dea83ea66d008da8f5de980162676e0131612a6dfa2a93e5bd14f6deb82c39d8f96e468181bb57b30219f668a45128
7
+ data.tar.gz: f716a6124b91d751c0f9bbdfd248f0b4ccedcd98548ed1572173d1166cfb2d912691f927e707499f0c113d1b62330340ce5f64198251899c404e0e83ff2de428
@@ -1,3 +1,20 @@
1
+ ## 0.3.0 - 2018/04/05
2
+
3
+ Sort of a breaking Change. Serializer classes has been renamed to Extractor. To upgrade, if you passed in a specific serializer to `field` or `identifier` such as:
4
+
5
+ ```
6
+ field(:first_name, serializer: CustomSerializer)
7
+ ```
8
+
9
+ Please rename that to:
10
+
11
+ ```
12
+ field(:first_name, extractor: CustomExtractor)
13
+ ```
14
+
15
+ * [ENHANCEMENT] Renamed Serializer classes to Extractor. See #72.
16
+ * [ENHANCEMENT] Updated README. See #66, #65
17
+
1
18
  ## 0.2.0 - 2018/01/22
2
19
 
3
20
  Breaking Changes. To upgrade, ensure that any associated objects have a blueprint. For example:
data/README.md CHANGED
@@ -1,13 +1,15 @@
1
1
  [![CircleCI](https://circleci.com/gh/procore/blueprinter.svg?style=svg)](https://circleci.com/gh/procore/blueprinter)
2
2
  [![Gem Version](https://badge.fury.io/rb/blueprinter.svg)](https://badge.fury.io/rb/blueprinter)
3
3
 
4
+ <img src="blueprinter_logo.svg" width="25%">
5
+
4
6
  # Blueprinter
5
7
  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.
6
8
 
7
9
  It heavily relies on the idea of `views` which, similar to Rails views, are ways of predefining output for data in different contexts.
8
10
 
9
11
  ## Documentation
10
- !TODO Link to the docs
12
+ Docs can be found [here](http://www.rubydoc.info/gems/blueprinter).
11
13
 
12
14
  ## Usage
13
15
  ### Basic
@@ -95,7 +97,7 @@ end
95
97
 
96
98
  Usage:
97
99
  ```ruby
98
- puts UserBlueprint.render(user, view: :extended)
100
+ puts UserBlueprint.render(user, view: :normal)
99
101
  ```
100
102
 
101
103
  Output:
@@ -212,7 +214,7 @@ Ensure that you have the `Oj` gem installed in your Gemfile if you haven't alrea
212
214
  gem 'oj'
213
215
  ```
214
216
 
215
- ## Documentation
217
+ ## How to Document
216
218
 
217
219
  We use [Yard](https://yardoc.org/) for documentation. Here are the following
218
220
  documentation rules:
@@ -1,11 +1,11 @@
1
1
  require_relative 'blueprinter_error'
2
2
  require_relative 'helpers/active_record_helpers'
3
- require_relative 'serializer'
4
- require_relative 'serializers/association_serializer'
5
- require_relative 'serializers/auto_serializer'
6
- require_relative 'serializers/block_serializer'
7
- require_relative 'serializers/hash_serializer'
8
- require_relative 'serializers/public_send_serializer'
3
+ require_relative 'extractor'
4
+ require_relative 'extractors/association_extractor'
5
+ require_relative 'extractors/auto_extractor'
6
+ require_relative 'extractors/block_extractor'
7
+ require_relative 'extractors/hash_extractor'
8
+ require_relative 'extractors/public_send_extractor'
9
9
  require_relative 'field'
10
10
  require_relative 'view'
11
11
  require_relative 'view_collection'
@@ -24,10 +24,10 @@ module Blueprinter
24
24
  # want to set for serialization.
25
25
  # @param name [Symbol] to rename the identifier key in the JSON
26
26
  # output. Defaults to method given.
27
- # @param serializer [AssociationSerializer,AutoSerializer,BlockSerializer,HashSerializer,PublicSendSerializer]
28
- # Kind of serializer to use.
29
- # Either define your own or use Blueprinter's premade serializers.
30
- # Defaults to AutoSerializer
27
+ # @param extractor [AssociationExtractor,AutoExtractor,BlockExtractor,HashExtractor,PublicSendExtractor]
28
+ # Kind of extractor to use.
29
+ # Either define your own or use Blueprinter's premade extractors.
30
+ # Defaults to AutoExtractor
31
31
  #
32
32
  # @example Specifying a uuid as an identifier.
33
33
  # class UserBlueprint < Blueprinter::Base
@@ -36,8 +36,8 @@ module Blueprinter
36
36
  # end
37
37
  #
38
38
  # @return [Field] A Field object
39
- def self.identifier(method, name: method, serializer: AutoSerializer)
40
- view_collection[:identifier] << Field.new(method, name, serializer)
39
+ def self.identifier(method, name: method, extractor: AutoExtractor)
40
+ view_collection[:identifier] << Field.new(method, name, extractor)
41
41
  end
42
42
 
43
43
  # Specify a field or method name to be included for serialization.
@@ -46,10 +46,10 @@ module Blueprinter
46
46
  # @param method [Symbol] the field or method name you want to include for
47
47
  # serialization.
48
48
  # @param options [Hash] options to overide defaults.
49
- # @option options [AssociationSerializer,BlockSerializer,HashSerializer,PublicSendSerializer] :serializer
50
- # Kind of serializer to use.
51
- # Either define your own or use Blueprinter's premade serializers. The
52
- # Default serializer is AutoSerializer
49
+ # @option options [AssociationExtractor,BlockExtractor,HashExtractor,PublicSendExtractor] :extractor
50
+ # Kind of extractor to use.
51
+ # Either define your own or use Blueprinter's premade extractors. The
52
+ # Default extractor is AutoExtractor
53
53
  # @option options [Symbol] :name Use this to rename the method. Useful if
54
54
  # if you want your JSON key named differently in the output than your
55
55
  # object's field or method name.
@@ -71,13 +71,13 @@ module Blueprinter
71
71
  # @return [Field] A Field object
72
72
  def self.field(method, options = {}, &block)
73
73
  options = if block_given?
74
- {name: method, serializer: BlockSerializer, block: {method => block}}
74
+ {name: method, extractor: BlockExtractor, block: {method => block}}
75
75
  else
76
- {name: method, serializer: AutoSerializer}
76
+ {name: method, extractor: AutoExtractor}
77
77
  end.merge(options)
78
78
  current_view << Field.new(method,
79
79
  options[:name],
80
- options[:serializer],
80
+ options[:extractor],
81
81
  options)
82
82
  end
83
83
 
@@ -106,7 +106,7 @@ module Blueprinter
106
106
  name = options.delete(:name) || method
107
107
  current_view << Field.new(method,
108
108
  name,
109
- AssociationSerializer,
109
+ AssociationExtractor,
110
110
  options.merge(association: true))
111
111
  end
112
112
 
@@ -172,7 +172,7 @@ module Blueprinter
172
172
  # @return [Array<Symbol>] an array of field names
173
173
  def self.fields(*field_names)
174
174
  field_names.each do |field_name|
175
- current_view << Field.new(field_name, field_name, AutoSerializer)
175
+ current_view << Field.new(field_name, field_name, AutoExtractor)
176
176
  end
177
177
  end
178
178
 
@@ -249,7 +249,7 @@ module Blueprinter
249
249
 
250
250
  def self.object_to_hash(object, view_name:, local_options:)
251
251
  view_collection.fields_for(view_name).each_with_object({}) do |field, hash|
252
- hash[field.name] = field.serialize(object, local_options)
252
+ hash[field.name] = field.extract(object, local_options)
253
253
  end
254
254
  end
255
255
  private_class_method :object_to_hash
@@ -0,0 +1,15 @@
1
+ # @api private
2
+ module Blueprinter
3
+ class Extractor
4
+ def initialize
5
+ end
6
+
7
+ def extract(field_name, object, local_options, options={})
8
+ fail NotImplementedError, "An Extractor must implement #extract"
9
+ end
10
+
11
+ def self.extract(field_name, object, local_options, options={})
12
+ self.new.extract(field_name, object, local_options, options)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,10 @@
1
+ module Blueprinter
2
+ class AssociationExtractor < Extractor
3
+ def extract(association_name, object, local_options, options={})
4
+ value = object.public_send(association_name)
5
+ return value if value.nil?
6
+ view = options[:view] || :default
7
+ options[:blueprint].prepare(value, view_name: view, local_options: local_options)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ module Blueprinter
2
+ class AutoExtractor < Extractor
3
+ def extract(field_name, object, local_options, options = {})
4
+ extractor = object.is_a?(Hash) ? HashExtractor : PublicSendExtractor
5
+ extractor.extract(field_name, object, local_options, options = {})
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ module Blueprinter
2
+ class BlockExtractor < Extractor
3
+ def extract(field_name, object, local_options, options = {})
4
+ options[:block][field_name].call(object, local_options)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Blueprinter
2
+ class HashExtractor < Extractor
3
+ def extract(field_name, object, local_options, options = {})
4
+ object[field_name] || object[field_name.to_s]
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Blueprinter
2
+ class PublicSendExtractor < Extractor
3
+ def extract(field_name, object, local_options, options = {})
4
+ object.public_send(field_name)
5
+ end
6
+ end
7
+ end
@@ -1,14 +1,14 @@
1
1
  # @api private
2
2
  class Blueprinter::Field
3
- attr_reader :method, :name, :serializer, :options
4
- def initialize(method, name, serializer, options = {})
3
+ attr_reader :method, :name, :extractor, :options
4
+ def initialize(method, name, extractor, options = {})
5
5
  @method = method
6
6
  @name = name
7
- @serializer = serializer
7
+ @extractor = extractor
8
8
  @options = options
9
9
  end
10
10
 
11
- def serialize(object, local_options)
12
- serializer.serialize(method, object, local_options, options)
11
+ def extract(object, local_options)
12
+ extractor.extract(method, object, local_options, options)
13
13
  end
14
14
  end
@@ -1,3 +1,3 @@
1
1
  module Blueprinter
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
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.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Hess
@@ -9,8 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-01-22 00:00:00.000000000 Z
12
+ date: 2018-04-05 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: 1.8.2
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: 1.8.2
14
28
  - !ruby/object:Gem::Dependency
15
29
  name: oj
16
30
  requirement: !ruby/object:Gem::Requirement
@@ -113,14 +127,14 @@ files:
113
127
  - lib/blueprinter/base.rb
114
128
  - lib/blueprinter/blueprinter_error.rb
115
129
  - lib/blueprinter/configuration.rb
130
+ - lib/blueprinter/extractor.rb
131
+ - lib/blueprinter/extractors/association_extractor.rb
132
+ - lib/blueprinter/extractors/auto_extractor.rb
133
+ - lib/blueprinter/extractors/block_extractor.rb
134
+ - lib/blueprinter/extractors/hash_extractor.rb
135
+ - lib/blueprinter/extractors/public_send_extractor.rb
116
136
  - lib/blueprinter/field.rb
117
137
  - lib/blueprinter/helpers/active_record_helpers.rb
118
- - lib/blueprinter/serializer.rb
119
- - lib/blueprinter/serializers/association_serializer.rb
120
- - lib/blueprinter/serializers/auto_serializer.rb
121
- - lib/blueprinter/serializers/block_serializer.rb
122
- - lib/blueprinter/serializers/hash_serializer.rb
123
- - lib/blueprinter/serializers/public_send_serializer.rb
124
138
  - lib/blueprinter/version.rb
125
139
  - lib/blueprinter/view.rb
126
140
  - lib/blueprinter/view_collection.rb
@@ -145,7 +159,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
145
159
  version: '0'
146
160
  requirements: []
147
161
  rubyforge_project:
148
- rubygems_version: 2.6.11
162
+ rubygems_version: 2.7.3
149
163
  signing_key:
150
164
  specification_version: 4
151
165
  summary: Simple Fast Declarative Serialization Library
@@ -1,13 +0,0 @@
1
- # @api private
2
- class Blueprinter::Serializer
3
- def initialize
4
- end
5
-
6
- def serialize(field_name, object, local_options, options={})
7
- fail NotImplementedError, "A serializer must implement #serialize"
8
- end
9
-
10
- def self.serialize(field_name, object, local_options, options={})
11
- self.new.serialize(field_name, object, local_options, options)
12
- end
13
- end
@@ -1,8 +0,0 @@
1
- class Blueprinter::AssociationSerializer < Blueprinter::Serializer
2
- def serialize(association_name, object, local_options, options={})
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)
7
- end
8
- end
@@ -1,8 +0,0 @@
1
- module Blueprinter
2
- class AutoSerializer < Blueprinter::Serializer
3
- def serialize(field_name, object, local_options, options = {})
4
- serializer = object.is_a?(Hash) ? HashSerializer : PublicSendSerializer
5
- serializer.serialize(field_name, object, local_options, options = {})
6
- end
7
- end
8
- end
@@ -1,5 +0,0 @@
1
- class Blueprinter::BlockSerializer < Blueprinter::Serializer
2
- def serialize(field_name, object, local_options, options = {})
3
- options[:block][field_name].call(object, local_options)
4
- end
5
- end
@@ -1,5 +0,0 @@
1
- class Blueprinter::HashSerializer < Blueprinter::Serializer
2
- def serialize(field_name, object, local_options, options = {})
3
- object[field_name] || object[field_name.to_s]
4
- end
5
- end
@@ -1,5 +0,0 @@
1
- class Blueprinter::PublicSendSerializer < Blueprinter::Serializer
2
- def serialize(field_name, object, local_options, options = {})
3
- object.public_send(field_name)
4
- end
5
- end