Sha256: 56af4d12a37b27790385629dbbe94cbcd719e7a62f534d1d1dddddca7b2ef8b5

Contents?: true

Size: 1.67 KB

Versions: 9

Compression:

Stored size: 1.67 KB

Contents

# Needed for `thread_mattr_accessor`
require 'active_support/core_ext/module/attribute_accessors_per_thread'
require 'inertia_rails/lazy'

module InertiaRails
  thread_mattr_accessor :threadsafe_shared_plain_data
  thread_mattr_accessor :threadsafe_shared_blocks

  def self.configure
    yield(Configuration)
  end

  # "Getters"
  def self.shared_data(controller)
    shared_plain_data.merge!(evaluated_blocks(controller, shared_blocks))
  end

  def self.version
    Configuration.evaluated_version
  end

  def self.layout
    Configuration.layout
  end

  # "Setters"
  def self.share(**args)
    self.shared_plain_data = self.shared_plain_data.merge(args)
  end

  def self.share_block(block)
    self.shared_blocks = self.shared_blocks + [block]
  end

  def self.reset!
    self.shared_plain_data = {}
    self.shared_blocks = []
  end

  def self.lazy(value = nil, &block)
    InertiaRails::Lazy.new(value, &block)
  end

  private

  module Configuration
    mattr_accessor(:layout) { 'application' }
    mattr_accessor(:version) { nil }

    def self.evaluated_version
      self.version.respond_to?(:call) ? self.version.call : self.version
    end
  end

  # Getters and setters to provide default values for the threadsafe attributes
  def self.shared_plain_data
    self.threadsafe_shared_plain_data || {}
  end

  def self.shared_plain_data=(val)
    self.threadsafe_shared_plain_data = val
  end

  def self.shared_blocks
    self.threadsafe_shared_blocks || []
  end

  def self.shared_blocks=(val)
    self.threadsafe_shared_blocks = val
  end

  def self.evaluated_blocks(controller,  blocks)
    blocks.flat_map { |block| controller.instance_exec(&block) }.reduce(&:merge) || {}
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
inertia_rails-1.11.1 lib/inertia_rails/inertia_rails.rb
inertia_rails-1.11.0 lib/inertia_rails/inertia_rails.rb
inertia_rails-1.10.0 lib/inertia_rails/inertia_rails.rb
inertia_rails-1.9.2 lib/inertia_rails/inertia_rails.rb
inertia_rails-1.9.1 lib/inertia_rails/inertia_rails.rb
inertia_rails-1.9.0 lib/inertia_rails/inertia_rails.rb
inertia_rails-1.8.0 lib/inertia_rails/inertia_rails.rb
inertia_rails-1.7.1 lib/inertia_rails/inertia_rails.rb
inertia_rails-1.7.0 lib/inertia_rails/inertia_rails.rb