Class: Paramoid::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/paramoid/object.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, alias_name, nested: nil, transformer: nil, default: nil, required: false) ⇒ Object

Returns a new instance of Object.

Parameters:

  • name (Symbol)
  • alias_name (Symbol)
  • nested (Object, NilClass) (defaults to: nil)
  • default (Object) (defaults to: nil)
  • transformer (Lambda, NilClass) (defaults to: nil)
  • required (TrueClass, FalseClass) (defaults to: false)


22
23
24
25
26
27
28
29
# File 'lib/paramoid/object.rb', line 22

def initialize(name, alias_name, nested: nil, transformer: nil, default: nil, required: false)
  @name = name
  @alias = alias_name
  @nested = nested
  @default = default
  @transformer = transformer
  @required = required
end

Instance Attribute Details

#alias_nameSymbol (readonly)

Returns the parameter alias.

Returns:

  • (Symbol)

    the parameter alias



6
7
8
# File 'lib/paramoid/object.rb', line 6

def alias_name
  @alias_name
end

#defaultObject (readonly)

Returns the parameter default value.

Returns:

  • (Object)

    the parameter default value



10
11
12
# File 'lib/paramoid/object.rb', line 10

def default
  @default
end

#nameSymbol (readonly)

Returns the parameter name.

Returns:

  • (Symbol)

    the parameter name



4
5
6
# File 'lib/paramoid/object.rb', line 4

def name
  @name
end

#nestedObject (readonly)

Returns the parameter nested.

Returns:

  • (Object)

    the parameter nested



8
9
10
# File 'lib/paramoid/object.rb', line 8

def nested
  @nested
end

#requiredTrueClass, FalseClass (readonly)

Returns required.

Returns:

  • (TrueClass, FalseClass)

    required



14
15
16
# File 'lib/paramoid/object.rb', line 14

def required
  @required
end

#transformerLambda, NilClass (readonly)

Returns the transformer.

Returns:

  • (Lambda, NilClass)

    the transformer



12
13
14
# File 'lib/paramoid/object.rb', line 12

def transformer
  @transformer
end

Instance Method Details

#<<(value) ⇒ Object



86
87
88
# File 'lib/paramoid/object.rb', line 86

def <<(value)
  @nested << value
end

#ensure_required_params!(params) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/paramoid/object.rb', line 62

def ensure_required_params!(params)
  if @required
    raise ActionController::ParameterMissing, output_key unless params&.key?(output_key)
    raise ActionController::ParameterMissing, output_key if params[output_key].nil?
  end

  @nested.ensure_required_params!(params[output_key]) if nested?

  params
end

#nested?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/paramoid/object.rb', line 90

def nested?
  !@nested.nil?
end

#output_keyObject



82
83
84
# File 'lib/paramoid/object.rb', line 82

def output_key
  @output_key ||= (@alias || @name).to_s
end

#to_defaultsObject



73
74
75
76
77
78
79
80
# File 'lib/paramoid/object.rb', line 73

def to_defaults
  if nested?
    nested_defaults = @nested.to_defaults
    (nested_defaults.present? ? { output_key => @nested.to_defaults } : {}).with_indifferent_access
  else
    (@default ? { output_key => @default } : {}).with_indifferent_access
  end
end

#to_paramsObject



46
47
48
49
50
51
52
# File 'lib/paramoid/object.rb', line 46

def to_params
  if nested?
    { @name.to_sym => @nested.to_params }
  else
    @name
  end
end

#to_required_paramsObject



54
55
56
57
58
59
60
# File 'lib/paramoid/object.rb', line 54

def to_required_params
  if nested?
    @nested.to_required_params
  else
    @required ? [@name] : []
  end
end

#transform_params!(params) ⇒ Object

Parameters:

  • params (Array, Hash)


32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/paramoid/object.rb', line 32

def transform_params!(params)
  return if @alias == @name

  if params.is_a?(Array)
    params.each { |param| transform_params!(param) }
    return
  end

  return unless params.key?(@name)

  @nested.transform_params!(params[@name]) if nested?
  params[@alias] = params.delete(@name) unless @alias == @name
end