Class: BindAction

Inherits:
Action show all
Defined in:
lib/MINT-core/mapping/action/bind_action.rb

Instance Method Summary collapse

Methods inherited from Action

#finished_callback, #id

Constructor Details

#initialize(params) ⇒ BindAction

Returns a new instance of BindAction.



4
5
6
7
8
9
# File 'lib/MINT-core/mapping/action/bind_action.rb', line 4

def initialize(params)
  super()
  @action = params
  @cb_observation_has_subscribed=nil
  @is_bound = false
end

Instance Method Details

#attrInObject



37
38
39
# File 'lib/MINT-core/mapping/action/bind_action.rb', line 37

def attrIn
  @action[:attrIn]
end

#attrOutObject



41
42
43
# File 'lib/MINT-core/mapping/action/bind_action.rb', line 41

def attrOut
  @action[:attrOut]
end

#channelInObject



45
46
47
# File 'lib/MINT-core/mapping/action/bind_action.rb', line 45

def channelIn
  "#{attrIn}:#{elementIn}.#{nameIn}"
end

#channelOutObject



49
50
51
# File 'lib/MINT-core/mapping/action/bind_action.rb', line 49

def channelOut
  "#{attrOut}:#{elementOut}.#{nameOut}"
end

#elementInObject



17
18
19
# File 'lib/MINT-core/mapping/action/bind_action.rb', line 17

def elementIn
  @action[:elementIn]
end

#elementOutObject



21
22
23
# File 'lib/MINT-core/mapping/action/bind_action.rb', line 21

def elementOut
  @action[:elementOut]
end

#identifierObject



25
26
27
# File 'lib/MINT-core/mapping/action/bind_action.rb', line 25

def identifier
  elementIn
end

#initiated_callback(cb) ⇒ Object



11
12
13
# File 'lib/MINT-core/mapping/action/bind_action.rb', line 11

def initiated_callback(cb)
  @cb_observation_has_subscribed = cb
end

#nameInObject



29
30
31
# File 'lib/MINT-core/mapping/action/bind_action.rb', line 29

def nameIn
  @action[:nameIn]
end

#nameOutObject



33
34
35
# File 'lib/MINT-core/mapping/action/bind_action.rb', line 33

def nameOut
  @action[:nameOut]
end

#start(observation_results) ⇒ Object

TODO handle observation_variables



54
55
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
# File 'lib/MINT-core/mapping/action/bind_action.rb', line 54

def start(observation_results)            # TODO handle observation_variables
  @result = false
  if @is_bound
    @cb_observation_has_subscribed.call(elementIn) if @cb_observation_has_subscribed
    return self
  end

  redis = RedisConnector.redis
  redis.pubsub.subscribe(channelIn) { |message|


    found=MultiJson.decode message

    if nameIn.eql? found['name']

      if found.has_key? @action[:attrIn]
        result =  found[@action[:attrIn]]
        result = @action[:transform].call result if @action[:transform]
        redis.publish channelOut, MultiJson.encode({:name=>@action[:nameOut], @action[:attrOut] => result})

      end

    end
  }.callback do
    @is_bound = true
    @cb_observation_has_subscribed.call(elementIn) if @cb_observation_has_subscribed
  end
  @result = true
  self
end

#unbindObject



85
86
87
# File 'lib/MINT-core/mapping/action/bind_action.rb', line 85

def unbind
  RedisConnector.sub.unsubscribe(@action[:in])
end