Class: ParamsReady::Parameter::ValueParameterDefinition

Inherits:
Definition show all
Extended by:
Forwardable
Includes:
ValueLike
Defined in:
lib/params_ready/parameter/value_parameter.rb

Defined Under Namespace

Modules: ValueLike

Instance Attribute Summary collapse

Attributes inherited from Definition

#default

Attributes inherited from AbstractDefinition

#altn, #name

Instance Method Summary collapse

Methods included from ValueLike

#duplicate_value, #freeze_value

Methods inherited from Definition

#canonical_default, #default_defined?, #fetch_callable_default, #fetch_default, #memoize?, #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(name, coder, *args, constraints: [], **options) ⇒ ValueParameterDefinition

Returns a new instance of ValueParameterDefinition.



147
148
149
150
151
# File 'lib/params_ready/parameter/value_parameter.rb', line 147

def initialize(name, coder, *args, constraints: [], **options)
  @coder = coder
  @constraints = constraints
  super name, *args, **options
end

Instance Attribute Details

#coderObject (readonly)

Returns the value of attribute coder.



140
141
142
# File 'lib/params_ready/parameter/value_parameter.rb', line 140

def coder
  @coder
end

Instance Method Details

#ensure_canonical(value) ⇒ Object



165
166
167
168
169
170
171
172
# File 'lib/params_ready/parameter/value_parameter.rb', line 165

def ensure_canonical(value)
  coerced = coder.try_coerce value, Format.instance(:backend)
  if coder.strict_default? && value != coerced
    raise ParamsReadyError, "input '#{value}'/#{value.class.name} (expected '#{coerced}'/#{coerced.class.name})"
  end
  validate coerced
  coerced
end

#finishObject



180
181
182
183
# File 'lib/params_ready/parameter/value_parameter.rb', line 180

def finish
  @coder.finish if @coder.respond_to?(:finish)
  super
end

#name_for_formatterObject



119
120
121
122
123
124
# File 'lib/params_ready/parameter/value_parameter.rb', line 119

def name_for_formatter
  coder_name = @coder.type_identifier if @coder.respond_to? :type_identifier
  return coder_name unless coder_name.nil?

  super
end

#try_canonicalize(input, context, validator = nil) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/params_ready/parameter/value_parameter.rb', line 153

def try_canonicalize(input, context, validator = nil)
  value = coder.try_coerce input, context
  return value if Extensions::Undefined.value_indefinite?(value)

  value, validator = validate value, validator
  if validator.nil? || validator.ok?
    [value.freeze, validator]
  else
    [nil, validator]
  end
end

#validate(value, validator = nil) ⇒ Object



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

def validate(value, validator = nil)
  constraints.reduce([value, validator]) do |(value, validator), constraint|
    constraint.validate value, validator
  end
end