Sha256: e1757489f0fb4dfc80959da435c49e2bcab60f694592a66ed87489ed1a0e0b68

Contents?: true

Size: 1.43 KB

Versions: 3

Compression:

Stored size: 1.43 KB

Contents

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
    alias eql? ==

    # @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)
      unwrap.attributes[key]
    end

    # @return [Hash]
    def attributes
      @context.dependency_tracker.bounce(unwrap)
      unwrap.attributes
    end

    # @see Hash#fetch
    def fetch(key, fallback = NONE, &_block)
      @context.dependency_tracker.bounce(unwrap)

      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)
      unwrap.attributes.key?(key)
    end

    # @api private
    def reference
      unwrap.reference
    end

    # @api private
    def raw_content
      unwrap.content.string
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nanoc-4.2.1 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.2.0 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.2.0b1 lib/nanoc/base/views/mixins/document_view_mixin.rb