Module: Briar::Picker::DateCore

Defined in:
lib/briar/picker/date_picker_core.rb

Instance Method Summary collapse

Instance Method Details

#convert_to_utc_from_options(options = {:convert_time_to_utc => false}) ⇒ Object



148
149
150
151
152
# File 'lib/briar/picker/date_picker_core.rb', line 148

def convert_to_utc_from_options(options={:convert_time_to_utc => false})
  return false if options.nil?
  return false if not options.has_key?(:convert_time_to_utc)
  options[:convert_time_to_utc]
end

#now_time_12h_localeObject



358
359
360
# File 'lib/briar/picker/date_picker_core.rb', line 358

def now_time_12h_locale
  now_times_map[:h12]
end

#now_time_24h_localeObject



354
355
356
# File 'lib/briar/picker/date_picker_core.rb', line 354

def now_time_24h_locale
  now_times_map[:h24]
end

#now_times_arrObject



362
363
364
# File 'lib/briar/picker/date_picker_core.rb', line 362

def now_times_arr
  [now_time_24h_locale, now_time_12h_locale]
end

#now_times_mapObject



342
343
344
345
346
347
348
349
350
351
352
# File 'lib/briar/picker/date_picker_core.rb', line 342

def now_times_map
  utc = Time.now.utc
  now = Time.now

  {:h12 => now.strftime(BRIAR_PICKER_12H_TIME_FMT).squeeze(' ').strip,
   :h24 => now.strftime(BRIAR_PICKER_24H_TIME_FMT).squeeze(' ').strip.sub(BRIAR_REMOVE_LEADING_ZERO_REGEX, ''),
   :h12_utc => utc.strftime(BRIAR_PICKER_12H_TIME_FMT).squeeze(' ').strip,
   :h24_utc => utc.strftime(BRIAR_PICKER_24H_TIME_FMT).squeeze(' ').strip.sub(BRIAR_REMOVE_LEADING_ZERO_REGEX, ''),
   :utc => utc,
   :time => now}
end

#picker_brief_date_str(format_key, options = {}) ⇒ Object



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/briar/picker/date_picker_core.rb', line 279

def picker_brief_date_str(format_key, options={})

  default_options = {:picker_id => nil,
                     :convert_time_to_utc => false,
                     :time_on_picker => nil,
                     :assert_mode => true}
  options = default_options.merge(options)

  picker_id = options[:picker_id]

  if options[:assert_mode]
    if picker_is_in_countdown_mode picker_id
      screenshot_and_raise 'date is not applicable for this mode'
    end
  end

  format = BRIAR_DATE_FORMATS[format_key]
  unless format
    screenshot_and_raise "format '#{format_key}' is not one of '#{BRIAR_DATE_FORMATS.keys}'"
  end

  time = options[:time_on_picker] || ruby_time_from_picker(options)
  time.strftime(format).strip.squeeze(' ')
end

#picker_brief_date_strs_arr(options = {}) ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/briar/picker/date_picker_core.rb', line 323

def picker_brief_date_strs_arr(options={})

  default_options = {:picker_id => nil,
                     :convert_time_to_utc => false,
                     :time_on_picker => nil,
                     :assert_mode => true}
  options = default_options.merge(options)

  picker_id = options[:picker_id]

  if options[:assert_mode]
    if picker_is_in_countdown_mode picker_id
      screenshot_and_raise 'date is not applicable for this mode'
    end
  end
  [picker_brief_date_str(:brief_24h, options), picker_brief_date_str(:brief_12h, options)]
end

#picker_brief_date_strs_hash(options = {}) ⇒ Object



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/briar/picker/date_picker_core.rb', line 304

def picker_brief_date_strs_hash(options={})

  default_options = {:picker_id => nil,
                     :convert_time_to_utc => false,
                     :time_on_picker => nil,
                     :assert_mode => true}
  options = default_options.merge(options)

  picker_id = options[:picker_id]

  if options[:assert_mode]
    if picker_is_in_countdown_mode picker_id
      screenshot_and_raise 'date is not applicable for this mode'
    end
  end
  {:brief_24h => picker_brief_date_str(:brief_24h, options),
   :brief_12h => picker_brief_date_str(:brief_12h, options)}
end

#picker_column_for_period(picker_id = nil) ⇒ Object

is the picker in 12h or 24h mode



193
194
195
196
197
198
# File 'lib/briar/picker/date_picker_core.rb', line 193

def picker_column_for_period(picker_id = nil)
  if picker_is_in_date_mode picker_id or picker_is_in_countdown_mode picker_id
    screenshot_and_raise '12h/24h mode is not applicable to this mode'
  end
  picker_is_in_time_mode ? 2 : 3
end

#picker_id_from_options(options = {:picker_id => nil}) ⇒ Object



144
145
146
# File 'lib/briar/picker/date_picker_core.rb', line 144

def picker_id_from_options (options={:picker_id => nil})
  options == nil ? nil : options[:picker_id]
end

#picker_is_in_12h_locale(picker_id = nil) ⇒ Object

