Skip to content

Commit

Permalink
Delete a Confg Key by assigning nil
Browse files Browse the repository at this point in the history
  • Loading branch information
bnorton committed Apr 4, 2012
1 parent 7a806f3 commit 3056625
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ User.async_fetch_content(user.id)

... # done fetching content
Howler::Config[user_id] = {:fetched_at => Time.now, :status => 'success'}.to_json

# Then to delete a key simply assign nil
Howler::Config[user_id] = nil
end
```

Expand Down
12 changes: 11 additions & 1 deletion lib/howler/support/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ def self.[](key)
end

def self.[]=(key, value)
if value.nil?
delete(key)
return
end

Howler.redis.with {|redis| redis.hset("howler:config", key.to_s, value) }
value
end

def self.flush
Expand All @@ -19,5 +23,11 @@ def self.flush
end
end
end

private

def self.delete(key)
Howler.redis.with {|redis| redis.hdel("howler:config", key.to_s) }
end
end
end
10 changes: 10 additions & 0 deletions spec/models/support/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@
it "should configure options" do
Howler.redis.with {|redis| redis.hget("howler:config", "message") }.should == '{"key": 3}'
end

describe "when the value is nil" do
it "should remove the key" do
Howler.redis.with {|redis| redis.hexists("howler:config", "message") }.should == true

Howler::Config[:message] = nil

Howler.redis.with {|redis| redis.hexists("howler:config", "message") }.should == false
end
end
end

describe ".flush" do
Expand Down

0 comments on commit 3056625

Please sign in to comment.