-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
204 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'blueprinter/base' | ||
require_relative 'blueprinter/extension' | ||
|
||
module Blueprinter | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
module Blueprinter | ||
# | ||
# Base class for all extensions. All extension methods are implemented as no-ops. | ||
# | ||
class Extension | ||
# | ||
# Called eary during "render", this method receives the object to be rendered and | ||
# may return a modified (or new) object to be rendered. | ||
# | ||
# @param object [Object] The object to be rendered | ||
# @param _blueprint [Class] The Blueprinter class | ||
# @param _view [Symbol] The blueprint view | ||
# @param _options [Hash] Options passed to "render" | ||
# @return [Object] The object to continue rendering | ||
# | ||
def pre_render(object, _blueprint, _view, _options) | ||
object | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
module Blueprinter | ||
# | ||
# Stores and runs Blueprinter extensions. An extension is any object that implements one or more of the | ||
# extension methods: | ||
# | ||
# The Render Extension intercepts an object before rendering begins. The return value from this | ||
# method is what is ultimately rendered. | ||
# | ||
# def pre_render(object, blueprint, view, options) | ||
# # returns original, modified, or new object | ||
# end | ||
# | ||
class Extensions | ||
def initialize(extensions = []) | ||
@extensions = extensions | ||
end | ||
|
||
def to_a | ||
@extensions.dup | ||
end | ||
|
||
# Appends an extension | ||
def <<(ext) | ||
@extensions << ext | ||
self | ||
end | ||
|
||
# Runs the object through all Render Extensions and returns the final result | ||
def pre_render(object, blueprint, view, options = {}) | ||
@extensions.reduce(object) do |acc, ext| | ||
ext.pre_render(acc, blueprint, view, options) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module Blueprinter | ||
VERSION = '0.30.0' | ||
VERSION = '1.0.0' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'json' | ||
require 'ostruct' | ||
|
||
describe Blueprinter::Extensions do | ||
let(:all_extensions) { | ||
[ | ||
foo_extension.new, | ||
bar_extension.new, | ||
zar_extension.new, | ||
] | ||
} | ||
|
||
let(:foo_extension) { | ||
Class.new(Blueprinter::Extension) do | ||
def pre_render(object, _blueprint, _view, _options) | ||
obj = object.dup | ||
obj.foo = "Foo" | ||
obj | ||
end | ||
end | ||
} | ||
|
||
let(:bar_extension) { | ||
Class.new(Blueprinter::Extension) do | ||
def pre_render(object, _blueprint, _view, _options) | ||
obj = object.dup | ||
obj.bar = "Bar" | ||
obj | ||
end | ||
end | ||
} | ||
|
||
let(:zar_extension) { | ||
Class.new(Blueprinter::Extension) do | ||
def self.something_else(object, _blueprint, _view, _options) | ||
object | ||
end | ||
end | ||
} | ||
|
||
it 'should append extensions' do | ||
extensions = Blueprinter::Extensions.new | ||
extensions << foo_extension.new | ||
extensions << bar_extension.new | ||
extensions << zar_extension.new | ||
expect(extensions.to_a.map(&:class)).to eq [ | ||
foo_extension, | ||
bar_extension, | ||
zar_extension, | ||
] | ||
end | ||
|
||
it "should initialize with extensions, removing any that don't have recognized extension methods" do | ||
extensions = Blueprinter::Extensions.new(all_extensions) | ||
expect(extensions.to_a.map(&:class)).to eq [ | ||
foo_extension, | ||
bar_extension, | ||
zar_extension, | ||
] | ||
end | ||
|
||
context '#pre_render' do | ||
before :each do | ||
Blueprinter.configure do |config| | ||
config.extensions = all_extensions | ||
end | ||
end | ||
|
||
after :each do | ||
Blueprinter.configure do |config| | ||
config.extensions = [] | ||
end | ||
end | ||
|
||
let(:test_blueprint) { | ||
Class.new(Blueprinter::Base) do | ||
field :id | ||
field :name | ||
field :foo | ||
|
||
view :with_bar do | ||
field :bar | ||
end | ||
end | ||
} | ||
|
||
it 'should run all pre_render extensions' do | ||
extensions = Blueprinter::Extensions.new(all_extensions) | ||
obj = OpenStruct.new(id: 42, name: 'Jack') | ||
obj = extensions.pre_render(obj, test_blueprint, :default, {}) | ||
expect(obj.id).to be 42 | ||
expect(obj.name).to eq 'Jack' | ||
expect(obj.foo).to eq 'Foo' | ||
expect(obj.bar).to eq 'Bar' | ||
end | ||
|
||
it 'should run with Blueprinter.render using default view' do | ||
obj = OpenStruct.new(id: 42, name: 'Jack') | ||
res = JSON.parse(test_blueprint.render(obj)) | ||
expect(res['id']).to be 42 | ||
expect(res['name']).to eq 'Jack' | ||
expect(res['foo']).to eq 'Foo' | ||
expect(res['bar']).to be_nil | ||
end | ||
|
||
it 'should run with Blueprinter.render using with_bar view' do | ||
obj = OpenStruct.new(id: 42, name: 'Jack') | ||
res = JSON.parse(test_blueprint.render(obj, view: :with_bar)) | ||
expect(res['id']).to be 42 | ||
expect(res['name']).to eq 'Jack' | ||
expect(res['foo']).to eq 'Foo' | ||
expect(res['bar']).to eq 'Bar' | ||
end | ||
|
||
it 'should run with Blueprinter.render_as_hash' do | ||
obj = OpenStruct.new(id: 42, name: 'Jack') | ||
res = test_blueprint.render_as_hash(obj, view: :with_bar) | ||
expect(res[:id]).to be 42 | ||
expect(res[:name]).to eq 'Jack' | ||
expect(res[:foo]).to eq 'Foo' | ||
expect(res[:bar]).to eq 'Bar' | ||
end | ||
end | ||
end |