-
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.
Break V2 into modules. Start on reflection
Signed-off-by: Jordan Hollinger <[email protected]>
- Loading branch information
1 parent
ee90379
commit 7af7bae
Showing
5 changed files
with
170 additions
and
64 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
module Blueprinter | ||
class V2 | ||
module DSL | ||
# Define a new child view, which is a subclass of self | ||
def view(name, &definition) | ||
raise Errors::InvalidBlueprint, "View name may not contain '.'" if name.to_s =~ /\./ | ||
|
||
view = Class.new(self) | ||
view.append_name(name) | ||
view.class_eval(&definition) if definition | ||
views[name.to_sym] = view | ||
end | ||
|
||
# Define a field | ||
# rubocop:todo Lint/UnusedMethodArgument | ||
def field(name, options = {}) | ||
fields[name.to_sym] = 'TODO' | ||
end | ||
|
||
# Define an association | ||
def association(name, blueprint, options = {}) | ||
fields[name.to_sym] = 'TODO' | ||
end | ||
|
||
# Exclude fields/associations | ||
def exclude(*names) | ||
unknown = [] | ||
names.each do |name| | ||
name_sym = name.to_sym | ||
if fields.key? name_sym | ||
fields.delete name_sym | ||
else | ||
unknown << name.to_s | ||
end | ||
end | ||
raise Errors::InvalidBlueprint, "Unknown excluded fields in '#{self}': #{unknown.join(', ')}" if unknown.any? | ||
end | ||
|
||
# rubocop:enable Lint/UnusedMethodArgument | ||
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,49 @@ | ||
# frozen_string_literal: true | ||
|
||
module Blueprinter | ||
class V2 | ||
module Reflection | ||
def self.extended(klass) | ||
klass.class_eval do | ||
private_class_method :flattened_views | ||
end | ||
end | ||
|
||
# | ||
# Returns a Hash of views keyed by name. | ||
# | ||
# @return [Hash<Symbol, Blueprinter::V2::Reflection::View>] | ||
# | ||
def reflections | ||
@reflections ||= flattened_views(views) | ||
end | ||
|
||
# Builds a flat Hash of nested views | ||
def flattened_views(views, acc = {}) | ||
views.each_with_object(acc) do |(_, blueprint), obj| | ||
obj[blueprint.view_name] = View.new(blueprint) | ||
children = blueprint.views.except(:default) | ||
flattened_views(children, obj) | ||
end | ||
end | ||
|
||
# | ||
# Represents a view within a Blueprint. | ||
# | ||
class View | ||
# @return [Symbol] Name of the view | ||
attr_reader :name | ||
# @return [Hash<Symbol, TODO>] Fields defined on the view | ||
attr_reader :fields | ||
# @return [Hash<Symbol, TODO>] Associations defined on the view | ||
attr_reader :associations | ||
|
||
def initialize(blueprint) | ||
@name = blueprint.view_name | ||
@fields = {} # TODO: get non-association fields from blueprint.fields | ||
@associations = {} # TODO: get association fields from blueprint.fields | ||
end | ||
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
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,23 @@ | ||
# frozen_string_literal: true | ||
|
||
describe "Blueprinter::V2 Reflection" do | ||
it "should find all view names" do | ||
blueprint = Class.new(Blueprinter::V2) do | ||
view :foo | ||
view :bar do | ||
view :foo do | ||
view :borp | ||
end | ||
end | ||
end | ||
|
||
view_names = blueprint.reflections.keys | ||
expect(view_names.sort).to eq %i( | ||
default | ||
foo | ||
bar | ||
bar.foo | ||
bar.foo.borp | ||
).sort | ||
end | ||
end |