Sha256: 115cdce120c2b4dc1fce33f5210e5368e49c35bbc76e823df474215f5800e6b0

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

require 'compo/finders/root'

module Compo
  module Mixins
    # Adds ID-based 'URL's to Compo classes
    #
    # For the purposes of this module, a URL is a string of slash-delimited IDs
    # representing the location of a Composite in the tree structure formed by
    # its ancestors.  Depending on the types of IDs used in the structure, the
    # URLs may not actually be literal Uniform Resource Locators.
    #
    # This module expects its includer to define #parent and #id.  These are
    # defined, for example, by the Compo::ParentTracker mixin.
    module UrlReferenceable
      extend Forwardable

      # Returns the URL of this object
      #
      # The #url of a Composite is defined inductively as '' for composites that
      # have no parent, and the joining of the parent URL and the current ID
      # otherwise.
      #
      # The result of #url can be used to give a URL hierarchy to Composites.
      #
      # @api  public
      # @example  Gets the URL of an object with no parent.
      #   orphan.url
      #   #=> ''
      # @example  Gets the URL of an object with a parent.
      #   leaf.url
      #   #=> '/grandparent_id/parent_id/id'
      #
      # @return [String]  The URL of this object.
      def url
        Compo::Finders::Root.new(self).reverse_each.map do |item|
          item.root? ? '' : item.id
        end.join('/')
      end

      # Returns the URL of this object's parent
      #
      # @api  public
      # @example  Gets the parent URL of an object with no parent.
      #   orphan.parent_url
      #   #=> ''
      # @example  Gets the URL of an object with a parent.
      #   leaf.parent_url
      #   #=> '/grandparent_id/parent_id'
      #
      # @return [String]  The URL of this object's parent, or nil if there is no
      #   parent.
      def_delegator :parent, :url, :parent_url
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
compo-0.5.1 lib/compo/mixins/url_referenceable.rb
compo-0.5.0 lib/compo/mixins/url_referenceable.rb