Class: Rave::Models::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/models/context.rb

Overview

Contains server request information including current waves and operations.

Constant Summary collapse

JAVA_CLASS =

:nodoc:

'com.google.wave.api.impl.OperationMessageBundle'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Context

Options include:

  • :waves

  • :wavelets

  • :blips

  • :operations

  • :users



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/models/context.rb', line 42

def initialize(options = {}) # :nodoc:
  @waves = options[:waves] || {}
  @waves.values.each { |wave| wave.context = self }          #Set up self as this wave's context

  @wavelets = options[:wavelets] || {}
  @wavelets.values.each { |wavelet| wavelet.context = self } #Set up self as this wavelet's context
  @primary_wavelet = @wavelets.values[0] # As opposed to any that are created later.

  
  @blips = options[:blips] || {}
  @blips.values.each { |blip| blip.context = self }          #Set up self as this blip's context
  
  @operations = options[:operations] || []
  
  @users = options[:users] || {}
  @users.values.each { |user| user.context = self }          #Set up self as this user's context

  resolve_user_references(options[:robot])
end

Instance Attribute Details

#primary_waveletObject (readonly)

:nodoc: API users should use Event#wavelet



6
7
8
# File 'lib/models/context.rb', line 6

def primary_wavelet
  @primary_wavelet
end

#robotObject (readonly)

The robot managing this context.



7
8
9
# File 'lib/models/context.rb', line 7

def robot
  @robot
end

Instance Method Details

#add_blip(blip) ⇒ Object

Add a blip to blips (Use an Operation to actually add the blip to the Wave). Returns: The blip [Blip].



95
96
97
98
99
# File 'lib/models/context.rb', line 95

def add_blip(blip) # :nodoc:
  @blips[blip.id] = blip
  blip.context = self
  blip
end

#add_operation(options) ⇒ Object

Add an operation to the list to be executed. Returns: self [Context]



103
104
105
106
# File 'lib/models/context.rb', line 103

def add_operation(options) # :nodoc:
  @operations << Operation.new(options)
  self
end

#add_user(options) ⇒ Object

Add a user to users (Use an Operation to actually add the blip to the Wave).

Raises:



147
148
149
150
151
152
153
154
# File 'lib/models/context.rb', line 147

def add_user(options) # :nodoc:
  options[:id].downcase! if options[:id]
  raise DuplicatedIDError.new("Can't add another User with id #{options[:id]}") if @users.has_key? options[:id].downcase
  user = User.new(options)
  @users[user.id] = user
  user.context = self
  user
end

#add_wave(wave) ⇒ Object

Add a wave to waves (Use an Operation to actually add the wave). Returns: The wave [Wave].



118
119
120
121
122
# File 'lib/models/context.rb', line 118

def add_wave(wave)# :nodoc:
  @waves[wave.id] = wave
  wave.context = self
  wave
end

#add_wavelet(wavelet) ⇒ Object

Add a wavelet to wavelets (Use an Operation to actually add the blip to the Wave). Returns: The wavelet [Wavelet].



110
111
112
113
114
# File 'lib/models/context.rb', line 110

def add_wavelet(wavelet)# :nodoc:
  @wavelets[wavelet.id] = wavelet
  wavelet.context = self
  wavelet
end

#blipsObject

All wavelets by ID [Hash of String => Wavelet]



20
21
22
# File 'lib/models/context.rb', line 20

def blips # :nodoc:
  @blips.dup
end

#create_wavelet(participants) ⇒ Object

participants

Participants to exist in the new wavelet, as IDs or objects [Array of String/User]

Returns: Newly created wave [Wave]



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/models/context.rb', line 126

def create_wavelet(participants) # :nodoc:
  # Map participants to strings, since they could be Users.
  participant_ids = participants.map {|p| p.to_s.downcase }
  participant_ids << @robot.id unless participant_ids.include? @robot.id
  
  wavelet = Wavelet.new(:context => self, :participants => participant_ids)
  add_wavelet(wavelet)

  # TODO: Get wave id from sensible place?
  add_operation(:type => Operation::WAVELET_CREATE, :wave_id => @waves.keys[0],
    :property => wavelet)

  wavelet
end

#operationsObject

All operations [Array of Operation]



25
26
27
# File 'lib/models/context.rb', line 25

def operations # :nodoc:
  @operations.dup
end

:nodoc:



165
166
167
168
169
170
171
# File 'lib/models/context.rb', line 165

def print_structure(indent = 0) # :nodoc:
  str = ''
  waves.each_value do |wave|
    str << wave.print_structure(indent)
  end
  str
end

#remove_blip(blip) ⇒ Object

Remove a blip.



142
143
144
# File 'lib/models/context.rb', line 142

def remove_blip(blip) # :nodoc:
  @blips.delete(blip.id)
end

#to_jsonObject

Serialize the context for use in the line protocol.



157
158
159
160
161
162
163
# File 'lib/models/context.rb', line 157

def to_json # :nodoc:
  hash = {
    'operations' => { 'javaClass' => 'java.util.ArrayList', 'list' => @operations },
    'javaClass' => JAVA_CLASS
  }
  hash.to_json
end

#usersObject

All users by ID [Hash of String => User]



30
31
32
# File 'lib/models/context.rb', line 30

def users # :nodoc:
  @users.dup
end

#waveletsObject

All wavelets by ID [Hash of String => Wavelet]



15
16
17
# File 'lib/models/context.rb', line 15

def wavelets # :nodoc:
  @wavelets.dup
end

#wavesObject

All waves by ID [Hash of String => Wave]



10
11
12
# File 'lib/models/context.rb', line 10

def waves # :nodoc:
  @waves.dup
end