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