Skip to content

Instantly share code, notes, and snippets.

@marka2g
Created January 31, 2012 19:47
Show Gist options
  • Select an option

  • Save marka2g/1712520 to your computer and use it in GitHub Desktop.

Select an option

Save marka2g/1712520 to your computer and use it in GitHub Desktop.
need an edge-case type test? simply add a new hash y'all!
describe Note do
describe 'notes with valid time entry' do
[ {text: 't:9h20m', parsed_entry: 33600},
{text: 't:20m', parsed_entry: 1200},
{text: 't: 9h', parsed_entry: 32400},
{text: 't: 20m', parsed_entry: 1200},
{text: 't: 09h', parsed_entry: 32400},
{text: 'time: 09h', parsed_entry: 32400},
{text: 'Time: 20m', parsed_entry: 1200}
].each do |note|
context "note with #{note[:text]}" do
subject { Note.new(text: note[:text]) }
describe '#time_entry?' do
it { subject.time_entry?.should be_true }
end
describe '#time_entry' do
it { subject.time_entry.should eql(note[:parsed_entry]) }
end
end
end
@marka2g

marka2g commented Jan 31, 2012

Copy link
Copy Markdown
Author

in note.rb

def time_entry?
text.downcase.start_with?('time:', 't:')
end

def time_entry
if match = text.match(/^(t|time):(\s+)?((?\d+h)?)((?\d+m)?)$/i)
(match[:minute].to_i * 60) + (match[:hour].to_i * 3600)
end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment