Class: FormInput::Parameter

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.



85
86
87
88
89
# File 'lib/form_input/core.rb', line 85

def initialize( name, code, opts )
  @name = name.freeze
  @code = code.freeze
  @opts = opts.freeze
end

Instance Attribute Details

#codeObject (readonly)

Name of the parameter as we use it in the form fields and url queries.



79
80
81
# File 'lib/form_input/core.rb', line 79

def code
  @code
end

#formObject (readonly)

Form this parameter belongs to.



73
74
75
# File 'lib/form_input/core.rb', line 73

def form
  @form
end

#nameObject (readonly)

Name of the parameter as we use it internally in our code.



76
77
78
# File 'lib/form_input/core.rb', line 76

def name
  @name
end

#optsObject (readonly)

Additional parameter options.



82
83
84
# File 'lib/form_input/core.rb', line 82

def opts
  @opts
end

Instance Method Details

#array?Boolean

Test if this is an array parameter.

Returns:

  • (Boolean)


309
310
311
# File 'lib/form_input/core.rb', line 309

def array?
  !! self[ :array ]
end

#bind(form) ⇒ Object

Bind self to given form instance. Can be done only once.



98
99
100
101
102
# File 'lib/form_input/core.rb', line 98

def bind( form )
  fail "parameter #{name} is already bound" if @form
  @form = form
  self
end

#blank?Boolean

Test if given parameter has blank value.

Returns:

  • (Boolean)


168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/form_input/core.rb', line 168

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

#cleanup_string(string, replacement) ⇒ Object

Make sure given string is in our default encoding and contains only valid characters.



110
111
112
113
114
115
# File 'lib/form_input/core.rb', line 110

def cleanup_string( string, replacement )
  unless string.valid_encoding? && ( string.encoding == DEFAULT_ENCODING || string.ascii_only? )
    string = string.dup.force_encoding( DEFAULT_ENCODING ).scrub( replacement )
  end
  string
end

#correct?Boolean

Test if given parameter has value of correct type.

Returns:

  • (Boolean)


149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/form_input/core.rb', line 149

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

#dataObject

Get data relevant for this parameter, if any. Returns empty array if there are none.



359
360
361
# File 'lib/form_input/core.rb', line 359

def data
  self[ :data ] || []
end

#disabled?Boolean

Test if the parameter is disabled.

Returns:

  • (Boolean)


279
280
281
# File 'lib/form_input/core.rb', line 279

def disabled?
  !! self[ :disabled ]
end

#empty?Boolean

Test if given parameter has empty value.

Returns:

  • (Boolean)


182
183
184
185
186
187
188
189
190
191
# File 'lib/form_input/core.rb', line 182

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.

Returns:

  • (Boolean)


284
285
286
# File 'lib/form_input/core.rb', line 284

def enabled?
  not disabled?
end

#errorObject

Get first error reported for this parameter. Always nil for unbound parameters.



254
255
256
# File 'lib/form_input/core.rb', line 254

def error
  errors.first
end

#error_titleObject

Get the title for use in error messages. Fallbacks to normal title and code if no form title was specified.



244
245
246
# File 'lib/form_input/core.rb', line 244

def error_title
  self[ :error_title ] || title || code.to_s
end

#errorsObject

Get list of errors reported for this paramater. Always empty for unbound parameters.



249
250
251
# File 'lib/form_input/core.rb', line 249

def errors
  form ? form.errors_for( name ) : []
end

#filled?Boolean

Test if given parameter has non-empty value.

Returns:

  • (Boolean)


194
195
196
# File 'lib/form_input/core.rb', line 194

def filled?
  not empty?
end

#filterObject

Get input filter for this paramater, if any.



344
345
346
# File 'lib/form_input/core.rb', line 344

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.



209
210
211
212
213
214
215
216
217
218
# File 'lib/form_input/core.rb', line 209

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_titleObject

Get the title for use in form. Fallbacks to normal title and code if no form title was specified.



239
240
241
# File 'lib/form_input/core.rb', line 239

def form_title
  self[ :form_title ] || title || code.to_s
end

#form_valueObject

Get value of this parameter for use in form, with all scalar values converted to strings.



139
140
141
# File 'lib/form_input/core.rb', line 139

def form_value
  formatted_value
end

#formatObject

Get output filter for this paramater, if any.



354
355
356
# File 'lib/form_input/core.rb', line 354

def format
  opts[ :format ]
end

#format_value(value, replacement = DEFAULT_REPLACEMENT_CHARACTER) ⇒ Object

Format given value for form/URL output, applying the formatting filter as necessary.



118
119
120
121
122
123
124
125
# File 'lib/form_input/core.rb', line 118

def format_value( value, replacement = DEFAULT_REPLACEMENT_CHARACTER )
  value = cleanup_string( value, replacement ) if replacement and value.is_a?( String )
  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

#formatted_value(replacement = DEFAULT_REPLACEMENT_CHARACTER) ⇒ Object

