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 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 9cee75e6..9fe65859 100644 --- a/lib/pliny/helpers/serialize.rb +++ b/lib/pliny/helpers/serialize.rb @@ -1,42 +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 = self.class.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) - @serializer_class = serializer_class - end - - attr_reader :serializer_class + # 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