Class: SDL::Base::Type

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Autoload, URIMappedResource
Includes:
URIMappedResource
Defined in:
lib/sdl/base/type.rb,
lib/sdl/util/documentation.rb,
lib/sdl/exporters/rdf_mapping.rb,
lib/sdl/exporters/xml_mapping.rb

Direct Known Subclasses

Service

Defined Under Namespace

Classes: RecursiveInstances, RecursiveSubtypes, Service

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from URIMappedResource

uri

Class Attribute Details

.list_itemObject

If the Type is a list item type !@attr [r] list_item



55
56
57
# File 'lib/sdl/base/type.rb', line 55

def list_item
  @list_item
end

.namespaceObject

The namespace URL of this Type class !@attr [r] namespace



51
52
53
# File 'lib/sdl/base/type.rb', line 51

def namespace
  @namespace
end

Instance Attribute Details

#identifierObject

An identifier for type instances



344
345
346
# File 'lib/sdl/base/type.rb', line 344

def identifier
  @identifier
end

#parentObject

The parent of this type.



350
351
352
# File 'lib/sdl/base/type.rb', line 350

def parent
  @parent
end

#parent_indexObject

The index of this type instance in the parent list



347
348
349
# File 'lib/sdl/base/type.rb', line 347

def parent_index
  @parent_index
end

Class Method Details

.[](symbol) ⇒ Object



84
85
86
# File 'lib/sdl/base/type.rb', line 84

def [](symbol)
  instances[symbol]
end

.add_property(sym, type, multi) ⇒ Object



237
238
239
240
241
242
243
244
245
246
# 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) ⇒ Object



270
271
272
273
274
# 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) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# 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) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
# 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!Object



92
93
94
# File 'lib/sdl/base/type.rb', line 92

def clear_instances!
  @instances = {}
end

.clear_properties!Object



112
113
114
# File 'lib/sdl/base/type.rb', line 112

def clear_properties!
  @properties = []
end

.define_type(sym, superklass = SDL::Base::Type) ⇒ Object



184
185
186
187
188
# 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_keyObject



72
73
74
# 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) ⇒ Object



152
153
154
155
156
# 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

.instancesObject



88
89
90
# File 'lib/sdl/base/type.rb', line 88

def instances
  @instances ||= {}
end

.instances_recursiveObject



148
149
150
# File 'lib/sdl/base/type.rb', line 148

def instances_recursive
  RecursiveInstances.new(self)
end

.is_sub?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/sdl/base/type.rb', line 140

def is_sub?
  superclass != SDL::Base::Type
end

.list(sym, &block) ⇒ Object

Define a list of a type, which is defined in the block.



200
201
202
203
204
205
206
207
# 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?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/sdl/base/type.rb', line 136

def list_item?
  @list_item == true
end

.local_nameObject



69
70
71
# File 'lib/sdl/base/type.rb', line 69

def local_name
  @local_name || original_name.demodulize
end

.local_name=(name) ⇒ Object



73
74
75
# File 'lib/sdl/base/type.rb', line 73

def local_name=(name)
  @local_name = name
end

.method_missing(name, *args, &block) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# 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) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/sdl/base/type.rb', line 132

def multi_property?(including_super = true)
  properties(including_super).count > 1
end

.nameObject



65
66
67
# File 'lib/sdl/base/type.rb', line 65

def name
  original_name || local_name
end

.original_nameObject



63
# File 'lib/sdl/base/type.rb', line 63

alias :original_name :name

.properties(including_super = false) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
# 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) ⇒ Object



116
117
118
# 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) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/sdl/base/type.rb', line 120

def propertyless?(including_super = true)
  properties(including_super).count == 0
end

.registered?Boolean

Returns Has this Type already been registered with the compendium?.

Returns:

  • (Boolean)

    Has this Type already been registered with the compendium?



159
160
161
# File 'lib/sdl/base/type.rb', line 159

def registered?
  @registered ||= false
end

.respond_to_missing?(symbol, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


233
234
235
# File 'lib/sdl/base/type.rb', line 233

def respond_to_missing?(symbol, include_all = false)
  false
end

.single_property(including_super = true) ⇒ Object



128
129
130
# File 'lib/sdl/base/type.rb', line 128

def single_property(including_super = true)
  properties(including_super).first
end

.single_property?(including_super = true) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/sdl/base/type.rb', line 124

def single_property?(including_super = true)
  properties(including_super).count == 1
end

.subtype(sym, &definition) ⇒ Object



190
191
192
193
194
195
196
# 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<Class>

A list of all subtypes !@attr [r] subtypes

Returns:

  • (<Class>)

    The subtypes



80
81
82
# File 'lib/sdl/base/type.rb', line 80

def subtypes
  @subtypes ||= []
end

.subtypes_recursiveObject



144
145
146
# File 'lib/sdl/base/type.rb', line 144

def subtypes_recursive
  RecursiveSubtypes.new(self)
end

.to_sObject



96
97
98
# File 'lib/sdl/base/type.rb', line 96

def to_s
  @local_name || name
end

.unregisterObject



178
179
180
181
182
# 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_nameObject



15
16
17
# File 'lib/sdl/exporters/xml_mapping.rb', line 15

def xsd_element_name
  local_name.camelize(:lower)
end

.xsd_type_identifier_nameObject



19
20
21
# File 'lib/sdl/exporters/xml_mapping.rb', line 19

def xsd_type_identifier_name
  "#{local_name}Identifier"
end

.xsd_type_nameObject



23
24
25
# File 'lib/sdl/exporters/xml_mapping.rb', line 23

def xsd_type_name
  local_name
end

Instance Method Details

#==(other) ⇒ Object

Two type instances are the same if they either have the same identifier, or are the same object



335
336
337
338
339
340
341
# 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?Boolean

Returns:

  • (Boolean)


325
326
327
# File 'lib/sdl/base/type.rb', line 325

def annotated?
  ! @annotations.blank?
end

#annotationsObject



329
330
331
# File 'lib/sdl/base/type.rb', line 329

def annotations
  @annotations ||= []
end

#documentation_keyObject



76
77
78
# 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) ⇒ Object



281
282
283
# File 'lib/sdl/base/type.rb', line 281

def get_sdl_value(property)
  send property.name
end

#property_values(include_empty = false) ⇒ Object

Gets the values of all properties



304
305
306
307
308
309
310
311
312
# 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_objectObject



12
13
14
# File 'lib/sdl/exporters/rdf_mapping.rb', line 12

def rdf_object
  RDF::URI.new(uri)
end

#set_sdl_property(property, value) ⇒ Object



277
278
279
# File 'lib/sdl/base/type.rb', line 277

def set_sdl_property(property, value)
  send "#{property.name}=", value
end

#set_sdl_values(*property_values) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# 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_sObject



314
315
316
317
318
319
320
321
322
323
# 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