Skip to content

Commit

Permalink
Added exceptions for empty or bad patterns, fixed tests, updated
Browse files Browse the repository at this point in the history
.gitignore file
  • Loading branch information
Sam Peters committed Sep 17, 2015
1 parent 83ad5e7 commit 7488d60
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ rdoc/
.DS_Store/
.DS_Store
lib/.DS_Store
Gemfile.lock
Gemfile.lock
.ruby-gemset
.ruby-version
Binary file added .gitignore.swp
Binary file not shown.
2 changes: 1 addition & 1 deletion lib/protokoll/counter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def self.next(object, options)
private

def self.outdated?(record, options)
Time.now.strftime(update_event(options)).to_i > record.updated_at.strftime(update_event(options)).to_i
Time.now.utc.strftime(update_event(options)).to_i > record.updated_at.strftime(update_event(options)).to_i
end

def self.update_event(options)
Expand Down
8 changes: 8 additions & 0 deletions lib/protokoll/protokoll.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def protokoll(column, _options = {})
:start => 0 }

options.merge!(_options)
raise ArgumentError.new("pattern can't be nil!") if options[:pattern].nil?
raise ArgumentError.new("pattern requires at least one counter symbol #{options[:number_symbol]}") unless pattern_includes_symbols?(options)

# Defining custom method
send :define_method, "reserve_#{options[:column]}!".to_sym do
Expand All @@ -30,6 +32,12 @@ def protokoll(column, _options = {})
end
end
end

private

def pattern_includes_symbols?(options)
options[:pattern].count(options[:number_symbol]) > 0
end
end

end
18 changes: 18 additions & 0 deletions test/protokoll_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,24 @@ class Protocol < ActiveRecord::Base
assert_equal "2011092601", protocol3.number
end

test "rejects empty patterns" do

assert_raise ArgumentError do
class Protocol < ActiveRecord::Base
protokoll :number, :pattern => nil
end
end
end

test "rejects invalid patterns" do

assert_raise ArgumentError do
class Protocol < ActiveRecord::Base
protokoll :number, :pattern => "%ydodgyPattern"
end
end
end

end


0 comments on commit 7488d60

Please sign in to comment.