Class: RubyPitaya::Parameters

Inherits:
Object
  • Object
show all
Defined in:
lib/rubypitaya/core/parameters.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parameters = {}) ⇒ Parameters

Returns a new instance of Parameters.



75
76
77
78
# File 'lib/rubypitaya/core/parameters.rb', line 75

def initialize(parameters = {})
  @parameters = parameters.with_indifferent_access
  @permitted = self.class.permit_all_parameters
end

Class Method Details

.hook_into_yaml_loadingObject



333
334
335
336
# File 'lib/rubypitaya/core/parameters.rb', line 333

def self.hook_into_yaml_loading
  YAML.load_tags["!ruby/hash-with-ivars:ActionController::Parameters"] = name
  YAML.load_tags["!ruby/hash:ActionController::Parameters"] = name
end

.nested_attribute?(key, value) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


70
71
72
# File 'lib/rubypitaya/core/parameters.rb', line 70

def nested_attribute?(key, value) # :nodoc:
  /\A-?\d+\z/.match?(key) && (value.is_a?(Hash) || value.is_a?(Parameters))
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



80
81
82
83
84
85
86
# File 'lib/rubypitaya/core/parameters.rb', line 80

def ==(other)
  if other.respond_to?(:permitted?)
    permitted? == other.permitted? && parameters == other.parameters
  else
    @parameters == other
  end
end

#[](key) ⇒ Object



182
183
184
# File 'lib/rubypitaya/core/parameters.rb', line 182

def [](key)
  convert_hashes_to_parameters(key, @parameters[key])
end

#[]=(key, value) ⇒ Object



186
187
188
# File 'lib/rubypitaya/core/parameters.rb', line 186

def []=(key, value)
  @parameters[key] = value
end

#compactObject



281
282
283
# File 'lib/rubypitaya/core/parameters.rb', line 281

def compact
  new_instance_with_inherited_permitted_status(@parameters.compact)
end

#compact!Object



285
286
287
# File 'lib/rubypitaya/core/parameters.rb', line 285

def compact!
  self if @parameters.compact!
end

#compact_blankObject



289
290
291
# File 'lib/rubypitaya/core/parameters.rb', line 289

def compact_blank
  reject { |_k, v| v.blank? }
end

#compact_blank!Object



293
294
295
# File 'lib/rubypitaya/core/parameters.rb', line 293

def compact_blank!
  reject! { |_k, v| v.blank? }
end

#converted_arraysObject



134
135
136
# File 'lib/rubypitaya/core/parameters.rb', line 134

def converted_arrays
  @converted_arrays ||= Set.new
end

#deep_dupObject



352
353
354
355
356
# File 'lib/rubypitaya/core/parameters.rb', line 352

def deep_dup
  self.class.new(@parameters.deep_dup).tap do |duplicate|
    duplicate.permitted = @permitted
  end
end

#deep_transform_keys(&block) ⇒ Object



246
247
248
249
250
# File 'lib/rubypitaya/core/parameters.rb', line 246

def deep_transform_keys(&block)
  new_instance_with_inherited_permitted_status(
    @parameters.deep_transform_keys(&block)
  )
end

#deep_transform_keys!(&block) ⇒ Object



252
253
254
255
# File 'lib/rubypitaya/core/parameters.rb', line 252

def deep_transform_keys!(&block)
  @parameters.deep_transform_keys!(&block)
  self
end

#delete(key, &block) ⇒ Object



257
258
259
# File 'lib/rubypitaya/core/parameters.rb', line 257

def delete(key, &block)
  convert_value_to_parameters(@parameters.delete(key, &block))
end

#dig(*keys) ⇒ Object



202
203
204
205
# File 'lib/rubypitaya/core/parameters.rb', line 202

def dig(*keys)
  convert_hashes_to_parameters(keys.first, @parameters[keys.first])
  @parameters.dig(*keys)
end

#each_pair(&block) ⇒ Object Also known as: each



115
116
117
118
119
120
121
122
# File 'lib/rubypitaya/core/parameters.rb', line 115

