123456789_123456789_123456789_123456789_123456789_

Class: ActionView::PathSet

Do not use. This class is for internal use only.
Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
Inherits: Object
Defined in: actionview/lib/action_view/path_set.rb

Overview

This class is used to store and access paths in Action View. A number of operations are defined so that you can search among the paths in this set and also perform operations on other PathSet objects.

A LookupContext will use a PathSet to store the paths in its context.

Constant Summary

Class Method Summary

Instance Attribute Summary

::Enumerable - Included

#many?

Returns true if the enumerable has more than 1 element.

Instance Method Summary

::Enumerable - Included

#compact_blank

Returns a new ::Array without the blank items.

#exclude?

The negative of the Enumerable#include?.

#excluding

Returns a copy of the enumerable excluding the specified elements.

#in_order_of

Returns a new ::Array where the order has been set to that provided in the series, based on the key of the objects in the original enumerable.

#including

Returns a new array that includes the passed elements.

#index_by

Convert an enumerable to a hash, using the block result as the key and the element as the value.

#index_with

Convert an enumerable to a hash, using the element as the key and the block result as the value.

#maximum

Calculates the maximum from the extracted elements.

#minimum

Calculates the minimum from the extracted elements.

#pick

Extract the given key from the first element in the enumerable.

#pluck

Extract the given key from each element in the enumerable.

#sole

Returns the sole item in the enumerable.

#without
#as_json

::ActiveSupport::EnumerableCoreExt::Constants - Included

Constructor Details

.new(paths = []) ⇒ PathSet

[ GitHub ]

  
# File 'actionview/lib/action_view/path_set.rb', line 20

def initialize(paths = [])
  @paths = typecast(paths).freeze
end

Instance Attribute Details

#each (readonly)

[ GitHub ]

  
# File 'actionview/lib/action_view/path_set.rb', line 18

delegate :[], :include?, :size, :each, to: :paths

#paths (readonly)

[ GitHub ]

  
# File 'actionview/lib/action_view/path_set.rb', line 16

attr_reader :paths

#size (readonly)

[ GitHub ]

  
# File 'actionview/lib/action_view/path_set.rb', line 18

delegate :[], :include?, :size, :each, to: :paths

Instance Method Details

#+(other)

[ GitHub ]

  
# File 'actionview/lib/action_view/path_set.rb', line 37

def +(other)
  array = Array === other ? other : other.paths
  PathSet.new(paths + array)
end

#[]

[ GitHub ]

  
# File 'actionview/lib/action_view/path_set.rb', line 18

delegate :[], :include?, :size, :each, to: :paths

#compact

[ GitHub ]

  
# File 'actionview/lib/action_view/path_set.rb', line 33

def compact
  PathSet.new paths.compact
end

#exists?(path, prefixes, partial, details, details_key, locals) ⇒ Boolean

[ GitHub ]

  
# File 'actionview/lib/action_view/path_set.rb', line 60

def exists?(path, prefixes, partial, details, details_key, locals)
  !find(path, prefixes, partial, details, details_key, locals).nil?
end

#find(path, prefixes, partial, details, details_key, locals)

[ GitHub ]

  
# File 'actionview/lib/action_view/path_set.rb', line 42

def find(path, prefixes, partial, details, details_key, locals)
  search_combinations(path, prefixes, paths) do |resolver, name, prefix|
    resolver.find(name, prefix, partial, details, details_key, locals)
  end
end

#find!(path, prefixes, partial, details, details_key, locals)

[ GitHub ]

  
# File 'actionview/lib/action_view/path_set.rb', line 48

def find!(path, prefixes, partial, details, details_key, locals)
  find(path, prefixes, partial, details, details_key, locals) ||
    raise(missing_template(path, prefixes, partial, details, details_key, locals))
end

#find_all(path, prefixes, partial, details, details_key, locals)

[ GitHub ]

  
# File 'actionview/lib/action_view/path_set.rb', line 53

def find_all(path, prefixes, partial, details, details_key, locals)
  search_combinations(path, prefixes, paths) do |resolver, name, prefix|
    templates = resolver.find_all(name, prefix, partial, details, details_key, locals)
    templates unless templates.empty?
  end || []
end

#include?Boolean

[ GitHub ]

  
# File 'actionview/lib/action_view/path_set.rb', line 18

delegate :[], :include?, :size, :each, to: :paths

#initialize_copy(other)

[ GitHub ]

  
# File 'actionview/lib/action_view/path_set.rb', line 24

def initialize_copy(other)
  @paths = other.paths.dup.freeze
  self
end

#missing_template(path, prefixes, partial, details, details_key, locals) (private)

[ GitHub ]

  
# File 'actionview/lib/action_view/path_set.rb', line 106

def missing_template(path, prefixes, partial, details, details_key, locals)
  name = nil
  searched = []
  # A single placeholder resolver, so each prefix is listed once.
  search_combinations(path, prefixes, [nil]) do |_resolver, normalized, prefix|
    name = normalized
    searched << prefix
    nil
  end
  MissingTemplate.new(self, name, searched, partial, details, details_key, locals)
end

#search_combinations(path, prefixes, resolvers) (private)

Combines each prefix as it is yielded rather than up front, since callers stop at the first hit.

[ GitHub ]

  
# File 'actionview/lib/action_view/path_set.rb', line 67

def search_combinations(path, prefixes, resolvers)
  path = path.to_s
  idx = path.rindex("/")
  prefixes = Array(prefixes)

  if idx
    path_prefix = path[0, idx]
    path_prefix = path_prefix.from(1) if path_prefix.start_with?("/")
    name = path.from(idx + 1)
  else
    name = path
  end

  i = 0
  count = prefixes.empty? ? 1 : prefixes.length

  while i < count
    prefix =
      if prefixes.empty?
        path_prefix || ""
      elsif path_prefix
        "#{prefixes[i]}/#{path_prefix}"
      else
        prefixes[i]
      end

    j = 0
    while j < resolvers.length
      found = yield resolvers[j], name, prefix
      return found if found
      j += 1
    end

    i += 1
  end

  nil
end

#to_ary

[ GitHub ]

  
# File 'actionview/lib/action_view/path_set.rb', line 29

def to_ary
  paths.dup
end

#typecast(paths) (private)

[ GitHub ]

  
# File 'actionview/lib/action_view/path_set.rb', line 118

def typecast(paths)
  paths.map do |path|
    case path
    when Pathname, String
      # This path should only be reached by "direct" users of
      # ActionView::Base (not using the ViewPaths or Renderer modules).
      # We can't cache/de-dup the file system resolver in this case as we
      # don't know which compiled_method_container we'll be rendering to.
      FileSystemResolver.new(path)
    when Resolver
      path
    else
      raise TypeError, "#{path.inspect} is not a valid path: must be a String, Pathname, or Resolver"
    end
  end
end