Created
October 24, 2013 20:03
-
-
Save eladmeidar/7144042 to your computer and use it in GitHub Desktop.
Redis recipes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/redis.rb | |
require 'rubygems' | |
require 'redis' | |
REDIS = Redis.new() | |
class User | |
attr_accessor :name, :id | |
def initialize(name, id) | |
@name = name | |
@id = id | |
end | |
def like_recipe(recipe) | |
REDIS.sadd "user:#{self.id}:liked_recipes", recipe.id | |
end | |
def has_in_common_with_user?(another_user) | |
REDIS.sinter("user:#{self.id}:liked_recipes", "user:#{another_user.id}:liked_recipes").size > 0 | |
end | |
def liked_recipes | |
REDIS.smembers "user:#{self.id}:liked_recipes" | |
end | |
end | |
class Recipe | |
attr_accessor :name, :id, :stuff_in_in | |
def initialize(name, id, stuff_in_it = []) | |
@name = name | |
@id = id | |
@stuff_in_it = [] | |
end | |
end | |
r1 = Recipe.new("Spaghetti", 1, ["tomato", "onion", "meat"]) | |
r2 = Recipe.new("Pizza", 2, ["tomato", "onion", "flour"]) | |
u1 = User.new("elad", 1) | |
u2 = User.new("casey", 2) | |
u1.like_recipe(r1) | |
u1.like_recipe(r2) | |
u2.like_recipe(r2) | |
puts "user #{u1.name} likes: #{u1.liked_recipes}" | |
puts "user #{u2.name} likes: #{u2.liked_recipes}" | |
puts "#{u1.name} has recipes in common with #{u2.name}: #{u1.has_in_common_with_user?(u2)}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment