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.



193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/kamelopard/classes.rb', line 193

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.



174
175
176
# File 'lib/kamelopard/classes.rb', line 174

def comment
  @comment
end

#kml_idObject

Returns the value of attribute kml_id.



173
174
175
# File 'lib/kamelopard/classes.rb', line 173

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.



180
181
182
# File 'lib/kamelopard/classes.rb', line 180

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



184
185
186
# File 'lib/kamelopard/classes.rb', line 184

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



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/kamelopard/classes.rb', line 217

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



293
294
295
296
297
298
299
# File 'lib/kamelopard/classes.rb', line 293

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)


269
270
271
# File 'lib/kamelopard/classes.rb', line 269

def master_only?
    return @master_only
end

#to_kml(elem) ⇒ Object

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



282
283
284
285
286
287
288
289
# File 'lib/kamelopard/classes.rb', line 282

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