class SDL::Base::Type
Attributes
namespace[RW]
The namespace URL of this Type
class !@attr [r] namespace
identifier[RW]
An identifier for type instances
parent[RW]
The parent of this type.
parent_index[RW]
The index of this type instance in the parent list
Public Class Methods
[](symbol)
click to toggle source
# File lib/sdl/base/type.rb, line 84 def [](symbol) instances[symbol] end
add_property(sym, type, multi)
click to toggle source
# File lib/sdl/base/type.rb, line 237 def add_property(sym, type, multi) properties.delete_if { |p| (p.name == sym.to_s) && puts("Warning: Overwritten property definition of #{p.to_s}").nil? } property = SDL::Base::Property.new(sym, type, self, multi) (@properties ||= []) << property add_property_setters(sym, type, multi) add_property_getter(sym, type) unless multi end
add_property_getter(sym, type)
click to toggle source
# File lib/sdl/base/type.rb, line 270 def add_property_getter(sym, type) define_method sym do instance_variable_get "@#{sym.to_s}" end end
add_property_setters(sym, type, multi)
click to toggle source
# File lib/sdl/base/type.rb, line 248 def add_property_setters(sym, type, multi) unless multi attr_reader sym # Setter define_method "#{sym}=" do |value| if type < SDL::Types::SDLSimpleType instance_variable_set "@#{sym}".to_s, type.new(value) else instance_variable_set "@#{sym}".to_s, value value.parent = self end end else # Define accessor method for lists define_method sym do eval "@#{sym} ||= []" end end end
class_definition_string(sym, superklass)
click to toggle source
# File lib/sdl/base/type.rb, line 163 def class_definition_string(sym, superklass) "class SDL::Base::Type::#{sym.to_s.camelize} < #{superklass.name} unless @registered include SDL::Types::SDLType wraps self codes local_name.underscore.to_sym superclass.subtypes << self @registered = true end end" end
clear_instances!()
click to toggle source
# File lib/sdl/base/type.rb, line 92 def clear_instances! @instances = {} end
clear_properties!()
click to toggle source
# File lib/sdl/base/type.rb, line 112 def clear_properties! @properties = [] end
define_type(sym, superklass = SDL::Base::Type)
click to toggle source
# File lib/sdl/base/type.rb, line 184 def define_type(sym, superklass = SDL::Base::Type) eval class_definition_string(sym, superklass) SDL::Base::Type.const_get(sym.to_s.camelize) end
documentation_key()
click to toggle source
# File lib/sdl/util/documentation.rb, line 72 def self.documentation_key "sdl.#{SDL::Util::Documentation.walk_the_class_name(self)}" end
find_subtype_recursive(symbol)
click to toggle source
# File lib/sdl/base/type.rb, line 152 def find_subtype_recursive(symbol) subtypes_recursive.find do |subtype| subtype.local_name.underscore.eql? symbol.to_s end end
instances()
click to toggle source
# File lib/sdl/base/type.rb, line 88 def instances @instances ||= {} end
instances_recursive()
click to toggle source
# File lib/sdl/base/type.rb, line 148 def instances_recursive RecursiveInstances.new(self) end
is_sub?()
click to toggle source
# File lib/sdl/base/type.rb, line 140 def is_sub? superclass != SDL::Base::Type end
list(sym, &block)
click to toggle source
Define a list of a type, which is defined in the block.
# File lib/sdl/base/type.rb, line 200 def list(sym, &block) list_type = SDL::Base::Type.subtype(sym.to_s.singularize.to_sym, &block) # Designate as list type list_type.list_item = true add_property sym, list_type, true end
list_item?()
click to toggle source
# File lib/sdl/base/type.rb, line 136 def list_item? @list_item == true end
local_name()
click to toggle source
# File lib/sdl/base/type.rb, line 69 def local_name @local_name || original_name.demodulize end
local_name=(name)
click to toggle source
# File lib/sdl/base/type.rb, line 73 def local_name=(name) @local_name = name end
method_missing(name, *args, &block)
click to toggle source
# File lib/sdl/base/type.rb, line 209 def method_missing(name, *args, &block) sym = args[0] || name.to_sym if name =~ /list_of_/ multi = true type = SDL::Types::SDLType.codes[name.to_s.gsub('list_of_', '').singularize.to_sym] sym = sym.to_s.gsub('list_of_', '').to_sym else multi = false type = SDL::Types::SDLType.codes[name.to_sym] if !type type = SDL::Base::Type.subtype(sym) if block_given? type.instance_eval &block else raise "No block given for type definition of #{sym}" end end end add_property sym, type, multi end
multi_property?(including_super = true)
click to toggle source
# File lib/sdl/base/type.rb, line 132 def multi_property?(including_super = true) properties(including_super).count > 1 end
name()
click to toggle source
# File lib/sdl/base/type.rb, line 65 def name original_name || local_name end
Also aliased as: original_name
properties(including_super = false)
click to toggle source
# File lib/sdl/base/type.rb, line 100 def properties(including_super = false) if including_super && is_sub? retrieved_properties = self.properties + superclass.properties(true) else retrieved_properties = @properties ||= [] end retrieved_properties.each do |p| p.holder = self end end
properties_hash(including_super = false)
click to toggle source
# File lib/sdl/base/type.rb, line 116 def properties_hash(including_super = false) Hash[properties(including_super).collect do |p| [p.name.to_sym, p] end] end
propertyless?(including_super = true)
click to toggle source
# File lib/sdl/base/type.rb, line 120 def propertyless?(including_super = true) properties(including_super).count == 0 end
registered?()
click to toggle source
@return [Boolean] Has this Type
already been registered with the compendium?
# File lib/sdl/base/type.rb, line 159 def registered? @registered ||= false end
respond_to_missing?(symbol, include_all = false)
click to toggle source
# File lib/sdl/base/type.rb, line 233 def respond_to_missing?(symbol, include_all = false) false end
single_property(including_super = true)
click to toggle source
# File lib/sdl/base/type.rb, line 128 def single_property(including_super = true) properties(including_super).first end
single_property?(including_super = true)
click to toggle source
# File lib/sdl/base/type.rb, line 124 def single_property?(including_super = true) properties(including_super).count == 1 end
subtype(sym, &definition)
click to toggle source
# File lib/sdl/base/type.rb, line 190 def subtype(sym, &definition) type = define_type(sym, self) type.instance_eval(&definition) if block_given? return type end
subtypes()
click to toggle source
A list of all subtypes !@attr [r] subtypes @return [<Class>] The subtypes
# File lib/sdl/base/type.rb, line 80 def subtypes @subtypes ||= [] end
subtypes_recursive()
click to toggle source
# File lib/sdl/base/type.rb, line 144 def subtypes_recursive RecursiveSubtypes.new(self) end
to_s()
click to toggle source
# File lib/sdl/base/type.rb, line 96 def to_s @local_name || name end
unregister()
click to toggle source
# File lib/sdl/base/type.rb, line 178 def unregister superclass.subtypes.delete self SDL::Base::Type.send(:remove_const, local_name.to_sym) end
xsd_element_name()
click to toggle source
# File lib/sdl/exporters/xml_mapping.rb, line 15 def xsd_element_name local_name.camelize(:lower) end
xsd_type_identifier_name()
click to toggle source
# File lib/sdl/exporters/xml_mapping.rb, line 19 def xsd_type_identifier_name "#{local_name}Identifier" end
xsd_type_name()
click to toggle source
# File lib/sdl/exporters/xml_mapping.rb, line 23 def xsd_type_name local_name end
Public Instance Methods
==(other)
click to toggle source
Two type instances are the same if they either have the same identifier, or are the same object
# File lib/sdl/base/type.rb, line 335 def ==(other) if(identifier && other.respond_to?(:identifier)) identifier == other.identifier else equal? other end end
annotated?()
click to toggle source
# File lib/sdl/base/type.rb, line 325 def annotated? ! @annotations.blank? end
annotations()
click to toggle source
# File lib/sdl/base/type.rb, line 329 def annotations @annotations ||= [] end
documentation_key()
click to toggle source
# File lib/sdl/util/documentation.rb, line 76 def documentation_key "sdl.instance.#{SDL::Util::Documentation.walk_the_class_name(self.class)}.#{identifier}" end
get_sdl_value(property)
click to toggle source
# File lib/sdl/base/type.rb, line 281 def get_sdl_value(property) send property.name end
property_values(include_empty = false)
click to toggle source
Gets the values of all properties
# File lib/sdl/base/type.rb, line 304 def property_values(include_empty = false) pv = Hash[self.class.properties(true).map{|p| [p, send(p.name)]}] unless include_empty pv.reject! {|p, v| v.blank? } end pv end
rdf_object()
click to toggle source
# File lib/sdl/exporters/rdf_mapping.rb, line 12 def rdf_object RDF::URI.new(uri) end
set_sdl_property(property, value)
click to toggle source
# File lib/sdl/base/type.rb, line 277 def set_sdl_property(property, value) send "#{property.name}=", value end
set_sdl_values(*property_values)
click to toggle source
# File lib/sdl/base/type.rb, line 285 def set_sdl_values(*property_values) property_values.zip(self.class.properties(true)).each do |value, property| if(value.is_a?(Hash)) # Setting values can be carried out by specifying the name of the property as a hash # e.g. a: 1, b: 2 SDL::Receivers::TypeInstanceReceiver.new(self).send(value.first[0].to_s, value.first[1]) else # Else, setting values is carried out by a value list, e.g. 1, 2 unless property raise "Specified value '#{value}' for non-existing property." end SDL::Receivers::TypeInstanceReceiver.new(self).send(property.name, value) end end end
to_s()
click to toggle source
# File lib/sdl/base/type.rb, line 314 def to_s # If there is a property with the same name, than the type, return its to_s, return the name of the class naming_property = self.class.properties(true).find {|p| p.name.eql?(self.class.to_s.underscore) } if(naming_property) instance_variable_get("@#{naming_property.name.to_sym}").to_s else self.class.to_s end end