Skip to content

Commit

Permalink
Prefer increment and decrement over incr and decr.
Browse files Browse the repository at this point in the history
  • Loading branch information
frodsan committed Sep 20, 2015
1 parent e8b6e6f commit 6a3e484
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 30 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ querying. Keep reading to find out what you can do with models.
Attribute types
---------------

Ohm::Model provides 4 attribute types:
Ohm::Model provides 4 attribute types:

* {Ohm::Model.attribute attribute},
* {Ohm::Model.attribute attribute},
* {Ohm::Model.set set}
* {Ohm::Model.list list}
* {Ohm::Model.counter counter}
Expand Down Expand Up @@ -207,8 +207,8 @@ and for keeping elements in order.
A `counter` is like a regular attribute, but the direct manipulation
of the value is not allowed. You can retrieve, increase or decrease
the value, but you can not assign it. In the example above, we used a
counter attribute for tracking votes. As the incr and decr operations
are atomic, you can rest assured a vote won't be counted twice.
counter attribute for tracking votes. As the `increment` and `decrement`
operations are atomic, you can rest assured a vote won't be counted twice.

### reference

Expand Down
4 changes: 2 additions & 2 deletions examples/tagging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,14 @@ def tag(tags = self.tags)
protected
def before_update
assigned_tags = tags
tag(get(:tags)).map(&Tag).each { |t| t.decr :total }
tag(get(:tags)).map(&Tag).each { |t| t.decrement :total }
self.tags = assigned_tags
end

# And of course, we increment all new tags for a particular record
# after successfully saving it.
def after_save
tag.map(&Tag).each { |t| t.incr :total }
tag.map(&Tag).each { |t| t.increment :total }
end
end

Expand Down
21 changes: 11 additions & 10 deletions lib/ohm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@ def except(dict)
#
# # The result will include all users with active status
# # and with names "John" or "Jane".
#
def combine(dict)
Ohm::Set.new(
model, namespace, [:SINTER, key, [:SUNION, *model.filters(dict)]]
Expand Down Expand Up @@ -652,7 +653,7 @@ def replace(models)
# end
#
# u = User.create(:name => "John", :email => "[email protected]")
# u.incr :points
# u.increment :points
# u.posts.add(Post.create)
#
# When you execute `User.create(...)`, you run the following Redis
Expand Down Expand Up @@ -1041,9 +1042,9 @@ def self.attribute(name, cast = nil)

# Declare a counter. All the counters are internally stored in
# a different Redis hash, independent from the one that stores
# the model attributes. Counters are updated with the `incr` and
# `decr` methods, which interact directly with Redis. Their value
# can't be assigned as with regular attributes.
# the model attributes. Counters are updated with the `increment`
# and `decrement` methods, which interact directly with Redis. Their
# value can't be assigned as with regular attributes.
#
# Example:
#
Expand All @@ -1052,7 +1053,7 @@ def self.attribute(name, cast = nil)
# end
#
# u = User.create
# u.incr :points
# u.increment :points
#
# u.points
# # => 1
Expand Down Expand Up @@ -1196,17 +1197,17 @@ def new?
end

# Increment a counter atomically. Internally uses HINCRBY.
def incr(att, count = 1)
def increment(att, count = 1)
redis.call("HINCRBY", key[:counters], att, count)
end

# Decrement a counter atomically. Internally uses HINCRBY.
def decr(att, count = 1)
incr(att, -count)
def decrement(att, count = 1)
increment(att, -count)
end

alias_method(:increment, :incr)
alias_method(:decrement, :decr)
alias_method(:incr, :increment)
alias_method(:decr, :decrement)

# Return a value that allows the use of models as hash keys.
#
Expand Down
12 changes: 6 additions & 6 deletions test/counters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ class Ad < Ohm::Model
Ad.counter :hits

instance1 = Ad.create
instance1.incr :hits
instance1.increment :hits

instance2 = Ad[instance1.id]

instance1.incr :hits
instance1.incr :hits
instance1.increment :hits
instance1.increment :hits

instance2.save

Expand All @@ -31,7 +31,7 @@ class Ad < Ohm::Model
ex = nil

begin
ad.incr :hits
ad.increment :hits
rescue ArgumentError => e
ex = e
end
Expand All @@ -44,7 +44,7 @@ class Ad < Ohm::Model
Ad.counter :hits

ad = Ad.create(:name => "foo")
ad.incr :hits, 10
ad.increment :hits, 10
assert_equal 10, ad.hits

# Now let's just load and save it.
Expand All @@ -59,7 +59,7 @@ class Ad < Ohm::Model
# If we load and save again while we incr behind the scenes,
# the latest counter values should be respected.
ad = Ad[ad.id]
ad.incr :hits, 5
ad.increment :hits, 5
ad.save

ad = Ad[ad.id]
Expand Down
16 changes: 8 additions & 8 deletions test/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Foo < Ohm::Model

test "counters are cleaned up during deletion" do
e = Event.create(:name => "Foo")
e.incr :votes, 10
e.increment :votes, 10

assert_equal 10, e.votes

Expand Down Expand Up @@ -286,7 +286,7 @@ class RedefinedModel < Ohm::Model
test "save counters" do
event = Event.create(:name => "Foo")

event.incr(:votes)
event.increment(:votes)
event.save

assert_equal 1, Event[event.id].votes
Expand Down Expand Up @@ -561,7 +561,7 @@ def tag
@event = Event.create(:name => "Ruby Tuesday")
{'D' => 4, 'C' => 2, 'B' => 5, 'A' => 3}.each_pair do |name, logins|
person = Person.create(:name => name)
person.incr :logins, logins
person.increment :logins, logins
@event.attendees.add(person)
end
end
Expand Down Expand Up @@ -630,18 +630,18 @@ def tag
end

test "be able to increment a counter" do
@event.incr(:votes)
@event.increment(:votes)
assert 1 == @event.votes

@event.incr(:votes, 2)
@event.increment(:votes, 2)
assert 3 == @event.votes
end

test "be able to decrement a counter" do
@event.decr(:votes)
@event.decrement(:votes)
assert @event.votes == -1

@event.decr(:votes, 2)
@event.decrement(:votes, 2)
assert @event.votes == -3
end

Expand Down Expand Up @@ -739,7 +739,7 @@ class ::Make < Ohm::Model
# Persistence
test "persist attributes to a hash" do
event = Event.create(:name => "Redis Meetup")
event.incr(:votes)
event.increment(:votes)

assert "hash" == Ohm.redis.call("TYPE", "Event:1")

Expand Down

0 comments on commit 6a3e484

Please sign in to comment.