Class: ParamsReady::Restriction

Inherits:
Object
  • Object
show all
Defined in:
lib/params_ready/restriction.rb

Direct Known Subclasses

Allowlist, Denylist

Defined Under Namespace

Modules: Wrapper Classes: Allowlist, Denylist, Everything, Nothing

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(restriction = self.class.default) ⇒ Restriction

Returns a new instance of Restriction.



115
116
117
118
119
120
121
122
# 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

Instance Attribute Details

#restrictionObject (readonly)

Returns the value of attribute restriction.



113
114
115
# File 'lib/params_ready/restriction.rb', line 113

def restriction
  @restriction
end

Class Method Details

.blanket_permissionObject



75
76
77
# File 'lib/params_ready/restriction.rb', line 75

def self.blanket_permission
  @blanket_permission ||= Allowlist.new
end

.from_array(arr) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# 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

.permit(*args) ⇒ Object



79
80
81
# File 'lib/params_ready/restriction.rb', line 79

def self.permit(*args)
  Allowlist.instance(*args)
end

.permit_allObject



185
186
187
# File 'lib/params_ready/restriction.rb', line 185

def self.permit_all
  new default
end

.prohibit(*args) ⇒ Object



83
84
85
# File 'lib/params_ready/restriction.rb', line 83

def self.prohibit(*args)
  Denylist.instance(*args)
end

Instance Method Details

#delegate(parent, delegate_name, *others) ⇒ Object



152
153
154
155
156
157
158
# 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?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/params_ready/restriction.rb', line 128

def everything?
  @restriction == self.class.default
end

#for_children(parameter) ⇒ Object



160
161
162
163
164
165
166
167
168
169
# 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

#hashObject



124
125
126
# File 'lib/params_ready/restriction.rb', line 124

def hash
  @restriction.hash
end

#name_listed?(name) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
135
136
137
138
# 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_allObject



181
182
183
# File 'lib/params_ready/restriction.rb', line 181

def permit_all
  self.class.permit_all
end

#permitted?(parameter) ⇒ Boolean

Returns:

  • (Boolean)


140
141
142
143
144
145
146
147
148
149
150
# 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) ⇒ Object

Raises:



171
172
173
174
175
# 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_restrictionObject



177
178
179
# File 'lib/params_ready/restriction.rb', line 177

def to_restriction
  self
end