Class: NabaztagHackKit::Bunny

Inherits:
Object
  • Object
show all
Defined in:
lib/nabaztag_hack_kit/bunny.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Bunny

Returns a new instance of Bunny.



29
30
31
32
33
34
# File 'lib/nabaztag_hack_kit/bunny.rb', line 29

def initialize(id)
  @id              = id
  @queued_commands = []

  Bunny.add(self)
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/nabaztag_hack_kit/bunny.rb', line 9

def id
  @id
end

#last_seenObject (readonly)

Returns the value of attribute last_seen.



9
10
11
# File 'lib/nabaztag_hack_kit/bunny.rb', line 9

def last_seen
  @last_seen
end

#queued_commandsObject (readonly)

Returns the value of attribute queued_commands.



9
10
11
# File 'lib/nabaztag_hack_kit/bunny.rb', line 9

def queued_commands
  @queued_commands
end

Class Method Details

.add(bunny) ⇒ Object



24
25
26
# File 'lib/nabaztag_hack_kit/bunny.rb', line 24

def add(bunny)
  @bunnies[bunny.id] = bunny
end

.allObject



12
13
14
# File 'lib/nabaztag_hack_kit/bunny.rb', line 12

def all
  @bunnies.values
end

.find(id) ⇒ Object



16
17
18
# File 'lib/nabaztag_hack_kit/bunny.rb', line 16

def find(id)
  @bunnies[id]
end

.find_or_initialize_by_id(id) ⇒ Object



20
21
22
# File 'lib/nabaztag_hack_kit/bunny.rb', line 20

def find_or_initialize_by_id(id)
  find(id) || Bunny.new(id)
end

Instance Method Details

#next_message!Object



44
45
46
# File 'lib/nabaztag_hack_kit/bunny.rb', line 44

def next_message!
  Message.build(*@queued_commands.shift || Message::Api::OK)
end

#queue_commands(commands) ⇒ Object



40
41
42
# File 'lib/nabaztag_hack_kit/bunny.rb', line 40

def queue_commands(commands)
  @queued_commands << commands
end

#seen!Object



36
37
38
# File 'lib/nabaztag_hack_kit/bunny.rb', line 36

def seen!
  @last_seen = Time.now
end

#to_json(_state = nil, _deepth = nil) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/nabaztag_hack_kit/bunny.rb', line 48

def to_json(_state = nil, _deepth = nil)
  {
    id: id,
    last_seen: last_seen,
    queued_commands_size: queued_commands.size
  }.to_json
end