Class: TextInputRow

Inherits:
BaseRow show all
Defined in:
lib/project/rows/text_input_row.rb

Direct Known Subclasses

ButtonRow, TextFieldRow

Constant Summary collapse

EMAIL_REGEX =
/^[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/
URL_REGEX =
/https?:\/\/[\S]+/

Instance Attribute Summary collapse

Attributes inherited from BaseRow

#key, #label, #options

Instance Method Summary collapse

Methods inherited from BaseRow

#cell_identifier, #dealloc, #has_value?, #notification_center, #observe, #post

Constructor Details

#initialize(key, options) ⇒ TextInputRow

Returns a new instance of TextInputRow.



10
11
12
13
14
15
16
17
18
# File 'lib/project/rows/text_input_row.rb', line 10

def initialize(key, options)
  super

  @value = options.fetch(:value, nil)

  setup_validation

  listen
end

Instance Attribute Details

#valueObject

Returns the value of attribute value.



8
9
10
# File 'lib/project/rows/text_input_row.rb', line 8

def value
  @value
end

Instance Method Details

#capitalize?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/project/rows/text_input_row.rb', line 66

def capitalize?
  options[:capitalize] || !(options[:email] || options[:secure])
end

#cell_typeObject



70
71
72
# File 'lib/project/rows/text_input_row.rb', line 70

def cell_type
  TextInputCell
end

#did_end_editing(notification) ⇒ Object



54
55
56
# File 'lib/project/rows/text_input_row.rb', line 54

def did_end_editing(notification)
  @value = notification.userInfo[:value] if notification.userInfo[:key] == key
end

#listenObject



50
51
52
# File 'lib/project/rows/text_input_row.rb', line 50

def listen
  observe('FormCellDidEndEditing', 'did_end_editing:')
end

#setup_validationObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/project/rows/text_input_row.rb', line 20

def setup_validation
  if options[:required]
    validation_rules << lambda { |value| value && value != '' }
  end

  if options[:email]
    validation_rules << lambda { |value| value && value[EMAIL_REGEX] }
  end

  if options[:url]
    validation_rules << lambda { |value| value && value[URL_REGEX] }
  end

  if options[:format] && options[:format].is_a?(Regexp)
    validation_rules << lambda { |value| value && value[options[:format]] }
  end

  if options[:validate_with] && options[:validate_with].is_a?(Proc)
    validation_rules << options[:validate_with]
  end
end

#update_cell(cell) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/project/rows/text_input_row.rb', line 58

def update_cell(cell)
  super

  cell.secure     = options[:secure]
  cell.value      = value
  cell.capitalize = capitalize?
end

#valid?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/project/rows/text_input_row.rb', line 42

def valid?
  validation_rules.all? { |rule| rule.call(value) }
end

#validation_rulesObject



46
47
48
# File 'lib/project/rows/text_input_row.rb', line 46

def validation_rules
  @validation_rules ||= []
end