syc-task 0.4.2 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/syctask/task.rb CHANGED
@@ -1,30 +1,28 @@
1
1
  require 'fileutils'
2
2
  require 'rainbow'
3
3
  require_relative 'evaluator'
4
- require_relative 'environment.rb'
5
- require_relative 'task_tracker.rb'
4
+ require_relative 'environment'
5
+ require_relative 'task_tracker'
6
6
 
7
7
  # Syctask provides functions for managing tasks in a task list
8
8
  module Syctask
9
-
10
9
  # A Task is the basic element of the task list and holds all information
11
10
  # about a task.
12
11
  class Task
13
-
14
12
  include Comparable
15
13
 
16
14
  # The fields that can be set for a task
17
- FIELDS = ["title", "description",
18
- "follow_up", "due_date", "prio",
19
- "note", "tags"]
20
- # Holds the options of the task.
15
+ FIELDS = %w[title description
16
+ follow_up due_date prio
17
+ note tags]
18
+ # Holds the options of the task.
21
19
  # Options are
22
20
  # * description - additional information about the task
23
21
  # * follow_up - follow-up date of the task
24
22
  # * due_date - due date of the task
25
23
  # * prio - priority of the task
26
24
  # * note - information about the progress or state of the task
27
- # * tags - can be used to search for tasks that belong to a certain
25
+ # * tags - can be used to search for tasks that belong to a certain
28
26
  # category
29
27
  attr_accessor :options
30
28
  # Title of the class
@@ -48,12 +46,14 @@ module Syctask
48
46
 
49
47
  # Creates a new task. If the options contain a note than the current date
50
48
  # and time is added.
51
- def initialize(options={}, title, id)
52
- @creation_date = Time.now.strftime("%Y-%m-%d - %H:%M:%S")
49
+ def initialize(options = {}, title, id)
50
+ @creation_date = Time.now.strftime('%Y-%m-%d - %H:%M:%S')
53
51
  @title = title
54
52
  @options = options
55
- @options[:note] =
56
- "#{@creation_date}\n#{@options[:note]}\n" if @options[:note]
53
+ if @options[:note]
54
+ @options[:note] =
55
+ "#{@creation_date}\n#{@options[:note]}\n"
56
+ end
57
57
  if @options[:follow_up] or @options[:due_date]
58
58
  @duration = 2 * 15 * 60
59
59
  @remaining = 2 * 15 * 60
@@ -63,14 +63,14 @@ module Syctask
63
63
  end
64
64
  @id = id
65
65
  end
66
-
67
- # Compares this task with another task regarding id and dir. If both are
66
+
67
+ # Compares this task with another task regarding id and dir. If both are
68
68
  # equal true is returned otherwise false
69
69
  def ==(other)
70
70
  @id == other.id and @dir == other.dir
71
71
  end
72
72
 
73
- # Compares this Task to the other task and compares them regarding the ID
73
+ # Compares this Task to the other task and compares them regarding the ID
74
74
  # and the dir. If ID is equal then dir is compared
75
75
  def <=>(other)
76
76
  id_compare = @id.to_i <=> other.id.to_i
@@ -84,7 +84,7 @@ module Syctask
84
84
  # Updates the task with new values. Except for note and tags which are
85
85
  # supplemented with the new values and not overridden.
86
86
  def update(options)
87
- @update_date = Time.now.strftime("%Y-%m-%d - %H:%M:%S")
87
+ @update_date = Time.now.strftime('%Y-%m-%d - %H:%M:%S')
88
88
  if options[:duration]
89
89
  set_duration(options.delete(:duration).to_i * 15 * 60)
90
90
  elsif options[:follow_up] or options[:due_date]
@@ -92,22 +92,22 @@ module Syctask
92
92
  end
93
93
  options.keys.each do |key|
94
94
  new_value = options[key]
95
-
95
+
96
96
  case key
97
97
  when :note
98
98
  new_value = "#{@update_date}\n#{new_value}\n#{@options[key]}"
