Class: ParamsReady::Parameter::Definition

Inherits:
AbstractDefinition show all
Defined in:
lib/params_ready/parameter/definition.rb

Instance Attribute Summary collapse

Attributes inherited from AbstractDefinition

#altn, #name

Instance Method Summary collapse

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, default: Extensions::Undefined, optional: false, preprocessor: nil, populator: nil, postprocessor: nil, no_input: nil, no_output: nil, **opts) ⇒ Definition

Returns a new instance of Definition.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/params_ready/parameter/definition.rb', line 93

def initialize(
  *args,
  default: Extensions::Undefined,
  optional: false,
  preprocessor: nil,
  populator: nil,
  postprocessor: nil,
  no_input: nil,
  no_output: nil,
  **opts
)
  super *args, **opts
  @default = Extensions::Undefined
  @optional = optional
  @preprocessor = preprocessor
  @postprocessor = postprocessor
  @populator = populator
  @no_input = no_input
  @no_output = no_output

  set_default(default) unless default == Extensions::Undefined
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



85
86
87
# File 'lib/params_ready/parameter/definition.rb', line 85

def default
  @default
end

Instance Method Details

#canonical_default(value) ⇒ Object



122
123
124
125
126
127
# File 'lib/params_ready/parameter/definition.rb', line 122

def canonical_default(value)
  return value if value.nil?
  ensure_canonical value
rescue => e
  raise ParamsReadyError, "Invalid default: #{e.message}"
end

#default_defined?Boolean

Returns:

  • (Boolean)


116
117
118
119
120
# File 'lib/params_ready/parameter/definition.rb', line 116

def default_defined?
  return false unless defined? @default
  return false if @default == Extensions::Undefined
  true
end

#fetch_callable_defaultObject



236
237
238
239
240
241
242
# File 'lib/params_ready/parameter/definition.rb', line 236

def fetch_callable_default
  value = @default.call
  value = ensure_canonical(value)
  duplicate_value(value)
rescue StandardError => e
  raise ParamsReadyError, "Invalid default: #{e.message}"
end

#fetch_default(duplicate: true) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
# File 'lib/params_ready/parameter/definition.rb', line 224

def fetch_default(duplicate: true)
  return Extensions::Undefined unless default_defined?
  return nil if @default.nil?

  if @default.is_a?(Helpers::Callable)
    fetch_callable_default
  else
    return @default unless duplicate
    duplicate_value(@default)
  end
end

#finishObject



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/params_ready/parameter/definition.rb', line 245

def finish
  if @populator && !@no_input
    raise ParamsReadyError, "Populator set for input parameter '#{name}'"
  end

  if @preprocessor && @no_input == true
    raise ParamsReadyError, "Preprocessor set for no-input parameter '#{name}'"
  end

  if @postprocessor && @no_input == true
    raise ParamsReadyError, "Postprocessor set for no-input parameter '#{name}'"
  end

  super
end

#memoize?Boolean

Returns:

  • (Boolean)


134
135
136
137
138
# File 'lib/params_ready/parameter/definition.rb', line 134

def memoize?
  return false if @memoize.nil?

  @memoize > 0
end

#name_for_formatterObject



89
90
91
# File 'lib/params_ready/parameter/definition.rb', line 89

def name_for_formatter
  self.class.name_for_formatter
end

#no_input?(format) ⇒ Boolean

Returns:

  • (Boolean)


191
192
193
# File 'lib/params_ready/parameter/definition.rb', line 191

def no_input?(format)
  restricted_for_format?(@no_input, format)
end

#no_output?(format) ⇒ Boolean

Returns:

  • (Boolean)


195
196
197
# File 'lib/params_ready/parameter/definition.rb', line 195

def no_output?(format)
  restricted_for_format?(@no_output, format)
end

#postprocess(param, context, validator) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/params_ready/parameter/definition.rb', line 164

def postprocess(param, context, validator)
  return if @postprocessor.nil?
  return unless @postprocessor.perform?(!context.local?, context.name)
  @postprocessor.block.call param, context
rescue => error
  postprocessor_error = PostprocessorError.new(error)
  if validator.nil?
    raise postprocessor_error
  else
    validator.error! postprocessor_error
  end
  validator
end

#preprocess(input, context, validator) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/params_ready/parameter/definition.rb', line 150

def preprocess(input, context, validator)
  return input if @preprocessor.nil?
  return input unless @preprocessor.perform?(!context.local?, context.name)
  @preprocessor.block.call input, context, self
rescue => error
  preprocessor_error = PreprocessorError.new(error)
  if validator.nil?
    raise preprocessor_error
  else
    validator.error! preprocessor_error
    Extensions::Undefined
  end
end

#restricted_for_format?(rule, format) ⇒ Boolean

Returns:

  • (Boolean)


199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/params_ready/parameter/definition.rb', line 199

def restricted_for_format?(rule, format)
  case rule
  when nil, false
    false
  when true
    !format.local?
  when Helpers::Rule
    rule.include?(format.name)
  else
    raise ParamsReadyError, "Unexpected rule: #{rule}"
  end
end

#set_local(*arr, rule: nil) ⇒ Object



185
186
187
188
189
# File 'lib/params_ready/parameter/definition.rb', line 185

def set_local(*arr, rule: nil)
  rule = Helpers::Rule(rule)
  set_no_input(*arr, rule: rule)
  set_no_output(rule || true)
end

#set_no_input(*arr, rule: nil) ⇒ Object

Raises:



178
179
180
181
182
183
# File 'lib/params_ready/parameter/definition.rb', line 178

def set_no_input(*arr, rule: nil)
  @no_input = Helpers::Rule(rule) || true
  raise ParamsReadyError, "Default not expected: #{arr}" if rule == false

  set_default *arr unless arr.empty?
end

#set_postprocessor(rule: nil, &block) ⇒ Object



145
146
147
148
# File 'lib/params_ready/parameter/definition.rb', line 145

def set_postprocessor(rule: nil, &block)
  raise "Postprocessor already set in '#{name}'" unless @postprocessor.nil?
  @postprocessor = Helpers::ConditionalBlock.new(rule: rule, &block)
end

#set_preprocessor(rule: nil, &block) ⇒ Object



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

def set_preprocessor(rule: nil, &block)
  raise "Preprocesser already set in '#{name}'" unless @preprocessor.nil?
  @preprocessor = Helpers::ConditionalBlock.new(rule: rule, &block)
end