From 8cb88547a6f00a89f0ed27b8cf554815c3876a01 Mon Sep 17 00:00:00 2001 From: Ben Symonds Date: Fri, 18 Sep 2020 12:25:26 +0100 Subject: [PATCH 1/4] Add `.byebug_history` to gitignore `pry-byebug` is in the gemspec so we expect this file to exist sometimes. --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 00c92f5a..9376a2c6 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ Gemfile.lock .bundle .ruby-* .tags +.byebug_history From 0472cd0cb3dee53ebcacb7cb79d3b57bce843ed3 Mon Sep 17 00:00:00 2001 From: Ben Symonds Date: Fri, 18 Sep 2020 12:05:56 +0100 Subject: [PATCH 2/4] Use squiggle heredoc in error message This allows us to indent the text properly without the whitespace making it into the resulting string. --- lib/pliny/helpers/serialize.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/pliny/helpers/serialize.rb b/lib/pliny/helpers/serialize.rb index 9cee75e6..678c2d6c 100644 --- a/lib/pliny/helpers/serialize.rb +++ b/lib/pliny/helpers/serialize.rb @@ -8,9 +8,9 @@ def serialize(data, structure = :default) serializer_class = self.class.serializer_class if serializer_class.nil? - raise <<-eos.strip -No serializer has been specified for this endpoint. Please specify one with -`serializer Serializers::ModelName` in the endpoint. + raise <<~eos.strip + No serializer has been specified for this endpoint. Please specify one with + `serializer Serializers::ModelName` in the endpoint. eos end From e5e236fc55211932d9d6686afe58a32507c594bf Mon Sep 17 00:00:00 2001 From: Ben Symonds Date: Fri, 18 Sep 2020 12:25:13 +0100 Subject: [PATCH 3/4] Do serializer configuration properly Looking at the sinatra extensions guide[1] it looks like you're meant to use `set` to set this kind of class-level state. The value has to be initialised to nil otherwise you get `NoMethodError: undefined method `serializer_class'`, I guess because the accessor method is only created when `set` is called. [1] http://sinatrarb.com/extensions.html#setting-options-and-other-extension-setup --- lib/pliny/helpers/serialize.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/pliny/helpers/serialize.rb b/lib/pliny/helpers/serialize.rb index 678c2d6c..d68f8dc4 100644 --- a/lib/pliny/helpers/serialize.rb +++ b/lib/pliny/helpers/serialize.rb @@ -2,10 +2,11 @@ module Pliny::Helpers module Serialize def self.included(base) base.send :extend, ClassMethods + base.set :serializer_class, nil end def serialize(data, structure = :default) - serializer_class = self.class.serializer_class + serializer_class = settings.serializer_class if serializer_class.nil? raise <<~eos.strip @@ -33,10 +34,8 @@ module ClassMethods # end # end def serializer(serializer_class) - @serializer_class = serializer_class + set :serializer_class, serializer_class end - - attr_reader :serializer_class end end end From ffefcdcbeb012d9bef317853ecf03cbc1a7e7daf Mon Sep 17 00:00:00 2001 From: Ben Symonds Date: Fri, 18 Sep 2020 12:27:31 +0100 Subject: [PATCH 4/4] Add serializer DSL correctly Following the sinatra extensions guide[1] it looks like this is how you're meant to do it. Unfortunately this results in a breaking change in pliny because pliny apps will have to switch their endpoint classes to use `register` instead of `helper` (see the change in the template in this commit). But it fixes a compatibility issue between pliny and sinatra 2.1 where the latter has switched its internal behaviour, using `prepend` rather than `include` for helpers, meaning the `self.included` hook is no longer being called. [1] http://sinatrarb.com/extensions.html#extending-the-dsl-class-context-with-sinatraregister --- CHANGELOG.md | 2 ++ lib/pliny/helpers/serialize.rb | 54 +++++++++++++++--------------- lib/template/lib/endpoints/base.rb | 2 +- spec/helpers/serialize_spec.rb | 4 +-- 4 files changed, 32 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf9632bd..7bd687a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Fixed +- **BREAKING**: Register `Serialize` sinatra extension correctly. This will require a change in `lib/endpoints/base.rb` - see [here](https://github.com/interagent/pliny/pull/337/files#diff-c7736e8c14f72274bc01c22fe809a6bb). ([#337](https://github.com/interagent/pliny/pull/337)) ## [0.28.0] - 2020-05-06 ### Changed diff --git a/lib/pliny/helpers/serialize.rb b/lib/pliny/helpers/serialize.rb index d68f8dc4..9fe65859 100644 --- a/lib/pliny/helpers/serialize.rb +++ b/lib/pliny/helpers/serialize.rb @@ -1,41 +1,41 @@ module Pliny::Helpers module Serialize - def self.included(base) - base.send :extend, ClassMethods + def self.registered(base) + base.helpers Helpers base.set :serializer_class, nil end - def serialize(data, structure = :default) - serializer_class = settings.serializer_class + module Helpers + def serialize(data, structure = :default) + serializer_class = settings.serializer_class - if serializer_class.nil? - raise <<~eos.strip - No serializer has been specified for this endpoint. Please specify one with - `serializer Serializers::ModelName` in the endpoint. - eos - end + if serializer_class.nil? + raise <<~eos.strip + No serializer has been specified for this endpoint. Please specify one with + `serializer Serializers::ModelName` in the endpoint. + eos + end - env['pliny.serializer_arity'] = data.respond_to?(:size) ? data.size : 1 + env['pliny.serializer_arity'] = data.respond_to?(:size) ? data.size : 1 - start = Time.now - serializer_class.new(structure).serialize(data).tap do - env['pliny.serializer_timing'] = (Time.now - start).to_f + start = Time.now + serializer_class.new(structure).serialize(data).tap do + env['pliny.serializer_timing'] = (Time.now - start).to_f + end end end - module ClassMethods - # Provide a way to specify endpoint serializer class. - # - # class Endpoints::User < Base - # serializer Serializers::User - # - # get do - # encode serialize(User.all) - # end - # end - def serializer(serializer_class) - set :serializer_class, serializer_class - end + # Provide a way to specify endpoint serializer class. + # + # class Endpoints::User < Base + # serializer Serializers::User + # + # get do + # encode serialize(User.all) + # end + # end + def serializer(serializer_class) + set :serializer_class, serializer_class end end end diff --git a/lib/template/lib/endpoints/base.rb b/lib/template/lib/endpoints/base.rb index 12b48546..dd925574 100644 --- a/lib/template/lib/endpoints/base.rb +++ b/lib/template/lib/endpoints/base.rb @@ -5,7 +5,7 @@ class Base < Sinatra::Base helpers Pliny::Helpers::Encode helpers Pliny::Helpers::Params - helpers Pliny::Helpers::Serialize + register Pliny::Helpers::Serialize set :dump_errors, false set :raise_errors, true diff --git a/spec/helpers/serialize_spec.rb b/spec/helpers/serialize_spec.rb index 0d31036d..e59418e5 100644 --- a/spec/helpers/serialize_spec.rb +++ b/spec/helpers/serialize_spec.rb @@ -4,7 +4,7 @@ context "without a serializer" do def app Sinatra.new do - helpers Pliny::Helpers::Serialize + register Pliny::Helpers::Serialize get "/" do MultiJson.encode(serialize([])) @@ -29,7 +29,7 @@ def serialize(data) def app Sinatra.new do - helpers Pliny::Helpers::Serialize + register Pliny::Helpers::Serialize serializer Serializer