Class: FormInput::Parameter
- Inherits:
-
Object
- Object
- FormInput::Parameter
- Includes:
- LocaleMethods, R18nMethods, R18n::Helpers
- Defined in:
- lib/form_input/core.rb,
lib/form_input/r18n.rb,
lib/form_input/localize.rb
Overview
Localize few parameter methods.
Defined Under Namespace
Modules: LocaleMethods, R18nMethods
Constant Summary collapse
- TRANSLATION_ORDER =
Array definining explicit default order of names of parameter options in translation files.
%w[title form_title error_title gender plural inflect required_msg msg match_msg reject_msg]
Constants included from R18nMethods
R18nMethods::UNLOCALIZED_OPTIONS
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Name of the parameter as we use it in the form fields and url queries.
-
#form ⇒ Object
readonly
Form this parameter belongs to.
-
#name ⇒ Object
readonly
Name of the parameter as we use it internally in our code.
-
#opts ⇒ Object
readonly
Additional parameter options.
Instance Method Summary collapse
-
#array? ⇒ Boolean
Test if this is an array parameter.
-
#bind(form) ⇒ Object
Bind self to given form instance.
-
#blank? ⇒ Boolean
Test if given parameter has blank value.
-
#correct? ⇒ Boolean
Test if given parameter has value of correct type.
-
#data ⇒ Object
Get data relevant for this parameter, if any.
-
#disabled? ⇒ Boolean
Test if the parameter is disabled.
-
#empty? ⇒ Boolean
Test if given parameter has empty value.
-
#enabled? ⇒ Boolean
Test if the parameter is enabled.
-
#error ⇒ Object
Get first error reported for this parameter.
-
#error_title ⇒ Object
Get the title for use in error messages.
-
#errors ⇒ Object
Get list of errors reported for this paramater.
-
#filled? ⇒ Boolean
Test if given parameter has non-empty value.
-
#filter ⇒ Object
Get input filter for this paramater, if any.
-
#form_name(key = nil) ⇒ Object
Get the proper name for use in form names, adding [] to array and [key] to hash parameters.
-
#form_title ⇒ Object
Get the title for use in form.
-
#form_value ⇒ Object
Get value of this parameter for use in form/URL, with all scalar values converted to strings.
-
#format ⇒ Object
Get output filter for this paramater, if any.
-
#format_value(value) ⇒ Object
Format given value for form/URL output, applying the formatting filter as necessary.
-
#hash? ⇒ Boolean
Test if this is a hash parameter.
-
#hidden? ⇒ Boolean
Test if the parameter is hidden.
-
#ignored? ⇒ Boolean
Test if the parameter is to be ignored on output.
-
#incorrect? ⇒ Boolean
Test if given parameter has value of incorrect type.
-
#initialize(name, code, opts) ⇒ Parameter
constructor
Initialize new parameter.
-
#initialize_dup(other) ⇒ Object
Allow copies to evaluate tags again.
-
#invalid? ⇒ Boolean
Test if this parameter had some errors reported.
-
#optional? ⇒ Boolean
Test if the parameter is optional.
-
#required? ⇒ Boolean
Test if the parameter is required.
-
#scalar? ⇒ Boolean
Test if this is a scalar parameter.
-
#selected?(value) ⇒ Boolean
Test if given value is the selected value, for use in form selects.
-
#set? ⇒ Boolean
Test if value of given parameter was set.
-
#tagged?(*tags) ⇒ Boolean
Test if the parameter is tagged with some of given tags, or any tag if the argument list is empty.
-
#tags ⇒ Object
Get list of tags of this parameter.
-
#title ⇒ Object
Get the name of the parameter to be displayed to the user, or nil if there is none.
-
#transform ⇒ Object
Get input transform for this paramater, if any.
-
#translation_hash ⇒ Object
Get hash of all parameter values which may need to be localized.
-
#translation_order(name) ⇒ Object
Get translation order for given option name.
-
#type ⇒ Object
Get type of the parameter.
-
#unset? ⇒ Boolean
Test if value of given parameter is not set.
-
#untagged?(*tags) ⇒ Boolean
Test if the parameter is not tagged with any of given tags, or any tag if the argument list is empty.
-
#valid? ⇒ Boolean
Test if this parameter had no errors reported.
-
#validate ⇒ Object
Validate this parameter.
-
#value ⇒ Object
Get the value of this parameter.
-
#visible? ⇒ Boolean
Test if the parameter is visible.
Methods included from R18nMethods
#[], #format_error_message, #ft, #gender, #inflection, #pluralize, #pt
Methods included from LocaleMethods
#[], #format_error_message, #report, #report!
Constructor Details
#initialize(name, code, opts) ⇒ Parameter
Initialize new parameter.
71 72 73 74 75 |
# File 'lib/form_input/core.rb', line 71 def initialize( name, code, opts ) @name = name.freeze @code = code.freeze @opts = opts.freeze end |
Instance Attribute Details
#code ⇒ Object (readonly)
Name of the parameter as we use it in the form fields and url queries.
65 66 67 |
# File 'lib/form_input/core.rb', line 65 def code @code end |
#form ⇒ Object (readonly)
Form this parameter belongs to.
59 60 61 |
# File 'lib/form_input/core.rb', line 59 def form @form end |
#name ⇒ Object (readonly)
Name of the parameter as we use it internally in our code.
62 63 64 |
# File 'lib/form_input/core.rb', line 62 def name @name end |
#opts ⇒ Object (readonly)
Additional parameter options.
68 69 70 |
# File 'lib/form_input/core.rb', line 68 def opts @opts end |
Instance Method Details
#array? ⇒ Boolean
Test if this is an array parameter.
276 277 278 |
# File 'lib/form_input/core.rb', line 276 def array? !! self[ :array ] end |
#bind(form) ⇒ Object
Bind self to given form instance. Can be done only once.
84 85 86 87 88 |
# File 'lib/form_input/core.rb', line 84 def bind( form ) fail "parameter #{name} is already bound" if @form @form = form self end |
#blank? ⇒ Boolean
Test if given parameter has blank value.
135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/form_input/core.rb', line 135 def blank? case v = value when nil true when String v.empty? or !! ( v.valid_encoding? && v =~ /\A\s*\z/ ) when Array, Hash v.empty? else false end end |
#correct? ⇒ Boolean
Test if given parameter has value of correct type.
116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/form_input/core.rb', line 116 def correct? case v = value when nil true when String scalar? when Array array? when Hash hash? end or ( scalar? && [ *self[ :class ] ].any?{ |x| v.is_a?( x ) } ) end |
#data ⇒ Object
Get data relevant for this parameter, if any. Returns empty array if there are none.
326 327 328 |
# File 'lib/form_input/core.rb', line 326 def data self[ :data ] || [] end |
#disabled? ⇒ Boolean
Test if the parameter is disabled.
246 247 248 |
# File 'lib/form_input/core.rb', line 246 def disabled? !! self[ :disabled ] end |
#empty? ⇒ Boolean
Test if given parameter has empty value.
149 150 151 152 153 154 155 156 157 158 |
# File 'lib/form_input/core.rb', line 149 def empty? case v = value when nil true when String, Array, Hash v.empty? else false end end |
#enabled? ⇒ Boolean
Test if the parameter is enabled.
251 252 253 |
# File 'lib/form_input/core.rb', line 251 def enabled? not disabled? end |
#error ⇒ Object
Get first error reported for this parameter. Always nil for unbound parameters.
221 222 223 |
# File 'lib/form_input/core.rb', line 221 def error errors.first end |
#error_title ⇒ Object
Get the title for use in error messages. Fallbacks to normal title and code if no form title was specified.
211 212 213 |
# File 'lib/form_input/core.rb', line 211 def error_title self[ :error_title ] || title || code.to_s end |
#errors ⇒ Object
Get list of errors reported for this paramater. Always empty for unbound parameters.
216 217 218 |
# File 'lib/form_input/core.rb', line 216 def errors form ? form.errors_for( name ) : [] end |
#filled? ⇒ Boolean
Test if given parameter has non-empty value.
161 162 163 |
# File 'lib/form_input/core.rb', line 161 def filled? not empty? end |
#filter ⇒ Object
Get input filter for this paramater, if any.
311 312 313 |
# File 'lib/form_input/core.rb', line 311 def filter opts[ :filter ] end |
#form_name(key = nil) ⇒ Object
Get the proper name for use in form names, adding [] to array and [key] to hash parameters.
176 177 178 179 180 181 182 183 184 185 |
# File 'lib/form_input/core.rb', line 176 def form_name( key = nil ) if array? "#{code}[]" elsif hash? fail( ArgumentError, "missing hash key" ) if key.nil? "#{code}[#{key}]" else code.to_s end end |
#form_title ⇒ Object
Get the title for use in form. Fallbacks to normal title and code if no form title was specified.
206 207 208 |
# File 'lib/form_input/core.rb', line 206 def form_title self[ :form_title ] || title || code.to_s end |
#form_value ⇒ Object
Get value of this parameter for use in form/URL, with all scalar values converted to strings.
105 106 107 108 109 110 111 112 113 |
# File 'lib/form_input/core.rb', line 105 def form_value if array? [ *value ].map{ |x| format_value( x ) } elsif hash? Hash[ [ *value ].map{ |k, v| [ k.to_s, format_value( v ) ] } ] else format_value( value ) end end |
#format ⇒ Object
Get output filter for this paramater, if any.
321 322 323 |
# File 'lib/form_input/core.rb', line 321 def format opts[ :format ] end |
#format_value(value) ⇒ Object
Format given value for form/URL output, applying the formatting filter as necessary.
96 97 98 99 100 101 102 |
# File 'lib/form_input/core.rb', line 96 def format_value( value ) if format.nil? or value.nil? or ( value.is_a?( String ) and type = self[ :class ] and type != String ) value.to_s else value.instance_exec( &format ).to_s end end |
#hash? ⇒ Boolean
Test if this is a hash parameter.
281 282 283 |
# File 'lib/form_input/core.rb', line 281 def hash? !! self[ :hash ] end |
#hidden? ⇒ Boolean
Test if the parameter is hidden.
261 262 263 |
# File 'lib/form_input/core.rb', line 261 def hidden? type == :hidden end |
#ignored? ⇒ Boolean
Test if the parameter is to be ignored on output.
266 267 268 |
# File 'lib/form_input/core.rb', line 266 def ignored? type == :ignore end |
#incorrect? ⇒ Boolean
Test if given parameter has value of incorrect type.
130 131 132 |
# File 'lib/form_input/core.rb', line 130 def incorrect? not correct? end |
#initialize_dup(other) ⇒ Object
Allow copies to evaluate tags again.
78 79 80 81 |
# File 'lib/form_input/core.rb', line 78 def initialize_dup( other ) super = nil end |
#invalid? ⇒ Boolean
Test if this parameter had some errors reported.
231 232 233 |
# File 'lib/form_input/core.rb', line 231 def invalid? not valid? end |
#optional? ⇒ Boolean
Test if the parameter is optional.
241 242 243 |
# File 'lib/form_input/core.rb', line 241 def optional? not required? end |
#required? ⇒ Boolean
Test if the parameter is required.
236 237 238 |
# File 'lib/form_input/core.rb', line 236 def required? !! self[ :required ] end |
#scalar? ⇒ Boolean
Test if this is a scalar parameter.
286 287 288 |
# File 'lib/form_input/core.rb', line 286 def scalar? not ( array? || hash? ) end |
#selected?(value) ⇒ Boolean
Test if given value is the selected value, for use in form selects.
188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/form_input/core.rb', line 188 def selected?( value ) if empty? false elsif array? self.value.include?( value ) elsif hash? false else self.value == value end end |
#set? ⇒ Boolean
Test if value of given parameter was set.
166 167 168 |
# File 'lib/form_input/core.rb', line 166 def set? form ? form.instance_variable_defined?( "@#{name}" ) : false end |
#tagged?(*tags) ⇒ Boolean
Test if the parameter is tagged with some of given tags, or any tag if the argument list is empty.
296 297 298 299 300 301 302 303 |
# File 'lib/form_input/core.rb', line 296 def tagged?( * ) t = self. if .empty? not t.empty? else .flatten.any?{ |x| t.include? x } end end |
#tags ⇒ Object
Get list of tags of this parameter.
291 292 293 |
# File 'lib/form_input/core.rb', line 291 def ||= [ *self[ :tag ], *self[ :tags ] ] end |
#title ⇒ Object
Get the name of the parameter to be displayed to the user, or nil if there is none.
201 202 203 |
# File 'lib/form_input/core.rb', line 201 def title self[ :title ] end |
#transform ⇒ Object
Get input transform for this paramater, if any.
316 317 318 |
# File 'lib/form_input/core.rb', line 316 def transform opts[ :transform ] end |
#translation_hash ⇒ Object
Get hash of all parameter values which may need to be localized.
25 26 27 |
# File 'lib/form_input/localize.rb', line 25 def translation_hash Hash[ opts.select{ |k, v| v.is_a? String }.sort_by{ |k, v| [ translation_order( k ), k ] } ] end |
#translation_order(name) ⇒ Object
Get translation order for given option name.
20 21 22 |
# File 'lib/form_input/localize.rb', line 20 def translation_order( name ) TRANSLATION_ORDER.index( name.to_s ) || TRANSLATION_ORDER.count end |
#type ⇒ Object
Get type of the parameter. Defaults to :text.
256 257 258 |
# File 'lib/form_input/core.rb', line 256 def type self[ :type ] || :text end |
#unset? ⇒ Boolean
Test if value of given parameter is not set.
171 172 173 |
# File 'lib/form_input/core.rb', line 171 def unset? not set? end |
#untagged?(*tags) ⇒ Boolean
Test if the parameter is not tagged with any of given tags, or any tag if the argument list is empty.
306 307 308 |
# File 'lib/form_input/core.rb', line 306 def untagged?( * ) not tagged?( * ) end |
#valid? ⇒ Boolean
Test if this parameter had no errors reported.
226 227 228 |
# File 'lib/form_input/core.rb', line 226 def valid? errors.empty? end |
#validate ⇒ Object
Validate this parameter. Does nothing if it was found invalid already.
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
# File 'lib/form_input/core.rb', line 375 def validate return if invalid? # First of all, make sure required parameters are present and not empty. if required? && empty? report( self[ :required_msg ] || ( scalar? ? :required_scalar : :required_array ) ) return end # Otherwise empty parameters are considered correct, as long as the type is correct. return if empty? && correct? # Make sure the parameter value contains only valid data. return unless if array? validate_array( value ) elsif hash? validate_hash( value ) else validate_value( value ) end # Finally, invoke the custom check callbacks if there are any. if checks = opts[ :check ] [ *checks ].each{ |x| instance_exec( &x ) } end end |
#value ⇒ Object
Get the value of this parameter. Always nil for unbound parameters.
91 92 93 |
# File 'lib/form_input/core.rb', line 91 def value form ? form[ name ] : nil end |
#visible? ⇒ Boolean
Test if the parameter is visible.
271 272 273 |
# File 'lib/form_input/core.rb', line 271 def visible? not ( hidden? || ignored? ) end |