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

Constructor Details

#initialize(name, code, opts) ⇒ Parameter

Initialize new parameter.



68
69
70
71
72
# File 'lib/form_input/core.rb', line 68

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.



62
63
64
# File 'lib/form_input/core.rb', line 62

def code
  @code
end

#formObject (readonly)

Form this parameter belongs to.



56
57
58
# File 'lib/form_input/core.rb', line 56

def form
  @form
end

#nameObject (readonly)

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



59
60
61
# File 'lib/form_input/core.rb', line 59

def name
  @name
end

#optsObject (readonly)

Additional parameter options.



65
66
67
# File 'lib/form_input/core.rb', line 65

def opts
  @opts
end

Instance Method Details

#array?Boolean

Test if this is an array parameter.

Returns:

  • (Boolean)


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

def array?
  !! self[ :array ]
end

#bind(form) ⇒ Object

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



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

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)


132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/form_input/core.rb', line 132

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.

Returns:

  • (Boolean)


113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/form_input/core.rb', line 113

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.



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

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

#disabled?Boolean

Test if the parameter is disabled.

Returns:

  • (Boolean)


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

def disabled?
  !! self[ :disabled ]
end

#empty?Boolean

Test if given parameter has empty value.

Returns:

  • (Boolean)


146
147
148
149
150
151
152
153
154
155
# File 'lib/form_input/core.rb', line 146

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)


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

def enabled?
  not disabled?
end

#errorObject

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



208
209
210
# File 'lib/form_input/core.rb', line 208

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.



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

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.



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

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

#filled?Boolean

Test if given parameter has non-empty value.

Returns:

  • (Boolean)


158
159
160
# File 'lib/form_input/core.rb', line 158

def filled?
  not empty?
end

#filterObject

Get input filter for this paramater, if any.



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

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.



163
164
165
166
167
168
169
170
171
172
# File 'lib/form_input/core.rb', line 163

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.



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

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

#form_valueObject

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



102
103
104
105
106
107
108
109
110
# File 'lib/form_input/core.rb', line 102

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

#formatObject

Get output filter for this paramater, if any.



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

def format
  opts[ :format ]
end

#format_value(value) ⇒ Object

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



93
94
95
96
97
98
99
# File 'lib/form_input/core.rb', line 93

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.

Returns:

  • (Boolean)


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

def hash?
  !! self[ :hash ]
end

#hidden?Boolean

Test if the parameter is hidden.

Returns:

  • (Boolean)


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

def hidden?
  type == :hidden
end

#ignored?Boolean

Test if the parameter is to be ignored on output.

Returns:

  • (Boolean)


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

def ignored?
  type == :ignore
end

#incorrect?Boolean

Test if given parameter has value of incorrect type.

Returns:

  • (Boolean)


127
128
129
# File 'lib/form_input/core.rb', line 127

def incorrect?
  not correct?
end

#initialize_dup(other) ⇒ Object

Allow copies to evaluate tags again.



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

def initialize_dup( other )
  super
  @tags = nil
end

#invalid?Boolean

Test if this parameter had some errors reported.

Returns:

  • (Boolean)


218
219
220
# File 'lib/form_input/core.rb', line 218

def invalid?
  not valid?
end

#optional?Boolean

Test if the parameter is optional.

Returns:

  • (Boolean)


228
229
230
# File 'lib/form_input/core.rb', line 228

def optional?
  not required?
end

#required?Boolean

Test if the parameter is required.

Returns:

  • (Boolean)


223
224
225
# File 'lib/form_input/core.rb', line 223

def required?
  !! self[ :required ]
end

#scalar?Boolean

Test if this is a scalar parameter.

Returns:

  • (Boolean)


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

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

#selected?(value) ⇒ Boolean

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

Returns:

  • (Boolean)


175
176
177
178
179
180
181
182
183
184
185
# File 'lib/form_input/core.rb', line 175

def selected?( value )
  if empty?
    false
  elsif array?
    self.value.include?( value )
  elsif hash?
    false
  else
    self.value == value
  end
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)


283
284
285
286
287
288
289
290
# File 'lib/form_input/core.rb', line 283

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.



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

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.



188
189
190
# File 'lib/form_input/core.rb', line 188

def title
  self[ :title ]
end

#transformObject

Get input transform for this paramater, if any.



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

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.



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

def type
  self[ :type ] || :text
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)


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

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

#valid?Boolean

Test if this parameter had no errors reported.

Returns:

  • (Boolean)


213
214
215
# File 'lib/form_input/core.rb', line 213

def valid?
  errors.empty?
end

#validateObject

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



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/form_input/core.rb', line 352

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 callback if there is any.

  if check = opts[ :check ]
    instance_exec( &check )
  end
end

#valueObject

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



88
89
90
# File 'lib/form_input/core.rb', line 88

def value
  form ? form[ name ] : nil
end

#visible?Boolean

Test if the parameter is visible.

Returns:

  • (Boolean)


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

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