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.



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/kamelopard/classes.rb', line 171

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.



158
159
160
# File 'lib/kamelopard/classes.rb', line 158

def comment
  @comment
end

#kml_idObject

Returns the value of attribute kml_id.



157
158
159
# File 'lib/kamelopard/classes.rb', line 157

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.



164
165
166
# File 'lib/kamelopard/classes.rb', line 164

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



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/kamelopard/classes.rb', line 187

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



263
264
265
266
267
268
269
270
271
272
# File 'lib/kamelopard/classes.rb', line 263

def change(field, value)
    c = XML::Node.new 'Change'
    o = XML::Node.new self.class.name.sub!(/Kamelopard::/, '')
    o.attributes['targetId'] = self.kml_id
    e = XML::Node.new field
    e.content = value.to_s
    o << e
    c << o
    c
end

#master_only?Boolean

This just makes the Ruby-ism question mark suffix work

Returns:

  • (Boolean)


239
240
241
# File 'lib/kamelopard/classes.rb', line 239

def master_only?
    return @master_only
end

#to_kml(elem) ⇒ Object

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



252
253
254
255
256
257
258
259
# File 'lib/kamelopard/classes.rb', line 252

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