Class: UIC::MetaData

Inherits:
Object
  • Object
show all
Defined in:
lib/ruic/assets.rb

Defined Under Namespace

Classes: AssetBase

Constant Summary collapse

HIER =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ MetaData

Returns a new instance of MetaData.



273
274
275
276
277
278
279
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
# File 'lib/ruic/assets.rb', line 273

def initialize(xml)
  @by_name = {'AssetBase'=>AssetBase}

  doc = Nokogiri.XML(xml)
  hack_in_slide_names!(doc)

  HIER.each do |class_name,parent_class_name|
    parent_class = @by_name[parent_class_name]
    el = doc.root.at(class_name)
    @by_name[class_name] = create_class(el,parent_class,el.name)
  end

  # Extend well-known classes with script interfaces after they are created

  @by_name['State'] = @by_name['Slide']
  @by_name['Slide'].instance_eval do
    attr_accessor :index, :name
    define_method :inspect do
      "<slide ##{index} of #{@el['component'] || @el.parent['component']}>"
    end
    define_method(:slide?){ true }
  end

  refmat = @by_name['ReferencedMaterial']
  @by_name['MaterialBase'].instance_eval do
    define_method :replace_with_referenced_material do
      type=='ReferencedMaterial' ? self : presentation.replace_asset( self, 'ReferencedMaterial', name:name )
    end
  end

  @by_name['Path'].instance_eval do
    define_method(:anchors){ find _type:'PathAnchorPoint' }
  end

end

Instance Attribute Details

#by_nameObject (readonly)

Returns the value of attribute by_name.



264
265
266
# File 'lib/ruic/assets.rb', line 264

def by_name
  @by_name
end

Instance Method Details

#create_class(el, parent_class, name, new_defaults = {}) ⇒ Object

Creates a class from MetaData.xml with accessors for the listed. Instances of the class are associated with a presentation and know how to get/set values in that XML based on value types, slides, defaults. Also used to create classes from effects, materials, and behavior preambles.

Parameters:

  • el (Nokogiri::XML::Element)

    the element in MetaData.xml representing this class.

  • parent_class (Class)

    the asset class to inherit from.

  • name (String)

    the name of this class.

  • new_defaults (Hash) (defaults to: {})

    hash mapping attribute name to a custom default value (as string) for this class.



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/ruic/assets.rb', line 316

def create_class(el,parent_class,name,new_defaults={})
  Class.new(parent_class) do
    @name = name.to_s
    @properties = Hash[ el.css("Property").map do |e|
      type = e['type'] || (e['list'] ? 'String' : 'Float')
      type = "Float" if type=="float"
      property = begin
        UIC::Property.const_get(type).new(e)
      rescue NameError
        warn "WARNING: Unsupported property type '#{type}' on\n#{e}\nTreating this as a String."
        UIC::Property::String.new(e)
      end
      new_defaults.delete(property.name)
      [ property.name, property ]
    end ]

    new_defaults.each do |name,value|
      if prop=properties[name] # look in ancestor classes

        @properties[name] = prop.dup
        @properties[name].default = value
      end
    end

    def self.inspect
      @name
    end
  end
end

#hack_in_slide_names!(doc) ⇒ Object



350
351
352
# File 'lib/ruic/assets.rb', line 350

def hack_in_slide_names!(doc)
  doc.at('Slide') << '<Property name="name" formalName="Name" type="String" default="Slide" hidden="True" />'
end

#new_instance(presentation, el) ⇒ Object



345
346
347
348
# File 'lib/ruic/assets.rb', line 345

def new_instance(presentation,el)
  klass = @by_name[el.name] || create_class(el,@by_name['Asset'],el.name)
  klass.new(presentation,el)
end