Class: SDL::Receivers::TypeReceiver

Inherits:
Receiver
  • Object
show all
Includes:
ActiveSupport::Inflector
Defined in:
lib/sdl/receivers/type_receiver.rb

Direct Known Subclasses

FactReceiver

Instance Attribute Summary collapse

Attributes inherited from Receiver

#compendium

Instance Method Summary collapse

Methods inherited from Receiver

#set_value

Constructor Details

#initialize(sym, compendium, superklass = nil) ⇒ TypeReceiver

Returns a new instance of TypeReceiver.



7
8
9
10
11
12
13
14
15
16
# File 'lib/sdl/receivers/type_receiver.rb', line 7

def initialize(sym, compendium, superklass = nil)
  super(compendium)

  @klass = Class.new(superklass || base_class)
  @klass.local_name = sym.to_s.camelize

  @subclasses = [@klass]

  register_sdltype(@klass)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sdl/receivers/type_receiver.rb', line 49

def method_missing(name, *args, &block)
  sym = args[0] || name.to_sym

  if name =~ /list_of_/
    multi = true
    type = @compendium.sdltype_codes[name.to_s.gsub('list_of_', '').singularize.to_sym]
  else
    multi = false
    type = @compendium.sdltype_codes[name.to_sym]
  end

  if type
    add_property sym, type, multi
  else
    super(name, *args, &block)
  end
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



4
5
6
# File 'lib/sdl/receivers/type_receiver.rb', line 4

def klass
  @klass
end

#subclassesObject (readonly)

Returns the value of attribute subclasses.



5
6
7
# File 'lib/sdl/receivers/type_receiver.rb', line 5

def subclasses
  @subclasses
end

Instance Method Details

#base_classObject



18
19
20
# File 'lib/sdl/receivers/type_receiver.rb', line 18

def base_class
  SDL::Base::Type
end

#list(name, &block) ⇒ Object

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



40
41
42
43
44
45
46
47
# File 'lib/sdl/receivers/type_receiver.rb', line 40

def list(name, &block)
  list_type = @compendium.type name.to_s.singularize.to_sym, &block

  # Designate as list type
  list_type.list_item = true

  add_property name.to_sym, list_type, true
end

#register_sdltype(klass) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/sdl/receivers/type_receiver.rb', line 22

def register_sdltype(klass)
  klass.class_eval do
    include SDL::Types::SDLType

    wraps self
    codes local_name.underscore.to_sym
  end
end

#subtype(sym, &definition) ⇒ Object



31
32
33
34
35
36
# File 'lib/sdl/receivers/type_receiver.rb', line 31

def subtype(sym, &definition)
  receiver = self.class.new(sym, @compendium, @klass)
  receiver.instance_eval(&definition) if block_given?

  @subclasses.concat(receiver.subclasses)
end