Class: Worklog::DSL
- Inherits:
-
Object
- Object
- Worklog::DSL
- Defined in:
- lib/worklog/dsl.rb
Overview
This class provides DSL for timelogging
Instance Attribute Summary collapse
-
#sheet ⇒ Object
readonly
Returns the value of attribute sheet.
Class Method Summary collapse
Instance Method Summary collapse
- #author(author) ⇒ Object
- #date_format(fmt) ⇒ Object
- #hourly_rate(rate) ⇒ Object
-
#initialize ⇒ DSL
constructor
A new instance of DSL.
- #parse_spent(time) ⇒ Object
-
#read(text) ⇒ Object
TODO: error handling.
- #read_file(filename) ⇒ Object
- #title(title) ⇒ Object
- #track(date, spent:, task: '', rate: nil) ⇒ Object
Constructor Details
Instance Attribute Details
#sheet ⇒ Object (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 () @sheet. = 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
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 |