Class: Kamelopard::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/kamelopard/classes.rb

Overview

Base class for all Kamelopard objects. Manages object ID and a single comment string associated with the object. Object IDs are stored in the kml_id attribute, and are prefixed with the value last passed to Kamelopard.id_prefix=, if anything. Note that assigning this prefix will not change the IDs of Kamelopard objects that are already initialized… just ones initialized thereafter.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Object

This constructor looks for values in the options hash that match class attributes, and sets those attributes to the values in the hash. So a class with an attribute called :when can be set via the constructor by including “:when => some-value” in the options argument to the constructor.


256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/kamelopard/classes.rb', line 256

def initialize(options = {})
    @kml_id = "#{Kamelopard.id_prefix}#{self.class.name.gsub('Kamelopard::', '')}_#{ Kamelopard.get_next_id }"
    @master_only = false

    options.each do |k, v|
        method = "#{k}=".to_sym
        if self.respond_to? method then
            self.method(method).call(v)
        else
            raise "Warning: couldn't find attribute for options hash key #{k}"
        end
    end
end

Instance Attribute Details

#commentObject

Returns the value of attribute comment


237
238
239
# File 'lib/kamelopard/classes.rb', line 237

def comment
  @comment
end

#kml_idObject

Returns the value of attribute kml_id


236
237
238
# File 'lib/kamelopard/classes.rb', line 236

def kml_id
  @kml_id
end

#master_onlyObject

The master_only attribute determines whether this Object should be included in slave mode KML files, or not. It defaults to false, indicating the Object should be included in KML files of all types. Set it to true to ensure it shows up only in slave mode.


243
244
245
# File 'lib/kamelopard/classes.rb', line 243

def master_only
  @master_only
end

Class Method Details

.parse(x) ⇒ Object

Abstract function, designed to take an XML node containing a KML object of this type, and parse it into a Kamelopard object


247
248
249
# File 'lib/kamelopard/classes.rb', line 247

def self.parse(x)
    raise "Cannot parse a #{self.class.name}"
end

Instance Method Details

#_alternate_to_kml(*a) ⇒ Object

If this is a master-only object, this function gets called internally in place of the object's original to_kml method


280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/kamelopard/classes.rb', line 280

def _alternate_to_kml(*a)
    if @master_only and ! DocumentHolder.instance.current_document.master_mode
        Kamelopard.log(:info, 'master/slave', "Because this object is master_only, and we're in slave mode, we're not including object #{self.inspect}")
        return ''
    end

    # XXX There must be a better way to do this, but I don't know what
    # it is. Running "@original_to_kml_method.call(a)" when the
    # original method expects multiple arguments interprets the
    # argument as an array, not as a list of arguments. This of course
    # makes sense, but I don't know how to get around it.
    case @original_to_kml_method.parameters.size
    when 0
        return @original_to_kml_method.call
    when 1
        # XXX This bothers me, and I'm unconvinced the calls to
        # functions with more than one parameter actually work. Why
        # should I have to pass a[0][0] here and just a[0], a[1], etc.
        # for larger numbers of parameters, if this were all correct?
        return @original_to_kml_method.call(a[0][0])
    when 2
        return @original_to_kml_method.call(a[0], a[1])
    when 3
        return @original_to_kml_method.call(a[0], a[1], a[2])
    else
        raise "Unsupported number of arguments (#{@original_to_kml_method.arity}) in to_kml function #{@original_to_kml_method}. This is a bug"
    end
end

#change(field, value) ⇒ Object

Generates a <Change> element suitable for changing the given field of an object to the given value


356
357
358
359
360
361
362
# File 'lib/kamelopard/classes.rb', line 356

def change(attributes, values)
    change = XML::Node.new 'Change'
    child = XML::Node.new self.class.name
    child.attributes[:targetId] = @kml_id
    change << child
    return change
end

#master_only?Boolean

This just makes the Ruby-ism question mark suffix work

Returns:

  • (Boolean)

332
333
334
# File 'lib/kamelopard/classes.rb', line 332

def master_only?
    return @master_only
end

#to_kml(elem) ⇒ Object

Returns XML::Node containing this object's KML. Objects should override this method


345
346
347
348
349
350
351
352
# File 'lib/kamelopard/classes.rb', line 345

def to_kml(elem)
    elem.attributes['id'] = @kml_id.to_s
    if not @comment.nil? and @comment != '' then
        c = XML::Node.new_comment " #{@comment} "
        elem << c
        return c
    end
end