Class: ParamsReady::Parameter::Definition
Instance Attribute Summary collapse
#altn, #name
Instance Method Summary
collapse
-
#canonical_default(value) ⇒ Object
-
#default_defined? ⇒ Boolean
-
#fetch_callable_default ⇒ Object
-
#fetch_default(duplicate: true) ⇒ Object
-
#finish ⇒ Object
-
#initialize(*args, default: Extensions::Undefined, optional: false, preprocessor: nil, populator: nil, postprocessor: nil, no_input: nil, no_output: nil, **opts) ⇒ Definition
constructor
A new instance of Definition.
-
#memoize? ⇒ Boolean
-
#name_for_formatter ⇒ Object
-
#no_input?(format) ⇒ Boolean
-
#no_output?(format) ⇒ Boolean
-
#postprocess(param, context, validator) ⇒ Object
-
#preprocess(input, context, validator) ⇒ Object
-
#restricted_for_format?(rule, format) ⇒ Boolean
-
#set_local(*arr, rule: nil) ⇒ Object
-
#set_no_input(*arr, rule: nil) ⇒ Object
-
#set_postprocessor(rule: nil, &block) ⇒ Object
-
#set_preprocessor(rule: nil, &block) ⇒ Object
#create, #from_hash, #from_input, #normalize_alternative_name, #parameter_class
#freeze_variable, #freeze_variables, #variables_to_freeze
#obligatory, #obligatory!
#class_reader_writer
#late_init
#collection
#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
#default ⇒ Object
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
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_default ⇒ Object
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
|
#finish ⇒ Object
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
134
135
136
137
138
|
# File 'lib/params_ready/parameter/definition.rb', line 134
def memoize?
return false if @memoize.nil?
@memoize > 0
end
|
89
90
91
|
# File 'lib/params_ready/parameter/definition.rb', line 89
def name_for_formatter
self.class.name_for_formatter
end
|
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
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
|
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
|
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
|