Class: Worklog::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/worklog/dsl.rb

Overview

This class provides DSL for timelogging

Examples:

sheet = DSL.build() do
  title "Worklog"
  date_format "%Y-%m-%d"
  hourly_rate 20

  track '2021-07-10', spent: '8h', task: 'work on func requirements'
  track '2021-07-11', spent: '8h', task: 'work on func requirements'
  track '2021-07-12', spent: '8h30m', task: 'work on use cases'
  track '2021-07-13', spent: '30m', task: 'work on user stories'
  track '2021-07-13', spent: '7h30m', task: 'administrator user stories'
end
sheet = DSL.build() { read File.read('project.timesheet') }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDSL

Returns a new instance of DSL.



36
37
38
# File 'lib/worklog/dsl.rb', line 36

def initialize
  @sheet = Sheet.new
end

Instance Attribute Details

#sheetObject (readonly)

Returns the value of attribute sheet.



26
27
28
# File 'lib/worklog/dsl.rb', line 26

def sheet
  @sheet
end

Class Method Details

.build(&block) ⇒ Object



28
29
30
31
32
# File 'lib/worklog/dsl.rb', line 28

def self.build(&block)
  dsl = new
  dsl.instance_eval(&block) if block_given?
  dsl.sheet
end

Instance Method Details

#author(author) ⇒ Object



44
45
46
# File 'lib/worklog/dsl.rb', line 44

def author(author)
  @sheet.author = author
end

#date_format(fmt) ⇒ Object



48
49
50
# File 'lib/worklog/dsl.rb', line 48

def date_format(fmt)
  @sheet.date_format = fmt
end

#hourly_rate(rate) ⇒ Object



52
53
54
# File 'lib/worklog/dsl.rb', line 52

def hourly_rate(rate)
  @sheet.hourly_rate = rate
end

#parse_spent(time) ⇒ Object

Raises:

  • (ArgumentError)


76
77
78
79
80
81
82
83
# File 'lib/worklog/dsl.rb', line 76

def parse_spent(time)
  regx = /^((\d{1,2})[hH])?\s?((\d{1,2})[mM])?$/
  data = time.match(regx)
  raise ArgumentError, "\"spent\" format \"\d{1,2}[Hh]d{1,2}[Mm]\"" unless data
  h = data[2] || 0
  m = data[4] || 0
  h.to_i * 60 + m.to_i
end

#read(text) ⇒ Object

TODO: error handling



57
58
59
# File 'lib/worklog/dsl.rb', line 57

def read(text)
  instance_eval text
end

#read_file(filename) ⇒ Object



61
62
63
64
65
# File 'lib/worklog/dsl.rb', line 61

def read_file(filename)
  text = File.read(filename)
  read text
  @sheet.source = filename
end

#title(title) ⇒ Object



40
41
42
# File 'lib/worklog/dsl.rb', line 40

def title(title)
  @sheet.title = title
end

#track(date, spent:, task: '', rate: nil) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/worklog/dsl.rb', line 67

def track(date, spent:, task: '', rate: nil)
  @sheet.track(
    Date.strptime(date, @sheet.date_format),
    spent: parse_spent(spent),
    rate: rate,
    task: task
  )
end