Class: ParamsReady::Parameter::ArrayParameterDefinition

Inherits:
Definition show all
Includes:
ArrayLike
Defined in:
lib/params_ready/parameter/array_parameter.rb

Direct Known Subclasses

Ordering::OrderingParameterDefinition

Defined Under Namespace

Modules: ArrayLike

Instance Attribute Summary

Attributes inherited from Definition

#default

Attributes inherited from AbstractDefinition

#altn, #name

Instance Method Summary collapse

Methods included from ArrayLike

#duplicate_value, #freeze_value

Methods inherited from Definition

#canonical_default, #default_defined?, #fetch_callable_default, #fetch_default, #memoize?, #name_for_formatter, #no_input?, #no_output?, #postprocess, #preprocess, #restricted_for_format?, #set_local, #set_no_input, #set_postprocessor, #set_preprocessor

Methods inherited from AbstractDefinition

#create, #from_hash, #from_input, #normalize_alternative_name, #parameter_class

Methods included from Extensions::Freezer

#freeze_variable, #freeze_variables, #variables_to_freeze

Methods included from Extensions::Finalizer

#obligatory, #obligatory!

Methods included from Extensions::ClassReaderWriter

#class_reader_writer

Methods included from Extensions::LateInit

#late_init

Methods included from Extensions::Collection

#collection

Methods included from Extensions::Freezer::InstanceMethods

#freeze

Constructor Details

#initialize(*args, prototype: nil, **options) ⇒ ArrayParameterDefinition

Returns a new instance of ArrayParameterDefinition.

Raises:



155
156
157
158
159
# File 'lib/params_ready/parameter/array_parameter.rb', line 155

def initialize(*args, prototype: nil, **options)
  raise ParamsReadyError, "Not a definition: #{prototype.class.name}" unless (prototype.is_a?(Definition) || prototype.nil?)
  @prototype = prototype
  super *args, **options
end

Instance Method Details

#ensure_canonical(array) ⇒ Object



173
174
175
176
177
178
# File 'lib/params_ready/parameter/array_parameter.rb', line 173

def ensure_canonical(array)
  raise "Not a canonical value, array expected, got: '#{array.class.name}'" unless array.is_a? Array
  context = Format.instance(:backend)
  value, _validator = try_canonicalize(array, context, nil, freeze: true)
  value
end

#finishObject



189
190
191
192
193
194
# File 'lib/params_ready/parameter/array_parameter.rb', line 189

def finish
  if compact? && (prototype&.default_defined?)
    raise ParamsReadyError, 'Prototype must not be optional nor have default in compact array parameter'
  end
  super
end

#set_default(args) ⇒ Object

Raises:



161
162
163
164
# File 'lib/params_ready/parameter/array_parameter.rb', line 161

def set_default(args)
  raise ParamsReadyError, "Can't set default before prototype has been defined" if @prototype.nil?
  super
end

#try_canonicalize(value, context, validator = nil, freeze: false) ⇒ Object



180
181
182
183
184
185
186
187
# File 'lib/params_ready/parameter/array_parameter.rb', line 180

def try_canonicalize(value, context, validator = nil, freeze: false)
  if freeze
    marshaller = marshallers.instance(Array)
    marshaller.canonicalize self, value, context, validator, freeze: freeze
  else
    super value, context, validator
  end
end