Sha256: ddf3fa7e15f45716cbe8ddece01c328b292d5af71011f5d87b658d9ff9a72398

Contents?: true

Size: 1.83 KB

Versions: 15

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

module Nanoc
  module DocumentViewMixin
    # @api private
    NONE = Object.new.freeze

    # @api private
    def initialize(document, context)
      super(context)
      @document = document
    end

    # @api private
    def _unwrap
      @document
    end

    # @see Object#==
    def ==(other)
      other.respond_to?(:identifier) && identifier == other.identifier
    end

    # @see Object#eql?
    def eql?(other)
      other.is_a?(self.class) && identifier.eql?(other.identifier)
    end

    # @see Object#hash
    def hash
      self.class.hash ^ identifier.hash
    end

    # @return [Nanoc::Identifier]
    def identifier
      _unwrap.identifier
    end

    # @see Hash#[]
    def [](key)
      @context.dependency_tracker.bounce(_unwrap, attributes: [key])
      _unwrap.attributes[key]
    end

    # @return [Hash]
    def attributes
      # TODO: Refine dependencies
      @context.dependency_tracker.bounce(_unwrap, attributes: true)
      _unwrap.attributes
    end

    # @see Hash#fetch
    def fetch(key, fallback = NONE, &_block)
      @context.dependency_tracker.bounce(_unwrap, attributes: [key])

      if _unwrap.attributes.key?(key)
        _unwrap.attributes[key]
      elsif !fallback.equal?(NONE)
        fallback
      elsif block_given?
        yield(key)
      else
        raise KeyError, "key not found: #{key.inspect}"
      end
    end

    # @see Hash#key?
    def key?(key)
      @context.dependency_tracker.bounce(_unwrap, attributes: [key])
      _unwrap.attributes.key?(key)
    end

    # @api private
    def reference
      _unwrap.reference
    end

    # @api private
    def raw_content
      @context.dependency_tracker.bounce(_unwrap, raw_content: true)
      _unwrap.content.string
    end

    def inspect
      "<#{self.class} identifier=#{_unwrap.identifier}>"
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
nanoc-4.11.0 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.10.4 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.10.3 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.10.2 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.10.1 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.10.0 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.9.9 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.9.8 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.9.7 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.9.6 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.9.5 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.9.4 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.9.3 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.9.2 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.9.1 lib/nanoc/base/views/mixins/document_view_mixin.rb