Get value of this parameter for use in form/URL, with all scalar values converted to strings.



128
129
130
131
132
133
134
135
136
# File 'lib/form_input/core.rb', line 128

def formatted_value( replacement = DEFAULT_REPLACEMENT_CHARACTER )
  if array?
    [ *value ].map{ |x| format_value( x, replacement ) }
  elsif hash?
    Hash[ [ *value ].map{ |k, v| [ replacement ? cleanup_string( k.to_s, replacement ) : k.to_s, format_value( v, replacement ) ] } ]
  else
    format_value( value, replacement )
  end
end

#hash?Boolean

Test if this is a hash parameter.

Returns:

  • (Boolean)


314
315
316
# File 'lib/form_input/core.rb', line 314

def hash?
  !! self[ :hash ]
end

#hidden?Boolean

Test if the parameter is hidden.

Returns:

  • (Boolean)


294
295
296
# File 'lib/form_input/core.rb', line 294

def hidden?
  type == :hidden
end

#ignored?Boolean

Test if the parameter is to be ignored on output.

Returns:

  • (Boolean)


299
300
301
# File 'lib/form_input/core.rb', line 299

def ignored?
  type == :ignore
end

#incorrect?Boolean

Test if given parameter has value of incorrect type.

Returns:

  • (Boolean)


163
164
165
# File 'lib/form_input/core.rb', line 163

def incorrect?
  not correct?
end

#initialize_dup(other) ⇒ Object

Allow copies to evaluate tags again.



92
93
94
95
# File 'lib/form_input/core.rb', line 92

def initialize_dup( other )
  super
  @tags = nil
end

#invalid?Boolean

Test if this parameter had some errors reported.

Returns:

  • (Boolean)


264
265
266
# File 'lib/form_input/core.rb', line 264

def invalid?
  not valid?
end

#optional?Boolean

Test if the parameter is optional.

Returns:

  • (Boolean)


274
275
276
# File 'lib/form_input/core.rb', line 274

def optional?
  not required?
end

#required?Boolean

Test if the parameter is required.

Returns:

  • (Boolean)


269
270
271
# File 'lib/form_input/core.rb', line 269

def required?
  !! self[ :required ]
end

#scalar?Boolean

Test if this is a scalar parameter.

Returns:

  • (Boolean)


319
320
321
# File 'lib/form_input/core.rb', line 319

def scalar?
  not ( array? || hash? )
end

#selected?(value) ⇒ Boolean

Test if given value is the selected value, for use in form selects.

Returns:

  • (Boolean)


221
222
223
224
225
226
227
228
229
230
231
# File 'lib/form_input/core.rb', line 221

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.

Returns:

  • (Boolean)


199
200
201
# File 'lib/form_input/core.rb', line 199

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.

Returns:

  • (Boolean)


329
330
331
332
333
334
335
336
# File 'lib/form_input/core.rb', line 329

def tagged?( *tags )
  t = self.tags
  if tags.empty?
    not t.empty?
  else
    tags.flatten.any?{ |x| t.include? x }
  end
end

#tagsObject

Get list of tags of this parameter.



324
325
326
# File 'lib/form_input/core.rb', line 324

def tags
  @tags ||= [ *self[ :tag ], *self[ :tags ] ]
end

#titleObject

Get the name of the parameter to be displayed to the user, or nil if there is none.



234
235
236
# File 'lib/form_input/core.rb', line 234

def title
  self[ :title ]
end

#transformObject

Get input transform for this paramater, if any.



349
350
351
# File 'lib/form_input/core.rb', line 349

def transform
  opts[ :transform ]
end

#translation_hashObject

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

#typeObject

Get type of the parameter. Defaults to :text.



289
290
291
# File 'lib/form_input/core.rb', line 289

def type
  self[ :type ] || :text
end

#unset?Boolean

Test if value of given parameter is not set.

Returns:

  • (Boolean)


204
205
206
# File 'lib/form_input/core.rb', line 204

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.

Returns:

  • (Boolean)


339
340
341
# File 'lib/form_input/core.rb', line 339

def untagged?( *tags )
  not tagged?( *tags )
end

#url_valueObject

Get value of this parameter for use in URL, with all scalar values converted to strings.



144
145
146
# File 'lib/form_input/core.rb', line 144

def url_value
  formatted_value( nil )
end

#valid?Boolean

Test if this parameter had no errors reported.

Returns:

  • (Boolean)


259
260
261
# File 'lib/form_input/core.rb', line 259

def valid?
  errors.empty?
end

#validateObject

Validate this parameter. Does nothing if it was found invalid already.



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/form_input/core.rb', line 408

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

#valueObject

Get the value of this parameter. Always nil for unbound parameters.



105
106
107
# File 'lib/form_input/core.rb', line 105

def value
  form ? form[ name ] : nil
end

#visible?Boolean

Test if the parameter is visible.

Returns:

  • (Boolean)


304
305
306
# File 'lib/form_input/core.rb', line 304

def visible?
  not ( hidden? || ignored? )
end