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_many_characters?(value) ⇒ Boolean
- #too_many_words?(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.
17 18 19 20 21 22 23 |
# File 'lib/formeze.rb', line 17 def initialize(name, = {}) @name, @options = name, if .has_key?(:word_limit) Kernel.warn '[formeze] :word_limit option is deprecated, please use custom validation instead' end end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
15 16 17 |
# File 'lib/formeze.rb', line 15 def name @name end |
Instance Method Details
#blank_value ⇒ Object
99 100 101 |
# File 'lib/formeze.rb', line 99 def blank_value @options.fetch(:blank) end |
#blank_value? ⇒ Boolean
95 96 97 |
# File 'lib/formeze.rb', line 95 def blank_value? @options.has_key?(:blank) end |
#defined_if ⇒ Object
115 116 117 |
# File 'lib/formeze.rb', line 115 def defined_if @options.fetch(:defined_if) end |
#defined_if? ⇒ Boolean
111 112 113 |
# File 'lib/formeze.rb', line 111 def defined_if? @options.has_key?(:defined_if) end |
#defined_unless ⇒ Object
123 124 125 |
# File 'lib/formeze.rb', line 123 def defined_unless @options.fetch(:defined_unless) end |
#defined_unless? ⇒ Boolean
119 120 121 |
# File 'lib/formeze.rb', line 119 def defined_unless? @options.has_key?(:defined_unless) end |
#error(key, default) ⇒ Object
47 48 49 |
# File 'lib/formeze.rb', line 47 def error(key, default) Formeze.translate(key, :scope => [:formeze, :errors], :default => default) end |
#key ⇒ Object
51 52 53 |
# File 'lib/formeze.rb', line 51 def key @key ||= @name.to_s end |
#key_required? ⇒ Boolean
55 56 57 |
# File 'lib/formeze.rb', line 55 def key_required? @options.fetch(:key_required) { true } end |
#label ⇒ Object
59 60 61 |
# File 'lib/formeze.rb', line 59 def label @options.fetch(:label) { Formeze.translate(name, :scope => [:formeze, :labels], :default => Label.new(name)) } end |
#multiline? ⇒ Boolean
67 68 69 |
# File 'lib/formeze.rb', line 67 def multiline? @options.fetch(:multiline) { false } end |
#multiple? ⇒ Boolean
71 72 73 |
# File 'lib/formeze.rb', line 71 def multiple? @options.fetch(:multiple) { false } end |
#no_match?(value) ⇒ Boolean
91 92 93 |
# File 'lib/formeze.rb', line 91 def no_match?(value) @options.has_key?(:pattern) && value !~ @options[:pattern] end |
#required? ⇒ Boolean
63 64 65 |
# File 'lib/formeze.rb', line 63 def required? @options.fetch(:required) { true } end |
#too_long?(value) ⇒ Boolean
75 76 77 |
# File 'lib/formeze.rb', line 75 def too_long?(value) too_many_characters?(value) || too_many_words?(value) end |
#too_many_characters?(value) ⇒ Boolean
83 84 85 |
# File 'lib/formeze.rb', line 83 def too_many_characters?(value) value.chars.count > @options.fetch(:maxlength) { 64 } end |
#too_many_words?(value) ⇒ Boolean
87 88 89 |
# File 'lib/formeze.rb', line 87 def too_many_words?(value) @options.has_key?(:word_limit) && value.scan(/\w+/).length > @options[:word_limit] end |
#too_short?(value) ⇒ Boolean
79 80 81 |
# File 'lib/formeze.rb', line 79 def too_short?(value) @options.has_key?(:minlength) && value.chars.count < @options.fetch(:minlength) end |
#validate(value, form) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/formeze.rb', line 25 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
107 108 109 |
# File 'lib/formeze.rb', line 107 def values @options.fetch(:values) end |
#values? ⇒ Boolean
103 104 105 |
# File 'lib/formeze.rb', line 103 def values? @options.has_key?(:values) end |