Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract Rendering Functionality and Remove BaseHelpers Module #476

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ Metrics/AbcSize:

Metrics/ParameterLists:
Max: 10

Naming/MemoizedInstanceVariableName:
EnforcedStyleForLeadingUnderscores: required
1 change: 1 addition & 0 deletions bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ require 'bundler/setup'
require 'blueprinter'

require 'irb'

IRB.start(__FILE__)
4 changes: 2 additions & 2 deletions lib/blueprinter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Blueprinter
class << self
# @return [Configuration]
def configuration
@configuration ||= Configuration.new
@_configuration ||= Configuration.new
end

def configure
Expand All @@ -20,7 +20,7 @@ def configure

# Resets global configuration.
def reset_configuration!
@configuration = nil
@_configuration = nil
end
end
end
770 changes: 349 additions & 421 deletions lib/blueprinter/base.rb

Large diffs are not rendered by default.

24 changes: 17 additions & 7 deletions lib/blueprinter/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,21 @@

module Blueprinter
class Configuration
attr_accessor :association_default, :datetime_format, :deprecations, :field_default, :generator, :if, :method,
:sort_fields_by, :unless, :extractor_default, :default_transformers, :custom_array_like_classes
attr_accessor(
:association_default,
:custom_array_like_classes,
:datetime_format,
:default_transformers,
:deprecations,
:extractor_default,
:field_default,
:generator,
:if,
:method,
:sort_fields_by,
:unless
)
attr_reader :extensions

VALID_CALLABLES = %i[if unless].freeze

Expand All @@ -24,18 +37,15 @@ def initialize
@extractor_default = AutoExtractor
@default_transformers = []
@custom_array_like_classes = []
end

def extensions
@extensions ||= Extensions.new
@extensions = Extensions.new
jhollinger marked this conversation as resolved.
Show resolved Hide resolved
end

def extensions=(list)
@extensions = Extensions.new(list)
end

def array_like_classes
@array_like_classes ||= [
@_array_like_classes ||= [
Array,
defined?(ActiveRecord::Relation) && ActiveRecord::Relation,
*custom_array_like_classes
Expand Down
2 changes: 1 addition & 1 deletion lib/blueprinter/errors/invalid_blueprint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Blueprinter
module Errors
class InvalidBlueprint < Blueprinter::BlueprinterError; end
class InvalidBlueprint < BlueprinterError; end
end
end
13 changes: 13 additions & 0 deletions lib/blueprinter/errors/invalid_root.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require 'blueprinter/blueprinter_error'

module Blueprinter
module Errors
class InvalidRoot < BlueprinterError
def initialize(message = 'root key must be a Symbol or a String')
super
end
end
end
end
13 changes: 13 additions & 0 deletions lib/blueprinter/errors/meta_requires_root.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require 'blueprinter/blueprinter_error'

module Blueprinter
module Errors
class MetaRequiresRoot < BlueprinterError
def initialize(message = 'adding metadata requires that a root key is set')
super
end
end
end
end
2 changes: 1 addition & 1 deletion lib/blueprinter/extractors/association_extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def extract(association_name, object, local_options, options = {})

view = options[:view] || :default
blueprint = association_blueprint(options[:blueprint], value)
blueprint.prepare(value, view_name: view, local_options: local_options)
blueprint.hashify(value, view_name: view, local_options: local_options)
end

private
Expand Down
8 changes: 2 additions & 6 deletions lib/blueprinter/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,11 @@ def skip?(field_name, object, local_options)
private

def if_callable
return @if_callable if defined?(@if_callable)

@if_callable = callable_from(:if)
@_if_callable ||= callable_from(:if)
end

def unless_callable
return @unless_callable if defined?(@unless_callable)

@unless_callable = callable_from(:unless)
@_unless_callable ||= callable_from(:unless)
end

def callable_from(condition)
Expand Down
104 changes: 0 additions & 104 deletions lib/blueprinter/helpers/base_helpers.rb

This file was deleted.

6 changes: 3 additions & 3 deletions lib/blueprinter/reflection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module Reflection
# @return [Hash<Symbol, Blueprinter::Reflection::View>]
#
def reflections
@reflections ||= view_collection.views.transform_values do |view|
@_reflections ||= view_collection.views.transform_values do |view|
View.new(view.name, view_collection)
end
end
Expand All @@ -45,7 +45,7 @@ def initialize(name, view_collection)
# @return [Hash<Symbol, Blueprinter::Reflection::Field>]
#
def fields
@fields ||= @view_collection.fields_for(name).each_with_object({}) do |field, obj|
@_fields ||= @view_collection.fields_for(name).each_with_object({}) do |field, obj|
next if field.options[:association]

obj[field.name] = Field.new(field.method, field.name, field.options)
Expand All @@ -58,7 +58,7 @@ def fields
# @return [Hash<Symbol, Blueprinter::Reflection::Association>]
#
def associations
@associations ||= @view_collection.fields_for(name).each_with_object({}) do |field, obj|
@_associations ||= @view_collection.fields_for(name).each_with_object({}) do |field, obj|
next unless field.options[:association]

blueprint = field.options.fetch(:blueprint)
Expand Down
Loading