-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JIT evaluation of 'view', 'exclude', 'use', etc so that order doesn't…
… matter Signed-off-by: Jordan Hollinger <[email protected]>
- Loading branch information
1 parent
c2db5e5
commit ef93e6f
Showing
9 changed files
with
384 additions
and
80 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
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,66 @@ | ||
# frozen_string_literal: true | ||
|
||
module Blueprinter | ||
class V2 | ||
# | ||
# A Hash-like class that defers evaluation of "view" blocks until they're accessed. | ||
# This allows things like a parent's fields and partials to be defined AFTER views | ||
# and have inheritance still work as expected. | ||
# | ||
class ViewBuilder | ||
include Enumerable | ||
|
||
# @param parent [Class] A subclass of Blueprinter::V2 | ||
def initialize(parent) | ||
@parent = parent | ||
@views = { default: parent } | ||
@pending = {} | ||
@mut = Mutex.new | ||
end | ||
|
||
# | ||
# Add a view definition. | ||
# | ||
# @param name [Symbol] | ||
# @param definition [Proc] | ||
# | ||
def []=(name, definition) | ||
@pending[name.to_sym] = definition | ||
end | ||
|
||
# | ||
# Return, and build if necessary, the view. | ||
# | ||
# @param name [Symbol] Name of the view | ||
# @return [Class] An anonymous subclass of @parent | ||
# | ||
def [](name) | ||
name = name.to_sym | ||
if !@views.key?(name) and @pending.key?(name) | ||
@mut.synchronize do | ||
next if @views.key?(name) | ||
|
||
view = Class.new(@parent, &@pending[name]) | ||
view.append_name(name) | ||
view.eval!(false) | ||
@views[name] = view | ||
end | ||
end | ||
@views[name] | ||
end | ||
|
||
# Works like Hash#fetch | ||
def fetch(name) | ||
self[name] || raise(KeyError, "View '#{name}' not found") | ||
end | ||
|
||
def each(&block) | ||
enum = Enumerator.new do |y| | ||
y.yield(:default, self[:default]) | ||
@pending.each_key { |name| y.yield(name, self[name]) } | ||
end | ||
block ? enum.each(&block) : enum | ||
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# frozen_string_literal: true | ||
|
||
describe "Blueprinter::V2 Declarative API" do | ||
it "should inherit fields defined after the view" do | ||
blueprint = Class.new(Blueprinter::V2) do | ||
view :desc do | ||
field :description | ||
end | ||
|
||
field :id | ||
field :name | ||
end | ||
|
||
refs = blueprint.reflections | ||
expect(refs[:desc].fields.keys.sort).to eq %i(id name description).sort | ||
end | ||
|
||
it "should include partials defined after the view" do | ||
blueprint = Class.new(Blueprinter::V2) do | ||
field :name | ||
|
||
view :foo do | ||
use :desc | ||
end | ||
|
||
partial :desc do | ||
field :description | ||
end | ||
end | ||
|
||
refs = blueprint.reflections | ||
expect(refs[:foo].fields.keys.sort).to eq %i(name description).sort | ||
end | ||
|
||
it "should include partials defined after the use statement" do | ||
blueprint = Class.new(Blueprinter::V2) do | ||
field :name | ||
use :desc | ||
|
||
partial :desc do | ||
field :description | ||
end | ||
end | ||
|
||
refs = blueprint.reflections | ||
expect(refs[:default].fields.keys.sort).to eq %i(name description).sort | ||
end | ||
|
||
it "should exclude fields added after the exclude statement" do | ||
blueprint = Class.new(Blueprinter::V2) do | ||
field :id | ||
field :name | ||
|
||
view :foo do | ||
exclude :name, :description2, :description3 | ||
use :desc | ||
field :description3 | ||
end | ||
|
||
partial :desc do | ||
field :description | ||
field :description2 | ||
end | ||
end | ||
|
||
refs = blueprint.reflections | ||
expect(refs[:foo].fields.keys.sort).to eq %i(id description).sort | ||
end | ||
end |
Oops, something went wrong.