12h or 24h locale



201
202
203
204
205
206
207
208
# File 'lib/briar/picker/date_picker_core.rb', line 201

def picker_is_in_12h_locale(picker_id = nil)
  if picker_is_in_date_mode picker_id or picker_is_in_countdown_mode picker_id
    screenshot_and_raise '12h/24h mode is not applicable to this mode'
  end

  column = picker_column_for_period(picker_id=nil)
  !query("pickerTableView index:#{column}").empty?
end

#picker_is_in_24h_locale(picker_id = nil) ⇒ Object



210
211
212
213
214
215
# File 'lib/briar/picker/date_picker_core.rb', line 210

def picker_is_in_24h_locale(picker_id=nil)
  if picker_is_in_date_mode picker_id or picker_is_in_countdown_mode picker_id
    screenshot_and_raise '12h/24h mode is not applicable to this mode'
  end
  !picker_is_in_12h_locale picker_id
end

#picker_is_in_countdown_mode(picker_id = nil) ⇒ Object



140
141
142
# File 'lib/briar/picker/date_picker_core.rb', line 140

def picker_is_in_countdown_mode(picker_id=nil)
  picker_mode(picker_id) == UIDatePickerModeCountDownTimer
end

#picker_is_in_date_and_time_mode(picker_id = nil) ⇒ Object



136
137
138
# File 'lib/briar/picker/date_picker_core.rb', line 136

def picker_is_in_date_and_time_mode(picker_id=nil)
  picker_mode(picker_id) == UIDatePickerModeDateAndTime
end

#picker_is_in_date_mode(picker_id = nil) ⇒ Object



132
133
134
# File 'lib/briar/picker/date_picker_core.rb', line 132

def picker_is_in_date_mode(picker_id=nil)
  picker_mode(picker_id) == UIDatePickerModeDate
end

#picker_is_in_time_mode(picker_id = nil) ⇒ Object



128
129
130
# File 'lib/briar/picker/date_picker_core.rb', line 128

def picker_is_in_time_mode(picker_id=nil)
  picker_mode(picker_id) == UIDatePickerModeTime
end

#picker_maximum_date_time(picker_id = nil) ⇒ Object

appledoc ==> The property is an NSDate object or nil (the default), which means no maximum date.



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/briar/picker/date_picker_core.rb', line 89

def picker_maximum_date_time (picker_id = nil)
  if picker_is_in_countdown_mode picker_id
    screenshot_and_raise 'method is not available for pickers that are not in date or date time mode'
  end

  query_str = should_see_date_picker picker_id
  res = query(query_str, :maximumDate)
  if res.empty?
    screenshot_and_raise "should be able to get max date from picker with query '#{query_str}'"
  end
  return nil if res.first.nil?
  DateTime.parse(res.first)
end

#picker_minimum_date_time(picker_id = nil) ⇒ Object

appledoc ==> The property is an NSDate object or nil (the default), which means no minimum date.



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/briar/picker/date_picker_core.rb', line 105

def picker_minimum_date_time (picker_id = nil)
  if picker_is_in_countdown_mode picker_id
    screenshot_and_raise 'method is not available for pickers that are not in date or date time mode'
  end

  query_str = should_see_date_picker picker_id
  res = query(query_str, :minimumDate)
  if res.empty?
    screenshot_and_raise "should be able to get min date from picker with query '#{query_str}'"
  end
  return nil if res.first.nil?
  DateTime.parse(res.first)
end

#picker_minute_interval(picker_id = nil) ⇒ Object

apple docs You can use this property to set the interval displayed by the minutes wheel (for example, 15 minutes). The interval value must be evenly divided into 60; if it is not, the default value is used. The default and minimum values are 1; the maximum value is 30.



159
160
161
162
163
164
165
166
167
# File 'lib/briar/picker/date_picker_core.rb', line 159

def picker_minute_interval(picker_id = nil)
  screenshot_and_raise 'there is no minute in date mode' if picker_is_in_date_mode
  query_str = should_see_date_picker picker_id
  res = query(query_str, :minuteInterval)
  if res.empty?
    screenshot_and_raise "should be able to get minute interval from picker with '#{query_str}'"
  end
  @picker_minute_interval = res.first
end

#picker_mode(picker_id = nil) ⇒ Object



119
120
121
122
123
124
125
126
# File 'lib/briar/picker/date_picker_core.rb', line 119

def picker_mode(picker_id=nil)
  query_str = should_see_date_picker picker_id
  res = query(query_str, :datePickerMode)
  if res.empty?
    screenshot_and_raise "should be able to get mode from picker with query '#{query_str}'"
  end
  res.first
end

#picker_time_str(format_key, options = {}) ⇒ Object

get the times off the picker in 12h and 24h format



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/briar/picker/date_picker_core.rb', line 218

