Module: SPQR::ManageableClassMixins

Defined in:
lib/spqr/manageable.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.delete(instance) ⇒ Object



207
208
209
# File 'lib/spqr/manageable.rb', line 207

def self.delete(instance)
  instances.delete(instance.qmf_oid)
end

.find_allObject



169
170
171
# File 'lib/spqr/manageable.rb', line 169

def self.find_all
  instances
end

.find_by_id(id) ⇒ Object



173
174
175
# File 'lib/spqr/manageable.rb', line 173

def self.find_by_id(id)
  instances[0]
end

.instancesObject



165
166
167
# File 'lib/spqr/manageable.rb', line 165

def self.instances
  @instances ||= [self.new]
end

.new(*args) ⇒ Object

XXX: would it make more sense to call allocate and initialize explicitly?



201
202
203
204
205
# File 'lib/spqr/manageable.rb', line 201

def self.new(*args)
  result = old_new(*args)
  instances[result.qmf_oid] = result
  result
end

Instance Method Details

#appObject



153
154
155
# File 'lib/spqr/manageable.rb', line 153

def app
  @spqr_app
end

#app=(app) ⇒ Object



149
150
151
# File 'lib/spqr/manageable.rb', line 149

def app=(app)
  @spqr_app = app
end

#expose(name, description = nil, options = nil, &blk) ⇒ Object

Exposes a method to QMF



158
159
160
# File 'lib/spqr/manageable.rb', line 158

def expose(name, description=nil, options=nil, &blk)
  spqr_meta.declare_method(name, description, options, blk)
end

#is_singletonObject

Declares that this class is a singleton class; that is, only one instance will be published over QMF



164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/spqr/manageable.rb', line 164

def is_singleton
  def self.instances
    @instances ||= [self.new]
  end

  def self.find_all
    instances
  end

  def self.find_by_id(id)
    instances[0]
  end
end

#is_trackedObject

Declares that instances of this class will automatically be tracked (and find_all and find_by_id methods generated). Instances of automatically-tracked classes must be explicitly deleted (with the delete class method). Do not use automatic tracking with Rhubarb, which automatically tracks instances for you.



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/spqr/manageable.rb', line 184

def is_tracked
  # no pun intended, I promise
  alias_method :old_new, :new
  
  def self.instances
    @instances ||= {}
  end

  def self.find_all
    instances.values
  end

  def self.find_by_id(id)
    instances[id]
  end
  
  # XXX:  would it make more sense to call allocate and initialize explicitly?
  def self.new(*args)
    result = old_new(*args)
    instances[result.qmf_oid] = result
    result
  end

  def self.delete(instance)
    instances.delete(instance.qmf_oid)
  end
end

#logObject



145
146
147
# File 'lib/spqr/manageable.rb', line 145

def log
  @spqr_log || ::SPQR::Sink.new
end

#log=(logger) ⇒ Object



141
142
143
# File 'lib/spqr/manageable.rb', line 141

def log=(logger)
  @spqr_log = logger
end

#qmf_class_name(nm) ⇒ Object



216
217
218
# File 'lib/spqr/manageable.rb', line 216

def qmf_class_name(nm)
  spqr_meta.classname = nm
end

#qmf_description(d) ⇒ Object



220
221
222
# File 'lib/spqr/manageable.rb', line 220

def qmf_description(d)
  spqr_meta.description = d
end

#qmf_options(opts) ⇒ Object



224
225
226
# File 'lib/spqr/manageable.rb', line 224

def qmf_options(opts)
  spqr_meta.options = opts.dup
end

#qmf_package_name(nm) ⇒ Object



212
213
214
# File 'lib/spqr/manageable.rb', line 212

def qmf_package_name(nm)
  spqr_meta.package = nm
end

#qmf_property(name, kind, options = nil) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/spqr/manageable.rb', line 244

def qmf_property(name, kind, options=nil)
  spqr_meta.declare_property(name, kind, options)
  
  # add a property accessor to instances of other
  self.class_eval do
    # XXX: should cons up a "safe_attr_accessor" method that works like this:
    attr_reader name.to_sym unless method_defined? "#{name}"
    attr_writer name.to_sym unless method_defined? "#{name}="
  end
  
  if options and options[:index]
    # if this is an index property, add a find-by method if one
    # does not already exist
    spqr_define_index_find(name)
  end
end

#qmf_statistic(name, kind, options = nil) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/spqr/manageable.rb', line 228

def qmf_statistic(name, kind, options=nil)
  spqr_meta.declare_statistic(name, kind, options)
  
  self.class_eval do
    # XXX: are we only interested in declaring a reader for
    # statistics?  Doesn't it really makes more sense for the managed
    # class to declare a method with the same name as the
    # statistic so we aren't declaring anything at all here?
    
    # XXX: should cons up a "safe_attr_reader" method that works
    # like this:
    attr_reader name.to_sym unless method_defined? "#{name}"
    attr_writer name.to_sym unless method_defined? "#{name}="
  end
end

#spqr_metaObject



137
138
139
# File 'lib/spqr/manageable.rb', line 137

def spqr_meta
  @spqr_meta ||= ::SPQR::ManageableMeta.new
end