Class: Missions::DSL::Client::Proxy

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/missions/dsl/client.rb

Overview

Client side dsl proxy

Mechanism to allow clients to specify dsl methods to be used in server side operations.

Since these dsl methods are not serializable the client sends instances of this proxy in place which will be resolved on the server side

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#dsl_module_for, included, #is_dsl_category?

Constructor Details

#initialize(args = {}) ⇒ Proxy

Returns a new instance of Proxy


45
46
47
48
49
# File 'lib/missions/dsl/client.rb', line 45

def initialize(args={})
  attr_from_args args, :dsl_category => nil,
                       :dsl_method   => nil,
                       :params       =>  []
end

Instance Attribute Details

#dsl_categoryObject

Returns the value of attribute dsl_category


41
42
43
# File 'lib/missions/dsl/client.rb', line 41

def dsl_category
  @dsl_category
end

#dsl_methodObject

Returns the value of attribute dsl_method


42
43
44
# File 'lib/missions/dsl/client.rb', line 42

def dsl_method
  @dsl_method
end

#paramsObject

Returns the value of attribute params


43
44
45
# File 'lib/missions/dsl/client.rb', line 43

def params
  @params
end

Class Method Details

.json_create(o) ⇒ Object

Create new Proxy instance from JSON representation


133
134
135
# File 'lib/missions/dsl/client.rb', line 133

def self.json_create(o)
  new(o['data'])
end

.method_missing(method_id, *args) ⇒ Object

Create Proxy instances for DSL methods invoked directly on the class


26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/missions/dsl/client.rb', line 26

def self.method_missing(method_id, *args)
  DSL.constants.each { |c|
    dc = DSL.const_get(c)

    # XXX different dsl categories cannot define methods
    # with the same name, would be nice to resolve this:
    if(dc.methods.include?(method_id))
      return Proxy.new :dsl_category => c.to_s,
                       :dsl_method   => method_id.to_s,
                       :params       => args
    end
  }
  nil
end

.resolve(args = {}) ⇒ Object

Resolve all DSL proxies in mission or mission event handler.

Pass in the Mission or Missions::EventHandler::DSL instance to resolve proxy reference for. This method will resplace all proxies in the mission / event handler callbacks with their resolved DSL method


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/missions/dsl/client.rb', line 56

def self.resolve(args={})
  # specify mission or event handler to process
  mission       = args[:mission]
  event_handler = args[:event_handler]

  # Iterate through mission callbacks
  Mission::CALLBACKS.each { |cb|
    dsl_category = dsl_category_for(cb)

    # Retrieve callbacks registered with mission, iterate through arrays
    cbs = mission.send(cb)
    cbs.each_index { |i|

      # Resolve proxy
      if cbs[i].is_a?(Proxy)
        cbs[i] = cbs[i].resolve

      # If proxy specified is a string/symbol, assume it is for the
      # DSL method in the default category for the callback being processed
      elsif cbs[i].is_a?(String)
        proxy = Proxy.new :dsl_category => dsl_category.to_s,
                          :dsl_method   => cbs[i]
        cbs[i] = proxy.resolve

      # If proxy specified is an array, assume first param is the
      # DSL method in the default category for the callback being processed
      elsif cbs[i].is_a?(Array)
        dsl_method = cbs[i].shift
        proxy = Proxy.new :dsl_category => dsl_category.to_s,
                          :dsl_method   => dsl_method,
                          :params       => cbs[i]
        cbs[i] = proxy.resolve
      end
    }
  } if mission

  # Iterated over DSL Event Handler mission callbacks, resolving proxies
  event_handler.missions_callbacks.each_index { |cbi|
    cb = event_handler.missions_callbacks[cbi]
    event_handler.missions_callbacks[cbi] = cb.resolve if cb.is_a?(Proxy)
  } if event_handler
end

Instance Method Details

#resolveObject

Resolve this Proxy instance by invoking the DSL category method with the specified params


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/missions/dsl/client.rb', line 101

def resolve
  # Retrieve DSL module which to invoke method
  short_category = dsl_category.to_s.demodulize
  return unless is_dsl_category?(short_category)
  dcategory = dsl_module_for(short_category)

  # Retrieve DSL method in module to invoike
  return unless dcategory.has_dsl_method?(dsl_method.to_s)
  dmethod = dcategory.method(dsl_method)

  # Scan through params, call resolve on proxies
  params.each_index { |i|
    param     = params[i]
    params[i] = param.resolve if param.is_a?(Proxy)
  }

  # Invoke DSL method, returning results
  dmethod.call *params
end

#to_json(*a) ⇒ Object

Convert Proxy instance to JSON


122
123
124
125
126
127
128
129
130
# File 'lib/missions/dsl/client.rb', line 122

def to_json(*a)
   {
     'json_class'     => self.class.name,
     'data'           =>
       {:dsl_category => dsl_category,
        :dsl_method   => dsl_method,
        :params       => params}
   }.to_json(*a)
end