Class: ParamsReady::Parameter::Definition
Instance Attribute Summary collapse
#altn, #name
Instance Method Summary
collapse
-
#canonical_default(value) ⇒ Object
-
#default_defined? ⇒ Boolean
-
#duplicate_default ⇒ Object
-
#finish ⇒ Object
-
#initialize(*args, default: Extensions::Undefined, optional: false, preprocessor: nil, populator: nil, postprocessor: nil, local: false, no_output: nil, **opts) ⇒ Definition
constructor
A new instance of Definition.
-
#local?(format) ⇒ Boolean
-
#memoize? ⇒ Boolean
-
#name_for_formatter ⇒ Object
-
#no_output?(format) ⇒ Boolean
-
#postprocess(param, context, validator) ⇒ Object
-
#preprocess(input, context, validator) ⇒ Object
-
#set_local(*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, 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
#default ⇒ Object
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
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_default ⇒ Object
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
|
#finish ⇒ Object
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
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
132
133
134
135
136
|
# File 'lib/params_ready/parameter/definition.rb', line 132
def memoize?
return false if @memoize.nil?
@memoize > 0
end
|
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
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
|