Sha256: 578070347008697cd8d13e7505b44f8afa2a0c7c56d6854cf15ef9d953191095

Contents?: true

Size: 1.96 KB

Versions: 29

Compression:

Stored size: 1.96 KB

Contents

module Scrivito

  # This class provides methods used to retrieve objects from CMS based an entry
  # in CMS of the obj_class <tt>NamedLink</tt>.
  # @api public
  class NamedLink

    # @api public
    class NotFound < StandardError
    end

    @@named_links_cache = nil

    # Generates a cache of named links based on the CMS objects related link list.
    # Raises exceptions if zero or more than one objects have this obj_class
    def self.generate_named_links_cache
      return if @@named_links_cache

      found_object = find_named_link_obj
      if found_object.nil?
        raise NotFound, "Couldn't find NamedLink CMS Object!"
      else
        @@named_links_cache = found_object.
          related_links.
          flatten(1).
          each_with_object({}) do |link_obj, temp|
            temp[link_obj.title] = link_obj.obj
          end
      end

      return nil
    end

    # This method will be called to retrieve the NamedLink {BasicObj Obj}.
    # By default it will look for the Obj at the path "_named_links".
    # Overwrite this method only if you know what you are doing.
    # @api public
    def self.find_named_link_obj
      BasicObj.find_by_path("/_named_links")
    end

    def self.cache_expiry_time=(value)
      raise "NamedLink.cache_expiry_time is deprecated. NamedLink no longer has a separate cache."
    end

    def self.cache_expired?
      true
    end

    # Returns the CMS object mapped to the given title or nil.
    # The title can be a string of symbol.
    # @api public
    def self.get_object(title, options = {})
      object = named_links[title.to_s]
      raise NotFound, "The NamedLink '#{title.to_s}' does not exist" if object.nil?
      object
    end

    def self.reset_cache
      @@named_links_cache = nil
    end

    def self.named_links
      reset_cache if cache_expired?
      generate_named_links_cache unless named_links_cache
      named_links_cache
    end

    def self.named_links_cache
      @@named_links_cache
    end

  end

end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
scrivito_sdk-0.60.0 lib/scrivito/named_link.rb
scrivito_sdk-0.60.0.rc2 lib/scrivito/named_link.rb
scrivito_sdk-0.50.1 lib/scrivito/named_link.rb
scrivito_sdk-0.60.0.rc1 lib/scrivito/named_link.rb
scrivito_sdk-0.50.0 lib/scrivito/named_link.rb
scrivito_sdk-0.50.0.rc2 lib/scrivito/named_link.rb
scrivito_sdk-0.50.0.rc1 lib/scrivito/named_link.rb
scrivito_sdk-0.42.1 lib/scrivito/named_link.rb
scrivito_sdk-0.42.1.rc3 lib/scrivito/named_link.rb
scrivito_sdk-0.42.1.rc2 lib/scrivito/named_link.rb
scrivito_sdk-0.42.1.rc1 lib/scrivito/named_link.rb
scrivito_sdk-0.42.0 lib/scrivito/named_link.rb
scrivito_sdk-0.41.1 lib/scrivito/named_link.rb
scrivito_sdk-0.41.0 lib/scrivito/named_link.rb
scrivito_sdk-0.41.0.rc2 lib/scrivito/named_link.rb
scrivito_sdk-0.41.0.rc1 lib/scrivito/named_link.rb
scrivito_sdk-0.40.0 lib/scrivito/named_link.rb
scrivito_sdk-0.40.0.rc2 lib/scrivito/named_link.rb
scrivito_sdk-0.40.0.rc1 lib/scrivito/named_link.rb
scrivito_sdk-0.30.0 lib/scrivito/named_link.rb