Sha256: c8c1bb4ab1abf6345c4c575288249ca10fc3134dc0ce57b7f89ac5f7918765bf

Contents?: true

Size: 1.82 KB

Versions: 15

Compression:

Stored size: 1.82 KB

Contents

module Lookbook
  # Base entity class
  #
  # @api public
  class Entity
    include Comparable
    send(:include, Lookbook::Engine.routes.url_helpers) # YARD parsing workaround: https://github.com/lsegal/yard/issues/546

    # @api private
    def initialize(lookup_path = nil)
      @lookup_path = lookup_path
    end

    # @!group Identity

    # Human-readable unique ID for the entity
    #
    # @return [String] The ID
    def id
      Utils.id(fetch_config(:id) { lookup_path.tr("/", "-") })
    end

    # Parameter-safe entity name.
    #
    # @return [String] The name
    def name
      @_name ||= Utils.name(File.basename(@lookup_path))
    end

    # Titlized name for use in navigation etc.
    #
    # Can be customized using the `@label` tag where supported.
    #
    # @return [String] The label
    def label
      @_label ||= fetch_config(:label) { name.titleize }
    end

    # Entity type identifier
    #
    # @return [Symbol] The entity type
    def type
      @_type ||= self.class.name.gsub("Entity", "").demodulize.underscore.downcase.to_sym
    end

    # @!endgroup

    # @!group Paths

    # Canonical reference path.
    #
    # Used for generating URL paths and looking up entities.
    #
    # @return [String] The lookup path
    def lookup_path
      return @_lookup_path if @_lookup_path

      directory = fetch_config(:logical_path) do
        dir = File.dirname(@lookup_path)
        dir if !dir.start_with?(".")
      end
      @_lookup_path ||= PathUtils.strip_slashes(PathUtils.to_path(directory, name))
    end

    alias_method :path, :lookup_path
    deprecate path: :lookup_path, deprecator: Deprecation

    # @!endgroup

    def <=>(other)
      label <=> other.label
    end

    protected

    def fetch_config(key, fallback = nil, &block)
      Utils.value_or_fallback(nil, fallback, &block)
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
lookbook-2.0.5 lib/lookbook/entities/entity.rb
lookbook-2.0.4 lib/lookbook/entities/entity.rb
lookbook-2.0.3 lib/lookbook/entities/entity.rb
lookbook-2.0.2 lib/lookbook/entities/entity.rb
lookbook-2.0.1 lib/lookbook/entities/entity.rb
lookbook-2.0.0 lib/lookbook/entities/entity.rb
lookbook-2.0.0.rc.3 lib/lookbook/entities/entity.rb
lookbook-2.0.0.rc.2 lib/lookbook/entities/entity.rb
lookbook-2.0.0.rc.1 lib/lookbook/entities/entity.rb
lookbook-2.0.0.beta.9 lib/lookbook/entities/entity.rb
lookbook-2.0.0.beta.8 lib/lookbook/entities/entity.rb
lookbook-2.0.0.beta.7 lib/lookbook/entities/entity.rb
lookbook-2.0.0.beta.6 lib/lookbook/entities/entity.rb
lookbook-2.0.0.beta.5 lib/lookbook/entities/entity.rb
lookbook-2.0.0.beta.4 lib/lookbook/entities/entity.rb