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.
-
#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.
-
#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
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
#code ⇒ Object (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 |
#form ⇒ Object (readonly)
Form this parameter belongs to.
56 57 58 |
# File 'lib/form_input/core.rb', line 56 def form @form end |
#name ⇒ Object (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 |
#opts ⇒ Object (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.
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.
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.
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 |
#data ⇒ Object
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.
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.
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.
238 239 240 |
# File 'lib/form_input/core.rb', line 238 def enabled? not disabled? end |
#error ⇒ Object
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_title ⇒ Object
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 |
#errors ⇒ Object
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.
158 159 160 |
# File 'lib/form_input/core.rb', line 158 def filled? not empty? end |
#filter ⇒ Object
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_title ⇒ Object
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_value ⇒ Object
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 |
#format ⇒ Object
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.
268 269 270 |
# File 'lib/form_input/core.rb', line 268 def hash? !! self[ :hash ] end |
#hidden? ⇒ Boolean
Test if the parameter is hidden.
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.
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.
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 = nil end |
#invalid? ⇒ Boolean
Test if this parameter had some errors reported.
218 219 220 |
# File 'lib/form_input/core.rb', line 218 def invalid? not valid? end |
#optional? ⇒ Boolean
Test if the parameter is optional.
228 229 230 |
# File 'lib/form_input/core.rb', line 228 def optional? not required? end |
#required? ⇒ Boolean
Test if the parameter is required.
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.
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.
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.
283 284 285 286 287 288 289 290 |
# File 'lib/form_input/core.rb', line 283 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.
278 279 280 |
# File 'lib/form_input/core.rb', line 278 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.
188 189 190 |
# File 'lib/form_input/core.rb', line 188 def title self[ :title ] end |
#transform ⇒ Object
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_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.
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.
293 294 295 |
# File 'lib/form_input/core.rb', line 293 def untagged?( * ) not tagged?( * ) end |
#valid? ⇒ Boolean
Test if this parameter had no errors reported.
213 214 215 |
# File 'lib/form_input/core.rb', line 213 def valid? errors.empty? end |
#validate ⇒ Object
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 |
#value ⇒ Object
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.
258 259 260 |
# File 'lib/form_input/core.rb', line 258 def visible? not ( hidden? || ignored? ) end |