From e0801c7b51a06369f2add9ec68b9bf1691c451ca Mon Sep 17 00:00:00 2001 From: Jordan Hollinger Date: Mon, 15 Jul 2024 12:22:56 -0400 Subject: [PATCH] Add 'exclude' DSL method for excluding inherited fields Signed-off-by: Jordan Hollinger --- lib/blueprinter/v2.rb | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/blueprinter/v2.rb b/lib/blueprinter/v2.rb index 7f809efa..324f1542 100644 --- a/lib/blueprinter/v2.rb +++ b/lib/blueprinter/v2.rb @@ -44,6 +44,7 @@ def self.[](view) def self.view(name, &definition) raise Errors::InvalidBlueprint, "View name may not contain '.'" if name.to_s =~ /\./ + name = name.to_sym views[name] = Class.new(self) views[name].blueprint_name << name views[name].class_eval(&definition) if definition @@ -53,12 +54,26 @@ def self.view(name, &definition) # Define a field # rubocop:todo Lint/UnusedMethodArgument def self.field(name, options = {}) - fields[name] = 'TODO' + fields[name.to_sym] = 'TODO' end # Define an association def self.association(name, blueprint, options = {}) - fields[name] = 'TODO' + fields[name.to_sym] = 'TODO' + end + + # Exclude fields/associations + def self.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 def self.render(obj, options = {})