Class: ZeevexCluster::Message

Inherits:
Hash
  • Object
show all
Includes:
Serializer
Defined in:
lib/zeevex_cluster/message.rb

Constant Summary collapse

REQUIRED_KEYS =
%w{source sequence sent_at expires_at contents content_type}.
map {|x| x.to_sym}
ALLOWED_KEYS =
%w{vclock options flags encoding}.
map {|x| x.to_sym}
FORBIDDEN_KEYS =
['$primitive', '$type', '$encoding']
ALL_KEYS =
REQUIRED_KEYS + ALLOWED_KEYS

Instance Method Summary collapse

Methods included from Serializer

#included

Constructor Details

#initialize(hash) ⇒ Message

Returns a new instance of Message.



16
17
18
19
20
21
22
23
# File 'lib/zeevex_cluster/message.rb', line 16

def initialize(hash)
  super()
  hash.keys.each do |x|
    raise ArgumentError, 'Only symbol keys are allowed in Messages' unless x.is_a?(Symbol)
  end
  self.merge! :content_type => 'application/json'
  self.merge! hash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object (protected)



41
42
43
44
45
46
47
48
49
# File 'lib/zeevex_cluster/message.rb', line 41

def method_missing(meth, *args, &block)
  if ALL_KEYS.include?(meth.to_sym)
    self[meth]
  elsif ALL_KEYS.include?(key = meth.to_s.chomp('=').to_sym)
    self[key] = args[0]
  else
    super
  end
end

Instance Method Details

#respond_to?(meth) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
# File 'lib/zeevex_cluster/message.rb', line 31

def respond_to?(meth)
  if ALL_KEYS.include?(meth.to_s.chomp('=').to_sym)
    true
  else
    super
  end
end

#valid?Boolean

Returns:

  • (Boolean)


25
26
27
28
29
# File 'lib/zeevex_cluster/message.rb', line 25

def valid?
  vkeys = self.keys
  (REQUIRED_KEYS - vkeys).empty? &&
      (FORBIDDEN_KEYS & vkeys).empty?
end