Sha256: 09bae0a71662576851c5619769c7d91b2a0e2f1c4ebf508a78332605bf637843

Contents?: true

Size: 1.97 KB

Versions: 4

Compression:

Stored size: 1.97 KB

Contents

module RailsConnector

  # 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

4 entries across 4 versions & 1 rubygems

Version Path
infopark_cloud_connector-7.1.0 lib/rails_connector/named_link.rb
infopark_cloud_connector-7.0.2 lib/rails_connector/named_link.rb
infopark_cloud_connector-7.0.1 lib/rails_connector/named_link.rb
infopark_cloud_connector-7.0.0 lib/rails_connector/named_link.rb