99
99
  when :tags
100
100
  unless @options[key].nil?
101
- if @options[key].include? new_value
102
- new_value = @options[key]
103
- else
104
- new_value = "#{@options[key]},#{new_value}"
105
- end
101
+ new_value = if @options[key].include? new_value
102
+ @options[key]
103
+ else
104
+ "#{@options[key]},#{new_value}"
105
+ end
106
106
  end
107
107
  end
108
108
 
109
109
  @options[key] = new_value
110
- end
110
+ end
111
111
  end
112
112
 
113
113
  # Checks whether this task has been updated. Returns true if updated
@@ -140,12 +140,10 @@ module Syctask
140
140
 
141
141
  # Marks the task as done. When done than the done date is set. Optionally a
142
142
  # note can be provided.
143
- def done(note="")
144
- @done_date = Time.now.strftime("%Y-%m-%d - %H:%M:%S")
145
- if note
146
- options[:note] = "#{@done_date}\n#{note}\n#{@options[:note]}"
147
- end
148
- Syctask::log_task("done", self)
143
+ def done(note = '')
144
+ @done_date = Time.now.strftime('%Y-%m-%d - %H:%M:%S')
145
+ options[:note] = "#{@done_date}\n#{note}\n#{@options[:note]}" if note
146
+ Syctask.log_task('done', self)
149
147
  end
150
148
 
151
149
  # Checks if this task is done. Returns true if done otherwise false
@@ -157,9 +155,9 @@ module Syctask
157
155
  # date is today otherwise false.
158
156
  def today?
159
157
  evaluator = Evaluator.new
160
- today = Time.now.strftime("%Y-%m-%d")
158
+ today = Time.now.strftime('%Y-%m-%d')
161
159
  evaluator.compare_dates(@options[:follow_up], today) or \
162
- evaluator.compare_dates(@options[:due_date], today)
160
+ evaluator.compare_dates(@options[:due_date], today)
163
161
  end
164
162
 
165
163
  # Checks whether the task is currently tracked. Returns true if so otherwise
@@ -173,7 +171,7 @@ module Syctask
173
171
  # Compares the provided elements in the filter with the correspondent
174
172
  # elements in the task. When all comparissons match than true is returned.
175
173
  # If one comparisson does not match false is returned. If filter is empty
176
- # than true is returned. The values can be compared regarding <, =, > or
174
+ # than true is returned. The values can be compared regarding <, =, > or
177
175
  # whether the task's value is part of a list of provided values. It is also
178
176
  # possible to provide a regex as a filter. Following comparissons are
179
177
  # available
@@ -187,6 +185,7 @@ module Syctask
187
185
  # :due <|=|>
188
186
  def matches?(filter = {})
189
187
  return true if filter.empty?
188
+
190
189
  evaluator = Evaluator.new
191
190
  filter.each do |key, value|
192
191
  matches = false
@@ -195,8 +194,8 @@ module Syctask
195
194
  matches = evaluator.matches?(@title, value)
196
195
  when :description
197
196
  matches = evaluator.matches?(@options[:description], value)