def each_pair(&block)
  return to_enum(__callee__) unless block_given?
  @parameters.each_pair do |key, value|
    yield [key, convert_hashes_to_parameters(key, value)]
  end

  self
end

#each_value(&block) ⇒ Object



125
126
127
128
129
130
131
132
# File 'lib/rubypitaya/core/parameters.rb', line 125

def each_value(&block)
  return to_enum(:each_value) unless block_given?
  @parameters.each_pair do |key, value|
    yield convert_hashes_to_parameters(key, value)
  end

  self
end

#except(*keys) ⇒ Object



212
213
214
# File 'lib/rubypitaya/core/parameters.rb', line 212

def except(*keys)
  new_instance_with_inherited_permitted_status(@parameters.except(*keys))
end

#extract!(*keys) ⇒ Object



216
217
218
# File 'lib/rubypitaya/core/parameters.rb', line 216

def extract!(*keys)
  new_instance_with_inherited_permitted_status(@parameters.extract!(*keys))
end

#fetch(key, *args) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
# File 'lib/rubypitaya/core/parameters.rb', line 190

def fetch(key, *args)
  convert_value_to_parameters(
    @parameters.fetch(key) {
      if block_given?
        yield
      else
        args.fetch(0) { raise ActionController::ParameterMissing.new(key, @parameters.keys) }
      end
    }
  )
end

#hashObject



89
90
91
# File 'lib/rubypitaya/core/parameters.rb', line 89

def hash
  [@parameters.hash, @permitted].hash
end

#init_with(coder) ⇒ Object



339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/rubypitaya/core/parameters.rb', line 339

def init_with(coder)
  case coder.tag
  when "!ruby/hash:ActionController::Parameters"
    @parameters = coder.map.with_indifferent_access
    @permitted  = false
  when "!ruby/hash-with-ivars:ActionController::Parameters"
    @parameters = coder.map["elements"].with_indifferent_access
    @permitted  = coder.map["ivars"][:@permitted]
  when "!ruby/object:ActionController::Parameters"
    @parameters, @permitted = coder.map["parameters"], coder.map["permitted"]
  end
end

#inspectObject



329
330
331
# File 'lib/rubypitaya/core/parameters.rb', line 329

def inspect
  "#<#{self.class} #{@parameters} permitted: #{@permitted}>"
end

#merge(other_hash) ⇒ Object



301
302
303
304
305
# File 'lib/rubypitaya/core/parameters.rb', line 301

def merge(other_hash)
  new_instance_with_inherited_permitted_status(
    @parameters.merge(other_hash.to_h)
  )
end

#merge!(other_hash) ⇒ Object



307
308
309
310
# File 'lib/rubypitaya/core/parameters.rb', line 307

def merge!(other_hash)
  @parameters.merge!(other_hash.to_h)
  self
end

#permit(*filters) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/rubypitaya/core/parameters.rb', line 165

def permit(*filters)
  params = self.class.new

  filters.flatten.each do |filter|
    case filter
    when Symbol, String
      permitted_scalar_filter(params, filter)
    when Hash
      hash_filter(params, filter)
    end
  end

  unpermitted_parameters!(params) if self.class.action_on_unpermitted_parameters

  params.permit!
end

#permit!Object



142
143
144
145
146
147
148
149
150
151
# File 'lib/rubypitaya/core/parameters.rb', line 142

def permit!
  each_pair do |key, value|
    Array.wrap(value).flatten.each do |v|
      v.permit! if v.respond_to? :permit!
    end
  end

  @permitted = true
  self
end

#permitted?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/rubypitaya/core/parameters.rb', line 138

def permitted?
  @permitted
end

#reject(&block) ⇒ Object



271
272
273
# File 'lib/rubypitaya/core/parameters.rb', line 271

def reject(&block)
  new_instance_with_inherited_permitted_status(@parameters.reject(&block))
end

#reject!(&block) ⇒ Object Also known as: delete_if



275
276
277
278
# File 'lib/rubypitaya/core/parameters.rb', line 275

def reject!(&block)
  @parameters.reject!(&block)
  self
end