def picker_time_str(format_key, options={})
  default_options = {:picker_id => nil,
                     :convert_time_to_utc => false,
                     :time_on_picker => nil,
                     :assert_mode => true}
  options = default_options.merge(options)

  picker_id = options[:picker_id]

  if options[:assert_mode]
    if picker_is_in_date_mode picker_id or picker_is_in_countdown_mode picker_id
      screenshot_and_raise 'the time is not applicable for this mode'
    end
  end

  format = BRIAR_TIME_FORMATS[format_key]
  unless format
    screenshot_and_raise "format '#{format_key}' was not one of '#{BRIAR_TIME_FORMATS}'"
  end

  time = options[:time_on_picker] || ruby_time_from_picker(options)
  time.strftime(format).strip.sub(BRIAR_REMOVE_LEADING_ZERO_REGEX, '')
end

#picker_time_strs_arr(options = {}) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/briar/picker/date_picker_core.rb', line 261

def picker_time_strs_arr(options={})

  default_options = {:picker_id => nil,
                     :convert_time_to_utc => false,
                     :time_on_picker => nil,
                     :assert_mode => true}
  options = default_options.merge(options)

  picker_id = options[:picker_id]

  if options[:assert_mode]
    if picker_is_in_date_mode picker_id or picker_is_in_countdown_mode picker_id
      screenshot_and_raise 'the time is not applicable for this mode'
    end
  end
  [picker_time_str(:h24, options), picker_time_str(:h12, options)]
end

#picker_time_strs_hash(options = {}) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/briar/picker/date_picker_core.rb', line 243

def picker_time_strs_hash(options={})
  default_options = {:picker_id => nil,
                     :convert_time_to_utc => false,
                     :time_on_picker => nil,
                     :assert_mode => true}
  options = default_options.merge(options)
  picker_id = options[:picker_id]

  if options[:assert_mode]
    if picker_is_in_date_mode picker_id or picker_is_in_countdown_mode picker_id
      screenshot_and_raise 'the time is not applicable for this mode'
    end
  end

  {:h24 => picker_time_str(:h24, options),
   :h12 => picker_time_str(:h12, options)}
end

#query_string_for_picker(picker_id = nil) ⇒ Object



31
32
33
# File 'lib/briar/picker/date_picker_core.rb', line 31

def query_string_for_picker (picker_id = nil)
  picker_id.nil? ? 'datePicker' : "datePicker marked:'#{picker_id}'"
end

#ruby_time_from_picker(options = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/briar/picker/date_picker_core.rb', line 41

def ruby_time_from_picker (options = {})
  if not options.is_a?(Hash) or options == nil
    if options == nil
      msg = "'picker_id' argument has been replaced with options hash; pass '{:picker_id => nil}' instead"
      options = {:picker_id => nil}
    else
      msg = "'picker_id' argument has been replaced with options hash; pass '{:picker_id => #{options}}' instead"
      options = {:picker_id => options}
    end
    _deprecated('0.1.3', msg, :warn)
  end

  default_options = {:picker_id => nil,
                     :convert_time_to_utc => false,
                     :assert_mode => true,
                     :assert_picker => true}
  options = default_options.merge(options)

  picker_id = options[:picker_id]

  if options[:assert_mode]
    if picker_is_in_countdown_mode picker_id
      screenshot_and_raise 'method is not available for pickers that are not in date or date time mode'
    end
  end

  if options[:assert_picker]
    should_see_date_picker picker_id
  end

  query_str = query_string_for_picker(picker_id)
  res = query(query_str, :date)
  if res.empty?
    screenshot_and_raise "should be able to get date from picker with query '#{query_str}'"
  end

  rdt = DateTime.parse(res.first)
  convert = convert_to_utc_from_options options
  if convert
    rdt.to_time.utc
  else
    rdt.to_time
  end
end

#should_see_date_picker(picker_id = nil) ⇒ Object



35
36
37
38
39
# File 'lib/briar/picker/date_picker_core.rb', line 35

def should_see_date_picker (picker_id=nil)
  query_str = query_string_for_picker picker_id
  wait_for_query query_str
  query_str
end

#time_hash_by_add_minutes_until_at_closest_interval(time_str, interval = picker_minute_interval) ⇒ Object

funky little bugger - used to change a picker to the nearest minute based on the minute interval



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/briar/picker/date_picker_core.rb', line 171

def time_hash_by_add_minutes_until_at_closest_interval (time_str, interval=picker_minute_interval)
  screenshot_and_raise "interval '#{interval}' is not on (0, 59) which is not allowed" unless (0..59).member?(interval)
  time = Time.parse(time_str)
  # normalize to zero
  time = time - time.sec
  minute = time.min
  count = 0
  unless (minute % interval) == 0
    begin
      minute = (minute > 59) ? 0 : minute + 1
      count = count + 1
    end
  end while ((minute % interval) != 0)
  time = time + (count * 60)

  {:h12 => time.strftime(BRIAR_PICKER_12H_TIME_FMT).squeeze(' ').strip,
   :h24 => time.strftime(BRIAR_PICKER_24H_TIME_FMT).squeeze(' ').strip.sub(BRIAR_REMOVE_LEADING_ZERO_REGEX, ''),
   :time => time}
end