Class: SimpleParams::NestedParams

Inherits:
Params
  • Object
show all
Defined in:
lib/simple_params/nested_params.rb

Constant Summary

Constants included from HasTypedParams

HasTypedParams::TYPES

Instance Attribute Summary collapse

Attributes inherited from Params

#original_params

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Params

add_validations, api_pie_documentation, #define_attributes, #errors, model_name, nested_array, nested_arrays, nested_classes, nested_hash, nested_hashes, param, #to_hash

Methods included from Validations

#valid?, #validate!

Constructor Details

#initialize(params = {}, parent = nil, parent_attribute_name = nil) ⇒ NestedParams

Should allow NestedParams to be initialized with no arguments, in order

to be compatible with some Rails form gems like 'nested_form'


57
58
59
60
61
62
63
# File 'lib/simple_params/nested_params.rb', line 57

def initialize(params={}, parent = nil, parent_attribute_name = nil)
  @parent = parent
  @parent_attribute_name = parent_attribute_name
  @id = extract_id(params)
  @params = extract_initialization_params(params)
  super(@params)
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



53
54
55
# File 'lib/simple_params/nested_params.rb', line 53

def id
  @id
end

#paramsObject (readonly)

Returns the value of attribute params.



53
54
55
# File 'lib/simple_params/nested_params.rb', line 53

def params
  @params
end

#parentObject (readonly)

Returns the value of attribute parent.



53
54
55
# File 'lib/simple_params/nested_params.rb', line 53

def parent
  @parent
end

#parent_attribute_nameObject (readonly)

Returns the value of attribute parent_attribute_name.



53
54
55
# File 'lib/simple_params/nested_params.rb', line 53

def parent_attribute_name
  @parent_attribute_name
end

Class Method Details

.array?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/simple_params/nested_params.rb', line 8

def array?
  type.to_sym == :array
end

.build(params, parent, name) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/simple_params/nested_params.rb', line 32

def build(params, parent, name)
  if params.is_a?(Array)
    params.map { |p| build_instance(p, parent, name) }.compact
  elsif with_ids?
    params.each_pair.map { |key, val| build_instance({key => val}, parent, name) }.compact
  else
    build_instance(params, parent, name)
  end
end

.define_new_array_class(parent, name, options, &block) ⇒ Object



28
29
30
# File 'lib/simple_params/nested_params.rb', line 28

def define_new_array_class(parent, name, options, &block)
  define_new_class(parent, name, options.merge(type: :array), &block)
end

.define_new_hash_class(parent, name, options, &block) ⇒ Object



24
25
26
# File 'lib/simple_params/nested_params.rb', line 24

def define_new_hash_class(parent, name, options, &block)
  define_new_class(parent, name, options.merge(type: :hash), &block)
end

.hash?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/simple_params/nested_params.rb', line 12

def hash?
  type.to_sym == :hash
end

.optional?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/simple_params/nested_params.rb', line 20

def optional?
  !!options[:optional]
end

.typeObject



4
5
6
# File 'lib/simple_params/nested_params.rb', line 4

def type
  options[:type]
end

.with_ids?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/simple_params/nested_params.rb', line 16

def with_ids?
  !!options[:with_ids]
end

Instance Method Details

#destroyed?Boolean

Returns:

  • (Boolean)


65
66
67
68
# File 'lib/simple_params/nested_params.rb', line 65

def destroyed?
  sym_params = symbolize_params(params)
  [true, 1, "1", "true"].include?(sym_params[:_destroy])
end