Class: FakeGoUp::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/fake_go_up/item.rb

Constant Summary collapse

REDIS_KEY =
"fake_go_up:items"
@@instance_collector =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = 1) ⇒ Item

Returns a new instance of Item.



7
8
9
10
11
# File 'lib/fake_go_up/item.rb', line 7

def initialize(id = 1)
  @id = id
  @count = 0
  @@instance_collector[id] = self
end

Instance Attribute Details

#countObject

Returns the value of attribute count.



3
4
5
# File 'lib/fake_go_up/item.rb', line 3

def count
  @count
end

Class Method Details

.create(id) ⇒ Object



34
35
36
37
38
39
# File 'lib/fake_go_up/item.rb', line 34

def self.create(id)
  id = id.to_i

  FakeGoUp.redis.hset(FakeGoUp::Item::REDIS_KEY, id, 0)
  new(id)
end

.find_by_id(id) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fake_go_up/item.rb', line 22

def self.find_by_id(id)
  id = id.to_i

  instance = self.instances[id]
  return instance if instance

  count = FakeGoUp.redis.hget(FakeGoUp::Item::REDIS_KEY, id).to_i
  instance = self.new(id)
  instance.count = count
  instance
end

.instancesObject



41
42
43
# File 'lib/fake_go_up/item.rb', line 41

def self.instances
  @@instance_collector
end

Instance Method Details

#idObject



13
14
15
# File 'lib/fake_go_up/item.rb', line 13

def id
  @id
end