module Ecoportal module API class V2 class Page class PermissionFlags < Common::Content::DoubleModel class << self def new_doc { "can_restructure" => false, "can_configure" => false, "can_permission" => false, "can_create_actions" => false, "can_administrate_actions" => false, "subscribed" => false, "subscribed_to_tasks" => false } end end passboolean :can_restructure, :can_configure passboolean :can_permission, :can_create_actions, :can_administrate_actions passboolean :subscribed, :subscribed_to_tasks def reset! doc.merge!(self.class.new_doc) end # Quick config helper # @param conf [Symbol, Array] # - `:restructure` # - `:configure` # - `:can_permission` # - `:create_actions` # - `:admin_actions` # - `:subscribed` # - `:subscribed_to_tasks` def configure(*conf) conf.each_with_object([]) do |cnf, unused| if cnf.is_a?(Symbol) set_flag(cnf, true, residual: unused) elsif cnf.is_a?(Hash) cnf.each do |key, val| set_flag(key, val, residual: unused) end else raise ArgumentError.new("Expecting 'Symbol' or 'Hash'. Given: #{cnf.class}") end end.yield_self do |unused| raise ArgumentError.new("Unknown configuaration options #{unused}") unless unused.empty? end end private def set_flag(key, val = true, residual: []) case key when :restructure self.can_restructure = val when :configure self.can_configure = val when :can_permission self.can_permission = val when :create_actions self.can_create_actions = val when :admin_actions self.can_administrate_actions = val when :subscribed self.subscribed = val when :subscribed_to_tasks self.subscribed_to_tasks = val else residual.push(key) end end end end end end end