Class: SDL::Receivers::ServiceReceiver

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

Instance Attribute Summary collapse

Attributes inherited from Receiver

#compendium

Instance Method Summary collapse

Methods inherited from Receiver

#set_value

Constructor Details

#initialize(sym, compendium) ⇒ ServiceReceiver

Returns a new instance of ServiceReceiver.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/sdl/receivers/service_receiver.rb', line 8

def initialize(sym, compendium)
  super(compendium)

  @service = SDL::Base::Service.new(sym.to_s)

  compendium.fact_classes.each do |fact_class|
    define_singleton_method("is_#{fact_class.local_name.underscore.verb.conjugate(:tense => :past, :person => :third, :plurality => :singular, :aspect => :perfective)}") do |*args, &block|
      add_fact fact_class, *args, &block
    end

    define_singleton_method("has_#{fact_class.local_name.underscore}") do |*args, &block|
      add_fact fact_class, *args, &block
    end

    define_singleton_method("#{fact_class.local_name.underscore}") do |*args, &block|
      add_fact fact_class, *args, &block
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object (private)

Catches calls to methods named similarily to possible predefined type instances



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sdl/receivers/service_receiver.rb', line 46

def method_missing(name, *args)
  possible_type_instances = @compendium.type_instances.map{|k, v| v[name]}.select{|v| v != nil}

  unless possible_type_instances.nil? || possible_type_instances.empty?
    if possible_type_instances.length > 1
      raise Exception.new("Multiple possibilities for #{name} in #{caller[0]}")
    else
      possible_type_instances[0]
    end
  else
    raise Exception.new("I do not know what to do with '#{name}' in #{caller[0]}")
  end
end

Instance Attribute Details

#serviceObject (readonly)

Returns the value of attribute service.



6
7
8
# File 'lib/sdl/receivers/service_receiver.rb', line 6

def service
  @service
end