Sha256: e4f4a98db810cf79f037b1599e4a602b7f2c08e8fd38048a6d8f8372eb79afa2

Contents?: true

Size: 1.15 KB

Versions: 118

Compression:

Stored size: 1.15 KB

Contents

module Scrivito
  class RestrictionSet
    Restriction = Struct.new(:attribute, :callable) do
      def call(obj)
        if skip?(obj)
          false
        else
          value = obj[attribute]

          callable.call(value)
        end
      end

      private

      def skip?(obj)
        !obj.has_attribute?(attribute) || obj.type_of_attribute(attribute) == 'widgetlist'
      end
    end

    def add(options, &block)
      options = options.with_indifferent_access
      attribute = options.fetch(:using) do
        raise ScrivitoError, 'No "using" option given when adding a publish restriction'
      end

      unless block_given?
        raise ScrivitoError, 'No block provided when adding a publish restriction'
      end

      restrictions << Restriction.new(attribute, block)
    end

    def restriction_messages_for(obj)
      restriction_messages = restrictions.inject([]) do |messages, restriction|
        message = restriction.call(obj)

        if message
          messages << message.to_s
        end

        messages
      end

      restriction_messages.uniq
    end

    private

    def restrictions
      @restrictions ||= []
    end
  end
end

Version data entries

118 entries across 118 versions & 1 rubygems

Version Path
scrivito_sdk-1.2.0.rc2 lib/scrivito/restriction_set.rb
scrivito_sdk-1.2.0.rc1 lib/scrivito/restriction_set.rb
scrivito_sdk-1.1.1 lib/scrivito/restriction_set.rb
scrivito_sdk-1.1.0 lib/scrivito/restriction_set.rb
scrivito_sdk-1.1.0.rc3 lib/scrivito/restriction_set.rb
scrivito_sdk-1.1.0.rc2 lib/scrivito/restriction_set.rb
scrivito_sdk-1.1.0.rc1 lib/scrivito/restriction_set.rb
scrivito_sdk-1.0.0 lib/scrivito/restriction_set.rb
scrivito_sdk-1.0.0.rc4 lib/scrivito/restriction_set.rb
scrivito_sdk-1.0.0.rc3 lib/scrivito/restriction_set.rb
scrivito_sdk-1.0.0.rc2 lib/scrivito/restriction_set.rb
scrivito_sdk-1.0.0.rc1 lib/scrivito/restriction_set.rb
scrivito_sdk-0.90.0 lib/scrivito/restriction_set.rb
scrivito_sdk-0.90.0.rc3 lib/scrivito/restriction_set.rb
scrivito_sdk-0.90.0.rc2 lib/scrivito/restriction_set.rb
scrivito_sdk-0.90.0.rc1 lib/scrivito/restriction_set.rb
scrivito_sdk-0.70.2 lib/scrivito/restriction_set.rb
scrivito_sdk-0.71.2 lib/scrivito/restriction_set.rb
scrivito_sdk-0.71.1 lib/scrivito/restriction_set.rb
scrivito_sdk-0.70.1 lib/scrivito/restriction_set.rb