Sha256: a7bc2c4b51073c7d2cf8e32d19756217d568390966e3d8e1bad8135a518594d7

Contents?: true

Size: 1.15 KB

Versions: 20

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) == 'widget'
      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

20 entries across 20 versions & 1 rubygems

Version Path
scrivito_sdk-0.50.1 lib/scrivito/restriction_set.rb
scrivito_sdk-0.50.0 lib/scrivito/restriction_set.rb
scrivito_sdk-0.50.0.rc2 lib/scrivito/restriction_set.rb
scrivito_sdk-0.50.0.rc1 lib/scrivito/restriction_set.rb
scrivito_sdk-0.42.1 lib/scrivito/restriction_set.rb
scrivito_sdk-0.42.1.rc3 lib/scrivito/restriction_set.rb
scrivito_sdk-0.42.1.rc2 lib/scrivito/restriction_set.rb
scrivito_sdk-0.42.1.rc1 lib/scrivito/restriction_set.rb
scrivito_sdk-0.42.0 lib/scrivito/restriction_set.rb
scrivito_sdk-0.41.1 lib/scrivito/restriction_set.rb
scrivito_sdk-0.41.0 lib/scrivito/restriction_set.rb
scrivito_sdk-0.41.0.rc2 lib/scrivito/restriction_set.rb
scrivito_sdk-0.41.0.rc1 lib/scrivito/restriction_set.rb
scrivito_sdk-0.40.0 lib/scrivito/restriction_set.rb
scrivito_sdk-0.40.0.rc2 lib/scrivito/restriction_set.rb
scrivito_sdk-0.40.0.rc1 lib/scrivito/restriction_set.rb
scrivito_sdk-0.30.0 lib/scrivito/restriction_set.rb
scrivito_sdk-0.30.0.rc1 lib/scrivito/restriction_set.rb
scrivito_sdk-0.18.1 lib/scrivito/restriction_set.rb
scrivito_sdk-0.18.0 lib/scrivito/restriction_set.rb