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

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.



188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/kamelopard/classes.rb', line 188

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.



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

def comment
  @comment
end

#kml_idObject

Returns the value of attribute kml_id.



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

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.



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

def master_only
  @master_only
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



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/kamelopard/classes.rb', line 212

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



288
289
290
291
292
293
294
# File 'lib/kamelopard/classes.rb', line 288

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)


264
265
266
# File 'lib/kamelopard/classes.rb', line 264

def master_only?
    return @master_only
end

#to_kml(elem) ⇒ Object

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



277
278
279
280
281
282
283
284
# File 'lib/kamelopard/classes.rb', line 277

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