Sha256: bd0908f87207fd6ca96fb09c06fbec45ee1876fb5c1208397f8f0eaaa8a672e1

Contents?: true

Size: 1.75 KB

Versions: 20

Compression:

Stored size: 1.75 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

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

    # @return [Hash]
    def attributes
      @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: true)

      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: true)
      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

20 entries across 20 versions & 1 rubygems

Version Path
nanoc-4.7.2 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.7.1 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.7.0 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.6.4 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.6.3 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.6.2 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.6.1 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.6.0 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.5.4 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.5.3 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.5.2 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.5.1 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.5.0 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.4.7 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.4.6 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.4.5 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.4.4 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.4.3 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.4.2 lib/nanoc/base/views/mixins/document_view_mixin.rb
nanoc-4.4.1 lib/nanoc/base/views/mixins/document_view_mixin.rb