Class: Blueprinter::ViewCollection Private

Inherits:
Object
  • Object
show all
Defined in:
lib/blueprinter/view_collection.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

ViewCollection manages the views defined in a Blueprint, along with their fields and transformers.

To improve performance, the “structure” of each view is cached. Once the first view is accessed, we prewarm the cache for all views (while this isn’t the most optimal approach, the overhead is negligible, and allows the caching logic to be quite simple).

Thread Safety: view data is lazily compiled into a frozen snapshot on first access, and is assigned atomically. A mutex serializes the compilation itself to prevent duplicate work. After compilation, reads bypass the mutex entirely.

Future optimization: a public compile! method could allow eager compilation at boot time (e.g. in a Rails after_initialize hook) to move the first-render compilation cost (~10µs per blueprint) out of the request path entirely.

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeViewCollection

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of ViewCollection.



25
26
27
28
29
30
31
32
33
# File 'lib/blueprinter/view_collection.rb', line 25

def initialize
  @views = {
    identifier: View.new(:identifier),
    default: View.new(:default)
  }
  @sort_by_definition = Blueprinter.configuration.sort_fields_by.eql?(:definition)
  @cache_mutex = Mutex.new
  @cache = nil
end

Instance Attribute Details

#sort_by_definitionObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



23
24
25
# File 'lib/blueprinter/view_collection.rb', line 23

def sort_by_definition
  @sort_by_definition
end

#viewsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



23
24
25
# File 'lib/blueprinter/view_collection.rb', line 23

def views
  @views
end

Instance Method Details

#[](view_name) ⇒ View

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • view_name (String)

Returns:



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/blueprinter/view_collection.rb', line 75

def [](view_name)
  return @views[view_name] if @views.key?(view_name)

  @cache_mutex.synchronize do
    unless @views.key?(view_name)
      @views[view_name] = View.new(view_name)
      invalidate_cache!
    end
    @views[view_name]
  end
end

#fields_for(view_name) ⇒ Array<Field>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns an array of Field objects for the provided View.

Parameters:

  • view_name (String)

Returns:



58
59
60
61
62
# File 'lib/blueprinter/view_collection.rb', line 58

def fields_for(view_name)
  ensure_cached!

  @cache[:fields][view_name]
end

#inherit(view_collection) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



35
36
37
38
39
40
41
# File 'lib/blueprinter/view_collection.rb', line 35

def inherit(view_collection)
  view_collection.views.each do |view_name, view|
    self[view_name].inherit(view)
  end

  invalidate_cache!
end

#invalidate_cache!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Clears the compiled field/transformer cache. This should be called after any mutation to a view’s fields, exclusions, or transformers to ensure the cache reflects the current state.



45
46
47
# File 'lib/blueprinter/view_collection.rb', line 45

def invalidate_cache!
  @cache = nil
end

#transformers(view_name) ⇒ Array<Transformer>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns an array of Transformer objects for the provided View.

Parameters:

  • view_name (String)

Returns:



67
68
69
70
71
# File 'lib/blueprinter/view_collection.rb', line 67

def transformers(view_name)
  ensure_cached!

  @cache[:transformers][view_name]
end

#view?(view_name) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns true if the view exists, false otherwise.

Parameters:

  • view_name (String)

Returns:

  • (Boolean)

    true if the view exists, false otherwise



51
52
53
# File 'lib/blueprinter/view_collection.rb', line 51

def view?(view_name)
  views.key? view_name
end