Skip to content

Commit

Permalink
Merge pull request #337 from interagent/proper-extension-registration
Browse files Browse the repository at this point in the history
Fix sinatra extension registration
  • Loading branch information
bensymonds authored Sep 30, 2020
2 parents 182b447 + ffefcdc commit 3c5b0ce
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ Gemfile.lock
.bundle
.ruby-*
.tags
.byebug_history
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 28 additions & 29 deletions lib/pliny/helpers/serialize.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/template/lib/endpoints/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/helpers/serialize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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([]))
Expand All @@ -29,7 +29,7 @@ def serialize(data)

def app
Sinatra.new do
helpers Pliny::Helpers::Serialize
register Pliny::Helpers::Serialize

serializer Serializer

Expand Down

0 comments on commit 3c5b0ce

Please sign in to comment.