Skip to content
This repository has been archived by the owner on Dec 23, 2020. It is now read-only.

Commit

Permalink
Add time format validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
kommen committed Jan 12, 2009
1 parent b15161d commit 5cdf5a9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/freckle/cli.rb
Expand Up @@ -41,7 +41,12 @@ def self.execute(stdout, arguments=[])

entry = Freckle::Entry.new(entry_attributes.merge(:user => Freckle::AppConfig.user))
entry.project_name = options[:project_name] if options[:project_name]
entry.save
if entry.save
puts "time entry saved"
else
puts "failed to save entry."
entry.errors.full_messages.each { |msg| puts msg }
end
end
end
end
19 changes: 19 additions & 0 deletions lib/freckle/entry.rb
Expand Up @@ -4,6 +4,8 @@ module Freckle
class Entry < ActiveResource::Base
self.site = "http://#{Freckle::AppConfig.subdomain}.#{Freckle::AppConfig.host}:#{Freckle::AppConfig.port}/api/"

TIME_REGEXP = /^[0-9]{0,3}[:,.]?[0-9]{0,2}(m|min|mins|minute|minutes|h|hr|hrs|hour|hours|d|day|days)?$/

attr_accessor :project

def self.headers
Expand All @@ -17,5 +19,22 @@ def attributes
def project_name=(name)
self.project = Project.find_or_create_by_name(name)
end

def save
valid? ? super : false
end

def valid?
validate
super
end

protected

def validate
if minutes && !(minutes =~ TIME_REGEXP)
errors.add('minutes', 'has invalid format')
end
end
end
end
14 changes: 14 additions & 0 deletions test/project_test.rb
@@ -0,0 +1,14 @@
require File.join(File.dirname(__FILE__), "test_helper.rb")

class TestFreckleCli < Test::Unit::TestCase
def setup
@stdout_io = StringIO.new
Freckle::CLI.execute(@stdout_io, [])
@stdout_io.rewind
@stdout = @stdout_io.read
end

def test_not_print_default_output
assert_no_match(/To update this executable/, @stdout)
end
end
3 changes: 3 additions & 0 deletions test/test_helper.rb
@@ -1,3 +1,6 @@
require 'stringio'
require 'test/unit'
require 'mocha'
require File.dirname(__FILE__) + '/../lib/freckle'
require File.dirname(__FILE__) + '/../lib/freckle/cli'

0 comments on commit 5cdf5a9

Please sign in to comment.