198
- when :id, :i, "id", "i"
199
- matches = (evaluator.includes?(@id, value) or
197
+ when :id, :i, 'id', 'i'
198
+ matches = (evaluator.includes?(@id, value) or
200
199
  evaluator.compare_numbers(@id, value))
201
200
  when :prio, :p
202
201
  matches = (evaluator.includes?(@options[:prio], value) or
@@ -213,7 +212,7 @@ module Syctask
213
212
 
214
213
  # Prints the task in a formatted way eather all values when long is true
215
214
  # or only id, title, prio, follow-up and due date.
216
- def print_pretty(long=false)
215
+ def print_pretty(long = false)
217
216
  pretty_string(long)
218
217
  end
219
218
 
@@ -235,12 +234,12 @@ module Syctask
235
234
  def create_task_id
236
235
  tasks = dir.glob("#{@dir}/*")
237
236
  ids = []
238
- tasks.each {|task| ids << task.scan(/^\d+(?=\.task)/)[0].to_i }
239
- if ids.empty?
240
- @id = 1
241
- elsif
242
- @id = ids[ids.size-1] + 1
243
- end
237
+ tasks.each { |task| ids << task.scan(/^\d+(?=\.task)/)[0].to_i }
238
+ @id = if ids.empty?
239
+ 1
240
+ else
241
+ ids[ids.size - 1] + 1
242
+ end
244
243
  end
245
244
 
246
245
  # Prints the task formatted. Values that are nil are not printed. A type all
@@ -248,41 +247,53 @@ module Syctask
248
247
  # prio, follow-up and due date are printed.
249
248
  def pretty_string(long)
250
249
  color = :default
251
- color = :green if self.done?
252
-
250
+ color = :green if done?
251
+
253
252
  title = split_lines(@title, 70)
254
- title = title.chomp.gsub(/\n/, "\n#{' '*7}")
255
- title << ">" if !options[:note].nil?
256
- puts sprintf("%04d - %s", @id, title.bright).color(color)
253
+ title = title.chomp.gsub(/\n/, "\n#{' ' * 7}")
254
+ title << '>' unless options[:note].nil?
255
+ puts format('%04d - %s', @id, title.bright).color(color)
257
256
 
258
257
  if @options[:description]
259
258
  description = split_lines(@options[:description].chomp, 70)
260
- description = description.chomp.gsub(/\n/, "\n#{' '*7}")
261
- puts sprintf("%6s %s", " ", description.chomp).color(color)
259
+ description = description.chomp.gsub(/\n/, "\n#{' ' * 7}")
260
+ puts format('%6s %s', ' ', description.chomp).color(color)
262
261
  end
263
- puts sprintf("%6s Prio: %s", " ", @options[:prio]).
264
- color(color) if @options[:prio]
265
- puts sprintf("%6s Follow-up: %s", " ", @options[:follow_up]).
266
- color(color) if @options[:follow_up]
267
- puts sprintf("%6s Due: %s", " ", @options[:due_date]).
268
- color(color) if @options[:due_date]
269
- if long
270
- if @options[:note]
271
- note = split_lines(@options[:note].chomp, 70)
272
- note = note.chomp.
273
- gsub(/\n(?!\d{4}-\d{2}-\d{2} - \d{2}:\d{2}:\d{2})/, "\n#{' '*9}")
274
- note = note.
275
- gsub(/\n(?=\d{4}-\d{2}-\d{2} - \d{2}:\d{2}:\d{2})/, "\n#{' '*7}")
276
- puts sprintf("%6s %s", " ", note.chomp).color(color)
277
- end
278
- puts sprintf("%6s Tags: %s", " ", @options[:tags]).
279
- color(color) if @options[:tags]
280
- puts sprintf("%6s Created: %s", " ", @creation_date).color(color)
281
- puts sprintf("%6s Updated: %s", " ", @update_date).
282
- color(color) if @update_date
283
- puts sprintf("%6s Closed: %s", " ", @done_date).
284
- color(color) if @done_date
262
+ if @options[:prio]
263
+ puts format('%6s Prio: %s', ' ', @options[:prio])
264
+ .color(color)
265
+ end
266
+ if @options[:follow_up]
267
+ puts format('%6s Follow-up: %s', ' ', @options[:follow_up])
268
+ .color(color)
269
+ end
270
+ if @options[:due_date]
271
+ puts format('%6s Due: %s', ' ', @options[:due_date])
272
+ .color(color)
273
+ end
274
+ return unless long
275
+
276
+ if @options[:note]
277
+ note = split_lines(@options[:note].chomp, 70)
278
+ note = note.chomp
279
+ .gsub(/\n(?!\d{4}-\d{2}-\d{2} - \d{2}:\d{2}:\d{2})/, "\n#{' ' * 9}")
280
+ note = note
281
+ .gsub(/\n(?=\d{4}-\d{2}-\d{2} - \d{2}:\d{2}:\d{2})/, "\n#{' ' * 7}")
282
+ puts format('%6s %s', ' ', note.chomp).color(color)
283
+ end
284
+ if @options[:tags]
285
+ puts format('%6s Tags: %s', ' ', @options[:tags])
286
+ .color(color)
287
+ end
288
+ puts format('%6s Created: %s', ' ', @creation_date).color(color)
289
+ if @update_date
290
+ puts format('%6s Updated: %s', ' ', @update_date)
291
+ .color(color)
285
292
  end
293
+ return unless @done_date
294
+
295
+ puts format('%6s Closed: %s', ' ', @done_date)
296
+ .color(color)
286
297
  end
287
298
 
288
299
  # Prints all values as a csv separated with ";". This string can be read by
@@ -293,22 +304,22 @@ module Syctask
293
304
  string = "#{@id};#{@title};"
294
305
  string += "#{@options[:description]};#{@options[:prio]};"
295
306
  string += "#{@options[:follow_up]};#{@options[:due_date]};"
296
- string += "#{@options[:note] ? @options[:note].gsub(/\n/, '\\n') : ""};"
307
+ string += "#{@options[:note] ? @options[:note].gsub(/\n/, '\\n') : ''};"
297
308
  string += "#{@options[:tags]};"
298
309
  string += "#{@creation_date};"
299
- string += "#{@udpate_date ? "UPDATED" : "UNCHANGED"};"
300
- string += "#{@done_date ? "DONE" : "OPEN"}"
310
+ string += "#{@udpate_date ? 'UPDATED' : 'UNCHANGED'};"
311
+ string += "#{@done_date ? 'DONE' : 'OPEN'}"
301
312
  string
302
313
  end
303
314
 
304
315
  # Splits a string to size (chars) less or equal to length
305
316
  def split_lines(string, length)
306
- lines = string.squeeze(" ").split("\n")
317
+ lines = string.squeeze(' ').split("\n")
307
318
  i = 0
308
319
  new_lines = []
309
- new_lines[i] = ""
320
+ new_lines[i] = ''
310
321
  lines.each do |line|
311
- line.squeeze(" ").split.each do |w|
322
+ line.squeeze(' ').split.each do |w|
312
323
  if new_lines[i].length + w.length < length
313
324
  new_lines[i] += "#{w} "
314
325
  else
@@ -317,13 +328,11 @@ module Syctask
317
328
  end
318
329
  end
319
330
  i += 1
320
- new_lines[i] = ""
331
+ new_lines[i] = ''
321
332
  end
322
- text = ""
323
- new_lines.each {|l| text << "#{l}\n"}
333
+ text = ''
334
+ new_lines.each { |l| text << "#{l}\n" }
324
335
  text.chomp
325
336
  end
326
-
327
337
  end
328
-
329
338
  end
@@ -91,7 +91,7 @@ module Syctask
91
91
  case choice
92
92
  when 'e'
93
93
  task_file = "#{task.dir}/#{task.id}.task"
94
- system "vi #{task_file}" if File.exists? task_file
94
+ system "vi #{task_file}" if File.exist? task_file
95
95
  tasks[index] = @service.read(task.dir, task.id)
96
96
  redo
97
97
  when 'd'
@@ -108,7 +108,7 @@ module Syctask
108
108
  del = @service.delete(task.dir, {id: task.id.to_s}) if answer == "Y"
109
109
  if del.nil? or del == 0
110
110
  puts sprintf("--> Task not deleted").color(:green)
111
- else del > 0
111
+ elsif del > 0
112
112
  tasks.delete(task)
113
113
  puts sprintf("--> Deleted %d task%s",
114
114
  del, del == 1 ? "" : "s").color(:green)
@@ -275,7 +275,7 @@ module Syctask
275
275
  task = @service.read(dir, id)
276
276
  tasks << task if not task.nil? and task.matches?(filter)
277
277
  end
278
- end if File.exists? @todo_today_file
278
+ end if File.exist? @todo_today_file
279
279
  tasks
280
280
  end
281
281
 
@@ -297,7 +297,7 @@ module Syctask
297
297
  # overriden otherwise the tasks are appended
298
298
  def save_tasks(tasks, override=false)
299
299
  mode = override ? 'w' : 'a'
300
- FileUtils.mkdir_p WORK_DIR unless File.exists? WORK_DIR
300
+ FileUtils.mkdir_p WORK_DIR unless File.exist? WORK_DIR
301
301
  File.open(@todo_today_file, mode) do |file|
302
302
  tasks.each do |task|
303
303
  file.puts("#{task.dir},#{task.id}")
@@ -153,7 +153,7 @@ module Syctask
153
153
  busy_time: busy_time,
154
154
  meetings: meetings,
155
155
  assignments: assignments}
156
- FileUtils.mkdir WORK_DIR unless File.exists? WORK_DIR
156
+ FileUtils.mkdir WORK_DIR unless File.exist? WORK_DIR
157
157
  state_file = WORK_DIR+'/'+Time.now.strftime("%Y-%m-%d_time_schedule")
158
158
  File.open(state_file, 'w') do |file|
159
159
  YAML.dump(state, file)
@@ -164,8 +164,8 @@ module Syctask
164
164
  # time, meetings and assignments
165
165
  def restore_state
166
166
  state_file = WORK_DIR+'/'+Time.now.strftime("%Y-%m-%d_time_schedule")
167
- return [[], [], [], []] unless File.exists? state_file
168
- state = YAML.load_file(state_file)
167
+ return [[], [], [], []] unless File.exist? state_file
168
+ state = YAML.safe_load_file(state_file, permitted_classes: [Syctask::Task, Symbol])
169
169
  if state
170
170
  [state[:work_time],
171
171
  state[:busy_time],
@@ -37,7 +37,7 @@ module Syctask
37
37
  return task unless task.nil?
38
38
  #task = nil
39
39
  Dir.glob("#{dir}/*.task").each do |file|
40
- task = YAML.load_file(file) if File.file? file
40
+ task = YAML.safe_load_file(file, permitted_classes: [Syctask::Task, Symbol]) if File.file? file
41
41
  if not task.nil? and task.class == Syctask::Task and task.id == id.to_i
42
42
  return task
43
43
  end
@@ -50,10 +50,11 @@ module Syctask
50
50
  # Note: This method might return nil even though the task exists. You should
51
51
  # always use #read instead.
52
52
  def read_by_id(id)
53
- return nil unless File.exists? Syctask::IDS
53
+ return nil unless File.exist? Syctask::IDS
54
54
  ids = File.read(Syctask::IDS)
55
55
  entry = ids.scan(/(^#{id}),(.*\n)/)[0]
56
- return YAML.load_file(entry[1].chomp) if entry
56
+ return YAML.safe_load_file(entry[1].chomp,
57
+ permitted_classes: [Syctask::Task, Symbol]) if entry
57
58
  return nil
58
59
  end
59
60
 
@@ -71,7 +72,7 @@ module Syctask
71
72
  tasks = []
72
73
  Dir.glob("#{dir}/*.task").sort.each do |file|
73
74
  begin
74
- File.file?(file) ? task = YAML.load_file(file) : next
75
+ File.file?(file) ? task = YAML.safe_load_file(file, permitted_classes: [Syctask::Task, Symbol]) : next
75
76
  rescue Exception => e
76
77
  next # If the file is no task but read by YAML ignore it
77
78
  end
@@ -98,7 +99,7 @@ module Syctask
98
99
  task = read_by_id(id)
99
100
  unless task
100
101
  task_file = Dir.glob("#{dir}/#{id}.task")[0]
101
- task = YAML.load_file(task_file) if task_file
102
+ task = YAML.safe_load_file(task_file, permitted_classes: [Syctask::Task, Symbol]) if task_file
102
103
  end
103
104
  updated = false
104
105
  if task
@@ -117,7 +118,7 @@ module Syctask
117
118
  deleted = 0
118
119
  Dir.glob("#{dir}/*.task").each do |file|
119
120
  begin
120
- File.file?(file) ? task = YAML.load_file(file) : next
121
+ File.file?(file) ? task = YAML.safe_load_file(file, permitted_classes: [Syctask::Task, Symbol]) : next
121
122
  rescue Exception => e
122
123
  next # If the file is no task but read by YAML ignore it
123
124
  end
@@ -137,7 +138,7 @@ module Syctask
137
138
  def save(dir, task)
138
139
  task.dir = dir.nil? ? DEFAULT_DIR : File.expand_path(dir)
139
140
  task_file = "#{task.dir}/#{task.id}.task"
140
- unless File.exists? task_file
141
+ unless File.exist? task_file
141
142
  File.open(Syctask::IDS, 'a') {|f| f.puts "#{task.id},#{task_file}"}
142
143
  end
143
144
  File.open(task_file, 'w') {|f| YAML.dump(task, f)}
@@ -147,7 +148,7 @@ module Syctask
147
148
 
148
149
  # Creates the task directory if it does not exist
149
150
  def create_dir(dir)
150
- FileUtils.mkdir_p dir unless File.exists? dir
151
+ FileUtils.mkdir_p dir unless File.exist? dir
151
152
  end
152
153
 
153
154
  # Checks for the next possible task's ID based on the tasks available in
@@ -169,7 +170,7 @@ module Syctask
169
170
  # the next ID.
170
171
  def next_id(dir)
171
172
  local = local_id(dir)
172
- id = File.readlines(Syctask::ID)[0] if File.exists? Syctask::ID
173
+ id = File.readlines(Syctask::ID)[0] if File.exist? Syctask::ID
173
174
  id = id ? id.to_i + 1 : 1
174
175
  STDERR.puts "Warning: global id < local id" if id < local
175
176
  id = [id, local].max
@@ -2,7 +2,7 @@ require 'yaml'
2
2
  require 'fileutils'
3
3
  require_relative 'environment.rb'
4
4
  require_relative 'task_service.rb'
5
- #require_relative '../sycutil/console_timer.rb'
5
+ # require_relative '../sycutil/console_timer.rb'
6
6
 
7
7
  module Syctask
8
8
 
@@ -86,7 +86,7 @@ module Syctask
86
86
 
87
87
  # Saves the tracks to the tracked tasks file
88
88
  def save_tracks
89
- FileUtils.mkdir_p WORK_DIR unless File.exists? WORK_DIR
89
+ FileUtils.mkdir_p WORK_DIR unless File.exist? WORK_DIR
90
90
  File.open(TRACKED_TASKS_FILE, 'w') do |file|
91
91
  YAML.dump(@tracks, file)
92
92
  end
@@ -96,11 +96,15 @@ module Syctask
96
96
  # @tracks. If no tracked tasks exist @tracks and @tasks will be
97
97
  # empty
98
98
  def load_tracks
99
- unless File.exists? TRACKED_TASKS_FILE
99
+ unless File.exist? TRACKED_TASKS_FILE
100
100
  @tracks = []
101
101
  @tasks = []
102
102
  else
103
- @tracks ||= YAML.load_file(TRACKED_TASKS_FILE)
103
+ @tracks ||= YAML.safe_load_file(TRACKED_TASKS_FILE,
104
+ permitted_classes: [Syctask::Task,
105
+ Syctask::Track,
106
+ Time,
107
+ Symbol])
104
108
  @tasks = []
105
109
  if @tracks
106
110
  @tracks.each { |track| @tasks << @service.read(track.dir, track.id) }
@@ -112,7 +116,7 @@ module Syctask
112
116
 
113
117
  # Logs the start and stop of a task.
114
118
  def log_task(type, track)
115
- FileUtils.mkdir_r Syctask::WORK_DIR unless File.exists? Syctask::WORK_DIR
119
+ FileUtils.mkdir_r Syctask::WORK_DIR unless File.exist? Syctask::WORK_DIR
116
120
  File.open(TASK_LOG_FILE, 'a') do |file|
117
121
  log_entry = "#{type.to_s};"
118
122
  log_entry += "#{track.id};#{track.dir};"
@@ -161,7 +165,7 @@ module Syctask
161
165
 
162
166
  # Stops the task tracking and returns the lead time of the task
163
167
  def stop
164
- FileUtils.rm @semaphore if @semaphore and File.exists? @semaphore
168
+ FileUtils.rm @semaphore if @semaphore and File.exist? @semaphore
165
169
  @stopped ||= Time.now
166
170
  @stopped - @started
167
171
  end
@@ -1,5 +1,5 @@
1
1
  # Syctask provides functions for managing tasks in a task list
2
2
  module Syctask
3
- #Holds the version number of syctask
4
- VERSION = '0.4.2'
3
+ # Holds the version number of syctask
4
+ VERSION = '1.0.0'
5
5
  end
@@ -1,9 +1,8 @@
1
1
  require 'time'
2
- require 'syctimeleap/time_leap.rb'
2
+ require 'syctimeleap/time_leap'
3
3
 
4
4
  # Functions for time operations
5
5
  module Syctime
6
-
7
6
  # Translates seconds to years, months, weeks, days, hours, minutes and seconds
8
7
  # The return value is an array [seconds,...,years]
9
8
  def seconds_to_time(seconds)
@@ -21,9 +20,9 @@ module Syctime
21
20
  # Translates seconds into a time string like 1 year 2 weeks 5 days 10 minutes.
22
21
  def string_for_seconds(seconds)
23
22
  time = seconds_to_time(seconds)
24
- time_name = ['year','month','week','day','hour','minute','second']
25
- time_string = ""
26
- time.reverse.each_with_index do |part,index|
23
+ time_name = %w[year month week day hour minute second]
24
+ time_string = ''
25
+ time.reverse.each_with_index do |part, index|
27
26
  time_string << part.to_s + ' ' + time_name[index] + ' ' if part == 1
28
27
  time_string << part.to_s + ' ' + time_name[index] + 's ' if part > 1
29
28
  end
@@ -35,29 +34,30 @@ module Syctime
35
34
  def separated_time_string(seconds, separator)
36
35
  secs = seconds % 60
37
36
  mins = seconds / 60 % 60
38
- hours = seconds / 60 / 60
39
- time_string = sprintf("%02d#{separator}%02d#{separator}%02d", hours, mins, secs)
37
+ hours = seconds / 60 / 60
38
+ format("%02d#{separator}%02d#{separator}%02d", hours, mins, secs)
40
39
  end
41
40
 
42
41
  # Translates a time in the ISO 8601 schema to a time object.
43
42
  # 2013-04-09 21:45 -200
44
43
  def time_for_string(time)
45
- time = time.scan(/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/)[0].sub(' ','T')
44
+ time = time.scan(/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/)[0].sub(' ', 'T')
46
45
  Time.xmlschema(time)
47
46
  end
48
-
47
+
49
48
  # Tests whether the date is between from and to. Returns true then otherwise
50
49
  # false. Time, from and to are Time objects as retrieved from Time.now or
51
50
  # Time.local(2013,"apr",13,10,50,0). Alternatively time strings can be
52
51
  # provided in the form of "2013-04-13".
53
52
  def date_between?(date, from, to)
54
- date = date.strftime("%Y-%m-%d") if date.class == Time || date.class == Date
55
- from = from.strftime("%Y-%m-%d") if from.class == Time || from.class == Date
56
- to = to.strftime("%Y-%m-%d") if to.class == Time || to.class == Date
53
+ date = date.strftime('%Y-%m-%d') if date.class == Time || date.class == Date
54
+ from = from.strftime('%Y-%m-%d') if from.class == Time || from.class == Date
55
+ to = to.strftime('%Y-%m-%d') if to.class == Time || to.class == Date
57
56
  time_pattern = /\d{4}-\d{2}-\d{2}/
58
57
  raise ArgumentError if date.scan(time_pattern).empty?
59
58
  raise ArgumentError if from.scan(time_pattern).empty?
60
59
  raise ArgumentError if to.scan(time_pattern).empty?
60
+
61
61
  date >= from && date <= to
62
62
  end
63
63
 
@@ -73,28 +73,28 @@ module Syctime
73
73
  if date_string.match(/\d{4}-\d{2}-\d{2}/)
74
74
  begin
75
75
  Date.parse(date_string)
76
- return true
76
+ true
77
77
  rescue ArgumentError
78
- return false
78
+ false
79
79
  end
80
80
  else
81
81
  begin
82
82
  SycTimeleap::TimeLeap.new.send(date_string)
83
- return true
84
- rescue
85
- return false
83
+ true
84
+ rescue StandardError
85
+ false
86
86
  end
87
87
  end
88
88
  end
89
89
 
90
90
  # Extracts the time out of a time string. Accepts 'today', 'tomorrow',
91
- # 'yesterday' or a date in the form 'YYYY-MM-DD'. Returns the date contained
91
+ # 'yesterday' or a date in the form 'YYYY-MM-DD'. Returns the date contained
92
92
  # in the time_string or if time = true in a Time object
93
- def extract_time(time_string,time=false)
93
+ def extract_time(time_string, time = false)
94
94
  time_string = 'today' if time_string.nil?
95
95
 
96
96
  if time_string.match(/\d{4}-\d{2}-\d{2}/)
97
- date = time_string
97
+ date = time_string
98
98
  date = Time.xmlschema("#{time_string}T00:00:00") if time
99
99
  else
100
100
  timeleap = SycTimeleap::TimeLeap.new
@@ -102,20 +102,19 @@ module Syctime
102
102
  date = timeleap.send(time_string)
103
103
  date = date.to_s unless time
104
104
  rescue NoMethodError
105
- help_now! "Arguments may be 'time distances', YYYY-MM-DD or <RETURN>\n"+
106
- "\ntime distances are:\n"+
107
- "* yesterday|today|tomorrow\n"+
108
- "* next|previous_monday|tuesday|...|sunday\n"+
109
- "* in|back_10_days|weeks|months|years\n"+
110
- "* monday|tuesday|...|sunday_in|back_1_day|week|month|year\n"+
111
- "Short forms are also possible:\n"+
112
- "* y|tod|tom\n"+
113
- "* n|pmo|tu|we|th|fr|sa|su\n"+
114
- "* i|b10d|w|m|y\n"+
115
- "* mo|tu|we|th|fr|sa|sui|b1d|w|m|y"
116
- end
105
+ help_now! "Arguments may be 'time distances', YYYY-MM-DD or <RETURN>\n" +
106
+ "\ntime distances are:\n" +
107
+ "* yesterday|today|tomorrow\n" +
108
+ "* next|previous_monday|tuesday|...|sunday\n" +
109
+ "* in|back_10_days|weeks|months|years\n" +
110
+ "* monday|tuesday|...|sunday_in|back_1_day|week|month|year\n" +
111
+ "Short forms are also possible:\n" +
112
+ "* y|tod|tom\n" +
113
+ "* n|pmo|tu|we|th|fr|sa|su\n" +
114
+ "* i|b10d|w|m|y\n" +
115
+ '* mo|tu|we|th|fr|sa|sui|b1d|w|m|y'
116
+ end
117
117
  end
118
118
  date
119
119
  end
120
-
121
120
  end