Class: Formeze::Field

Inherits:
Object
  • Object
show all
Includes:
Presence
Defined in:
lib/formeze/field.rb

Constant Summary

Constants included from Presence

Presence::REGEXP

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Presence

#blank?, #present?

Constructor Details

#initialize(name, **options) ⇒ Field

Returns a new instance of Field.



8
9
10
# File 'lib/formeze/field.rb', line 8

def initialize(name, **options)
  @name, @options = name, options
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/formeze/field.rb', line 6

def name
  @name
end

Instance Method Details

#acceptObject



102
103
104
# File 'lib/formeze/field.rb', line 102

def accept
  @accept ||= @options.fetch(:accept).split(',').flat_map { |type| MIME::Types[type] }
end

#accept?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/formeze/field.rb', line 98

def accept?
  @options.key?(:accept)
end

#acceptable_file?(object) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
62
63
64
# File 'lib/formeze/field.rb', line 54

def acceptable_file?(object)
  types = MIME::Types.type_for(object.original_filename)

  if object.content_type == 'application/octet-stream'
    types.any? { |type| accept.include?(type) }
  else
    type = MIME::Types[object.content_type].first

    accept.include?(type) && types.include?(type)
  end
end

#blank_valueObject



118
119
120
# File 'lib/formeze/field.rb', line 118

def blank_value
  @options[:blank]
end

#defined_ifObject



134
135
136
# File 'lib/formeze/field.rb', line 134

def defined_if
  @options.fetch(:defined_if)
end

#defined_if?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/formeze/field.rb', line 130

def defined_if?
  @options.key?(:defined_if)
end

#defined_unlessObject



142
143
144
# File 'lib/formeze/field.rb', line 142

def defined_unless
  @options.fetch(:defined_unless)
end

#defined_unless?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/formeze/field.rb', line 138

def defined_unless?
  @options.key?(:defined_unless)
end

#fill_procObject



160
161
162
# File 'lib/formeze/field.rb', line 160

def fill_proc
  @options[:fill]
end

#fill_proc?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/formeze/field.rb', line 156

def fill_proc?
  @options.key?(:fill)
end

#keyObject



66
67
68
# File 'lib/formeze/field.rb', line 66

def key
  @key ||= @name.to_s
end

#key_required?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/formeze/field.rb', line 70

def key_required?
  @options.fetch(:key_required) { true }
end

#labelObject



74
75
76
# File 'lib/formeze/field.rb', line 74

def label
  @options.fetch(:label) { Formeze::Labels.translate(name) }
end

#maxsizeObject



94
95
96
# File 'lib/formeze/field.rb', line 94

def maxsize
  @options.fetch(:maxsize)
end

#maxsize?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/formeze/field.rb', line 90

def maxsize?
  @options.key?(:maxsize)
end

#multiline?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/formeze/field.rb', line 82

def multiline?
  @options.fetch(:multiline) { false }
end

#multiple?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/formeze/field.rb', line 86

def multiple?
  @options.fetch(:multiple) { false }
end

#no_match?(value) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/formeze/field.rb', line 114

def no_match?(value)
  @options.key?(:pattern) && value !~ @options[:pattern]
end

#required?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/formeze/field.rb', line 78

def required?
  @options.fetch(:required) { true }
end

#too_long?(value) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/formeze/field.rb', line 106

def too_long?(value)
  value.chars.count > @options.fetch(:maxlength) { 64 }
end

#too_short?(value) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/formeze/field.rb', line 110

def too_short?(value)
  @options.key?(:minlength) && value.chars.count < @options.fetch(:minlength)
end

#undefined?(form) ⇒ Boolean

Returns:

  • (Boolean)


146
147
148
149
150
151
152
153
154
# File 'lib/formeze/field.rb', line 146

def undefined?(form)
  if defined_if?
    !Formeze::Block.evaluate(form, defined_if)
  elsif defined_unless?
    Formeze::Block.evaluate(form, defined_unless)
  else
    false
  end
end

#validate(value, form) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/formeze/field.rb', line 32

def validate(value, form)
  value = Formeze.scrub(value, @options[:scrub])

  if blank?(value)
    form.add_error(self, :required) if required?

    value = blank_value
  else
    form.add_error(self, :not_multiline) if !multiline? && value.lines.count > 1

    form.add_error(self, :too_long) if too_long?(value)

    form.add_error(self, :too_short) if too_short?(value)

    form.add_error(self, :no_match) if no_match?(value)

    form.add_error(self, :bad_value) if values? && !values.include?(value)
  end

  value
end

#validate_all(values, form) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/formeze/field.rb', line 12

def validate_all(values, form)
  size = 0

  values.each do |value|
    if String === value
      value = validate(value, form)
    else
      form.add_error(self, :not_accepted) unless acceptable_file?(value)

      size += value.size
    end

    value = Array(form.send(name)).push(value) if multiple?

    form.send(:"#{name}=", value)
  end

  form.add_error(self, :too_large) if maxsize? && size > maxsize
end

#valuesObject



126
127
128
# File 'lib/formeze/field.rb', line 126

def values
  @options.fetch(:values)
end

#values?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/formeze/field.rb', line 122

def values?
  @options.key?(:values)
end