#require(key) ⇒ Object Also known as: required



153
154
155
156
157
158
159
160
161
# File 'lib/rubypitaya/core/parameters.rb', line 153

def require(key)
  return key.map { |k| require(k) } if key.is_a?(Array)
  value = self[key]
  if value.present? || value == false
    value
  else
    raise ParameterMissing.new(key, @parameters.keys)
  end
end

#reverse_merge(other_hash) ⇒ Object Also known as: with_defaults



312
313
314
315
316
# File 'lib/rubypitaya/core/parameters.rb', line 312

def reverse_merge(other_hash)
  new_instance_with_inherited_permitted_status(
    other_hash.to_h.merge(@parameters)
  )
end

#reverse_merge!(other_hash) ⇒ Object Also known as: with_defaults!



319
320
321
322
# File 'lib/rubypitaya/core/parameters.rb', line 319

def reverse_merge!(other_hash)
  @parameters.merge!(other_hash.to_h) { |key, left, right| left }
  self
end

#select(&block) ⇒ Object



261
262
263
# File 'lib/rubypitaya/core/parameters.rb', line 261

def select(&block)
  new_instance_with_inherited_permitted_status(@parameters.select(&block))
end

#select!(&block) ⇒ Object Also known as: keep_if



265
266
267
268
# File 'lib/rubypitaya/core/parameters.rb', line 265

def select!(&block)
  @parameters.select!(&block)
  self
end

#slice!(*keys) ⇒ Object



207
208
209
210
# File 'lib/rubypitaya/core/parameters.rb', line 207

def slice!(*keys)
  @parameters.slice!(*keys)
  self
end

#stringify_keysObject



325
326
327
# File 'lib/rubypitaya/core/parameters.rb', line 325

def stringify_keys 
  dup
end

#to_hObject



93
94
95
96
97
98
99
# File 'lib/rubypitaya/core/parameters.rb', line 93

def to_h
  if permitted?
    convert_parameters_to_hashes(@parameters, :to_h)
  else
    raise UnfilteredParameters
  end
end

#to_hashObject



101
102
103
# File 'lib/rubypitaya/core/parameters.rb', line 101

def to_hash
  to_h.to_hash
end

#to_query(*args) ⇒ Object Also known as: to_param



105
106
107
# File 'lib/rubypitaya/core/parameters.rb', line 105

def to_query(*args)
  to_h.to_query(*args)
end

#to_unsafe_hObject Also known as: to_unsafe_hash



110
111
112
# File 'lib/rubypitaya/core/parameters.rb', line 110

def to_unsafe_h
  convert_parameters_to_hashes(@parameters, :to_unsafe_h)
end

#transform_keys(&block) ⇒ Object



233
234
235
236
237
238
# File 'lib/rubypitaya/core/parameters.rb', line 233

def transform_keys(&block)
  return to_enum(:transform_keys) unless block_given?
  new_instance_with_inherited_permitted_status(
    @parameters.transform_keys(&block)
  )
end

#transform_keys!(&block) ⇒ Object



240
241
242
243
244
# File 'lib/rubypitaya/core/parameters.rb', line 240

def transform_keys!(&block)
  return to_enum(:transform_keys!) unless block_given?
  @parameters.transform_keys!(&block)
  self
end

#transform_valuesObject



220
221
222
223
224
225
# File 'lib/rubypitaya/core/parameters.rb', line 220

def transform_values
  return to_enum(:transform_values) unless block_given?
  new_instance_with_inherited_permitted_status(
    @parameters.transform_values { |v| yield convert_value_to_parameters(v) }
  )
end

#transform_values!Object



227
228
229
230
231
# File 'lib/rubypitaya/core/parameters.rb', line 227

def transform_values!
  return to_enum(:transform_values!) unless block_given?
  @parameters.transform_values! { |v| yield convert_value_to_parameters(v) }
  self
end

#values_at(*keys) ⇒ Object



297
298
299
# File 'lib/rubypitaya/core/parameters.rb', line 297

def values_at(*keys)
  convert_value_to_parameters(@parameters.values_at(*keys))
end