Class: Formeze::Field
- Inherits:
-
Object
- Object
- Formeze::Field
- Defined in:
- lib/formeze.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #blank_value ⇒ Object
- #blank_value? ⇒ Boolean
- #defined_if ⇒ Object
- #defined_if? ⇒ Boolean
- #defined_unless ⇒ Object
- #defined_unless? ⇒ Boolean
- #error(key, default) ⇒ Object
-
#initialize(name, options = {}) ⇒ Field
constructor
A new instance of Field.
- #key ⇒ Object
- #key_required? ⇒ Boolean
- #label ⇒ Object
- #multiline? ⇒ Boolean
- #multiple? ⇒ Boolean
- #no_match?(value) ⇒ Boolean
- #required? ⇒ Boolean
- #too_long?(value) ⇒ Boolean
- #too_short?(value) ⇒ Boolean
- #validate(value, form) ⇒ Object
- #values ⇒ Object
- #values? ⇒ Boolean
Constructor Details
#initialize(name, options = {}) ⇒ Field
Returns a new instance of Field.
7 8 9 |
# File 'lib/formeze.rb', line 7 def initialize(name, = {}) @name, @options = name, end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/formeze.rb', line 5 def name @name end |
Instance Method Details
#blank_value ⇒ Object
77 78 79 |
# File 'lib/formeze.rb', line 77 def blank_value @options.fetch(:blank) end |
#blank_value? ⇒ Boolean
73 74 75 |
# File 'lib/formeze.rb', line 73 def blank_value? @options.has_key?(:blank) end |
#defined_if ⇒ Object
93 94 95 |
# File 'lib/formeze.rb', line 93 def defined_if @options.fetch(:defined_if) end |
#defined_if? ⇒ Boolean
89 90 91 |
# File 'lib/formeze.rb', line 89 def defined_if? @options.has_key?(:defined_if) end |
#defined_unless ⇒ Object
101 102 103 |
# File 'lib/formeze.rb', line 101 def defined_unless @options.fetch(:defined_unless) end |
#defined_unless? ⇒ Boolean
97 98 99 |
# File 'lib/formeze.rb', line 97 def defined_unless? @options.has_key?(:defined_unless) end |
#error(key, default) ⇒ Object
33 34 35 |
# File 'lib/formeze.rb', line 33 def error(key, default) Formeze.translate(key, :scope => [:formeze, :errors], :default => default) end |
#key ⇒ Object
37 38 39 |
# File 'lib/formeze.rb', line 37 def key @key ||= @name.to_s end |
#key_required? ⇒ Boolean
41 42 43 |
# File 'lib/formeze.rb', line 41 def key_required? @options.fetch(:key_required) { true } end |
#label ⇒ Object
45 46 47 |
# File 'lib/formeze.rb', line 45 def label @options.fetch(:label) { Formeze.translate(name, :scope => [:formeze, :labels], :default => Formeze.label(name)) } end |
#multiline? ⇒ Boolean
53 54 55 |
# File 'lib/formeze.rb', line 53 def multiline? @options.fetch(:multiline) { false } end |
#multiple? ⇒ Boolean
57 58 59 |
# File 'lib/formeze.rb', line 57 def multiple? @options.fetch(:multiple) { false } end |
#no_match?(value) ⇒ Boolean
69 70 71 |
# File 'lib/formeze.rb', line 69 def no_match?(value) @options.has_key?(:pattern) && value !~ @options[:pattern] end |
#required? ⇒ Boolean
49 50 51 |
# File 'lib/formeze.rb', line 49 def required? @options.fetch(:required) { true } end |
#too_long?(value) ⇒ Boolean
61 62 63 |
# File 'lib/formeze.rb', line 61 def too_long?(value) value.chars.count > @options.fetch(:maxlength) { 64 } end |
#too_short?(value) ⇒ Boolean
65 66 67 |
# File 'lib/formeze.rb', line 65 def too_short?(value) @options.has_key?(:minlength) && value.chars.count < @options.fetch(:minlength) end |
#validate(value, form) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/formeze.rb', line 11 def validate(value, form) value = Formeze.scrub(value, @options[:scrub]) if value !~ /\S/ form.add_error(self, error(:required, 'is required')) if required? form.send(:"#{name}=", blank_value? ? blank_value : value) else form.add_error(self, error(:not_multiline, 'cannot contain newlines')) if !multiline? && value.lines.count > 1 form.add_error(self, error(:too_long, 'is too long')) if too_long?(value) form.add_error(self, error(:too_short, 'is too short')) if too_short?(value) form.add_error(self, error(:no_match, 'is invalid')) if no_match?(value) form.add_error(self, error(:bad_value, 'is invalid')) if values? && !values.include?(value) form.send(:"#{name}=", value) end end |
#values ⇒ Object
85 86 87 |
# File 'lib/formeze.rb', line 85 def values @options.fetch(:values) end |
#values? ⇒ Boolean
81 82 83 |
# File 'lib/formeze.rb', line 81 def values? @options.has_key?(:values) end |