class ParamsReady::Restriction

Attributes

restriction[R]

Public Class Methods

blanket_permission() click to toggle source
# File lib/params_ready/restriction.rb, line 75
def self.blanket_permission
  @blanket_permission ||= Allowlist.new
end
from_array(arr) click to toggle source
# File lib/params_ready/restriction.rb, line 87
def self.from_array(arr)
  arr.each_with_object({}) do |element, restriction|
    case element
    when String, Symbol
      restriction[element.to_sym] = Everything
    when Hash
      element.each do |key, value|
        restriction[key.to_sym] = value
      end
    else
      raise TypeError.new("Unexpected as restriction item: #{element}")
    end
  end
end
instance(*list) click to toggle source
# File lib/params_ready/restriction.rb, line 102
def self.instance(*list)
  return blanket_permission if list.length == 1 && list[0] == default

  restriction_list = if list.length == 1 && list[0].is_a?(Regexp)
    list[0]
  else
    from_array(list)
  end
  new restriction_list
end
new(restriction = self.class.default) click to toggle source
# File lib/params_ready/restriction.rb, line 115
def initialize(restriction = self.class.default)
  @restriction = if restriction.is_a? self.class
    restriction.restriction
  else
    restriction.freeze
  end
  freeze
end
permit(*args) click to toggle source
# File lib/params_ready/restriction.rb, line 79
def self.permit(*args)
  Allowlist.instance(*args)
end
permit_all() click to toggle source
# File lib/params_ready/restriction.rb, line 185
def self.permit_all
  new default
end
prohibit(*args) click to toggle source
# File lib/params_ready/restriction.rb, line 83
def self.prohibit(*args)
  Denylist.instance(*args)
end

Public Instance Methods

delegate(parent, delegate_name, *others) click to toggle source
# File lib/params_ready/restriction.rb, line 152
def delegate(parent, delegate_name, *others)
  return self if everything?

  list = restriction_list_for(parent)

  self.class.instance({ delegate_name => list }, *others)
end
everything?() click to toggle source
# File lib/params_ready/restriction.rb, line 128
def everything?
  @restriction == self.class.default
end
for_children(parameter) click to toggle source
# File lib/params_ready/restriction.rb, line 160
def for_children(parameter)
  return self if everything?

  list = restriction_list_for(parameter)
  if list.is_a? Restriction
    list
  else
    self.class.instance(*list)
  end
end
hash() click to toggle source
# File lib/params_ready/restriction.rb, line 124
def hash
  @restriction.hash
end
name_listed?(name) click to toggle source
# File lib/params_ready/restriction.rb, line 132
def name_listed?(name)
  if @restriction.is_a? Regexp
    name =~ @restriction
  else
    @restriction.key?(name)
  end
end
permit_all() click to toggle source
# File lib/params_ready/restriction.rb, line 181
def permit_all
  self.class.permit_all
end
permitted?(parameter) click to toggle source
# File lib/params_ready/restriction.rb, line 140
def permitted?(parameter)
  name = parameter.name
  return false unless name_permitted?(name)
  return true unless parameter.respond_to? :permission_depends_on

  children = parameter.permission_depends_on
  intent = parameter.intent_for_children(self)
  children.all? do |child|
    intent.permitted?(child)
  end
end
restriction_list_for(parameter) click to toggle source
# File lib/params_ready/restriction.rb, line 171
def restriction_list_for(parameter)
  name = parameter.name
  raise ParamsReadyError, "Parameter '#{name}' not permitted" unless name_permitted? name
  restriction_list_for_name(name)
end
to_restriction() click to toggle source
# File lib/params_ready/restriction.rb, line 177
def to_restriction
  self
end