module Milestoner::Views::ScopeBuilder

A scope builder which is meant to replace and fix the default scope builder.

Public Class Methods

call(name = nil, locals:, rendering: scope_for(name, rendering).new name:, locals:, rendering: end) click to toggle source
# File lib/milestoner/views/scope_builder.rb, line 9
  def self.call name = nil, locals:, rendering:
    scope_for(name, rendering).new name:, locals:, rendering:
  end

  def self.scope_for name, rendering
    case name
      in nil then rendering.config.scope_class
      in Class then name
      else fetch_or_store name, rendering
    end
  end

  def self.fetch_or_store name, rendering
    config = rendering.config

    Hanami::View.cache.fetch_or_store name, config do
      constant = constant_for name, rendering
      constant && constant < Hanami::View::Scope ? constant : config.scope_class
    end
  end

  def self.constant_for name, rendering
    name = rendering.inflector.camelize name.to_s
    namespace = rendering.config.scope_namespace || Object

    namespace.const_get name if namespace.const_defined? name, false
  end

  private_class_method :scope_for, :fetch_or_store, :constant_for
end
constant_for(name, rendering) click to toggle source
# File lib/milestoner/views/scope_builder.rb, line 30
def self.constant_for name, rendering
  name = rendering.inflector.camelize name.to_s
  namespace = rendering.config.scope_namespace || Object

  namespace.const_get name if namespace.const_defined? name, false
end
fetch_or_store(name, rendering) click to toggle source
# File lib/milestoner/views/scope_builder.rb, line 21
def self.fetch_or_store name, rendering
  config = rendering.config

  Hanami::View.cache.fetch_or_store name, config do
    constant = constant_for name, rendering
    constant && constant < Hanami::View::Scope ? constant : config.scope_class
  end
end
scope_for(name, rendering) click to toggle source
# File lib/milestoner/views/scope_builder.rb, line 13
def self.scope_for name, rendering
  case name
    in nil then rendering.config.scope_class
    in Class then name
    else fetch_or_store name, rendering
  end
end