Sha256: 20639d28b65ca885850ad22bf82df4e0ec6ff81c00b0b1a7ad49d888e0efe50c

Contents?: true

Size: 1.31 KB

Versions: 8

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

module Nanoc
  module MutableDocumentViewMixin
    # @api private
    class DisallowedAttributeValueError < Nanoc::Error
      attr_reader :value

      def initialize(value)
        @value = value
      end

      def message
        "The #{value.class} cannot be stored inside an attribute. Store its identifier instead."
      end
    end

    def raw_content=(arg)
      unwrap.content = Nanoc::Int::Content.create(arg)
    end

    # Sets the value for the given attribute.
    #
    # @param [Symbol] key
    #
    # @see Hash#[]=
    def []=(key, value)
      disallowed_value_classes = Set.new([
        Nanoc::Int::Item,
        Nanoc::Int::Layout,
        Nanoc::CompilationItemView,
        Nanoc::LayoutView,
      ])
      if disallowed_value_classes.include?(value.class)
        raise DisallowedAttributeValueError.new(value)
      end

      unwrap.set_attribute(key, value)
    end

    # Sets the identifier to the given argument.
    #
    # @param [String, Nanoc::Identifier] arg
    def identifier=(arg)
      unwrap.identifier = Nanoc::Identifier.from(arg)
    end

    # Updates the attributes based on the given hash.
    #
    # @param [Hash] hash
    #
    # @return [self]
    def update_attributes(hash)
      hash.each { |k, v| unwrap.set_attribute(k, v) }
      self
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
nanoc-4.9.0 lib/nanoc/base/views/mixins/mutable_document_view_mixin.rb
nanoc-4.8.19 lib/nanoc/base/views/mixins/mutable_document_view_mixin.rb
nanoc-4.8.18 lib/nanoc/base/views/mixins/mutable_document_view_mixin.rb
nanoc-4.8.17 lib/nanoc/base/views/mixins/mutable_document_view_mixin.rb
nanoc-4.8.16 lib/nanoc/base/views/mixins/mutable_document_view_mixin.rb
nanoc-4.8.15 lib/nanoc/base/views/mixins/mutable_document_view_mixin.rb
nanoc-4.8.14 lib/nanoc/base/views/mixins/mutable_document_view_mixin.rb
nanoc-4.8.13 lib/nanoc/base/views/mixins/mutable_document_view_mixin.rb