Class: SDL::Receivers::TypeReceiver
- Includes:
- ActiveSupport::Inflector
- Defined in:
- lib/sdl/receivers/type_receiver.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#subclasses ⇒ Object
readonly
Returns the value of attribute subclasses.
Attributes inherited from Receiver
Instance Method Summary collapse
- #base_class ⇒ Object
-
#initialize(sym, compendium, superklass = nil) ⇒ TypeReceiver
constructor
A new instance of TypeReceiver.
-
#list(name, &block) ⇒ Object
Define a list of a type, which is defined in the block.
- #method_missing(name, *args, &block) ⇒ Object
- #register_sdltype(klass) ⇒ Object
- #subtype(sym, &definition) ⇒ Object
Methods inherited from Receiver
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
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
4 5 6 |
# File 'lib/sdl/receivers/type_receiver.rb', line 4 def klass @klass end |
#subclasses ⇒ Object (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_class ⇒ Object
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 |