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, local: false, no_output: nil, **opts) ⇒ Definition

Returns a new instance of Definition.



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

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

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

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



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

def default
  @default
end

Instance Method Details

#canonical_default(value) ⇒ Object



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

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)


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

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

#duplicate_defaultObject



229
230
231
232
233
234
# File 'lib/params_ready/parameter/definition.rb', line 229

def duplicate_default
  return Extensions::Undefined unless default_defined?
  return nil if @default.nil?

  duplicate_value(@default)
end

#finishObject



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/params_ready/parameter/definition.rb', line 237

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

  if @preprocessor && @local
    raise ParamsReadyError, "Preprocessor set for local parameter '#{name}'"
  end

  if @postprocessor && @local
    raise ParamsReadyError, "Postprocessor set for local parameter '#{name}'"
  end

  super
end

#local?(format) ⇒ Boolean

Returns:

  • (Boolean)


185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/params_ready/parameter/definition.rb', line 185

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

#memoize?Boolean

Returns:

  • (Boolean)


132
133
134
135
136
# File 'lib/params_ready/parameter/definition.rb', line 132

def memoize?
  return false if @memoize.nil?

  @memoize > 0
end

#name_for_formatterObject



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

def name_for_formatter
  self.class.name_for_formatter
end

#no_output?(format) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#postprocess(param, context, validator) ⇒ Object



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

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



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

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

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



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

def set_local(*arr, rule: nil)
  if rule.nil?
    @local = true
  else
    @local = Helpers::Rule(rule)
  end
  set_default *arr unless arr.empty?
end

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



143
144
145
146
# File 'lib/params_ready/parameter/definition.rb', line 143

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



138
139
140
141
# File 'lib/params_ready/parameter/definition.rb', line 138

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