Class: Tochtli::Message

Inherits:
Object
  • Object
show all
Extended by:
Uber::InheritableAttr
Includes:
SimpleValidation
Defined in:
lib/tochtli/message.rb

Direct Known Subclasses

ErrorMessage

Instance Attribute Summary collapse

Attributes included from SimpleValidation

#errors

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SimpleValidation

#add_error, #invalid?, #valid?

Constructor Details

#initialize(attributes = {}, properties = nil) ⇒ Message

Returns a new instance of Message.



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

def initialize(attributes={}, properties=nil)
  super attributes || {}

  @properties = properties
  @id         = properties.message_id if properties
  @id         ||= self.class.generate_id

  store_extra_attributes(attributes)
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



12
13
14
# File 'lib/tochtli/message.rb', line 12

def id
  @id
end

#propertiesObject (readonly)

Returns the value of attribute properties.



12
13
14
# File 'lib/tochtli/message.rb', line 12

def properties
  @properties
end

Class Method Details

.attributes(*attributes) ⇒ Object

Compatibility with version 0.3



19
20
21
22
23
24
25
26
# File 'lib/tochtli/message.rb', line 19

def self.attributes(*attributes)
  options  = attributes.extract_options!
  required = options.fetch(:validate, true)

  attributes.each do |name|
    attribute name, String, required: required
  end
end

.generate_idObject



86
87
88
# File 'lib/tochtli/message.rb', line 86

def self.generate_id
  SecureRandom.uuid
end

.ignore_extra_attributesObject



38
39
40
# File 'lib/tochtli/message.rb', line 38

def self.ignore_extra_attributes
  self.extra_attributes_policy = :ignore
end

.optional_attributes(*attributes) ⇒ Object



33
34
35
36
# File 'lib/tochtli/message.rb', line 33

def self.optional_attributes(*attributes)
  options = attributes.extract_options!
  self.attributes *attributes, options.merge(validate: false)
end

.required_attributes(*attributes) ⇒ Object



28
29
30
31
# File 'lib/tochtli/message.rb', line 28

def self.required_attributes(*attributes)
  options = attributes.extract_options!
  self.attributes *attributes, options.merge(validate: true)
end

.route_to(routing_key = nil, &block) ⇒ Object



14
15
16
# File 'lib/tochtli/message.rb', line 14

def self.route_to(routing_key=nil, &block)
  self.routing_key = routing_key || block
end

Instance Method Details

#attributes=(attributes) ⇒ Object



52
53
54
55
# File 'lib/tochtli/message.rb', line 52

def attributes=(attributes)
  super
  store_extra_attributes(attributes)
end

#routing_keyObject



90
91
92
93
94
95
96
# File 'lib/tochtli/message.rb', line 90

def routing_key
			if self.class.routing_key.is_a?(Proc)
self.instance_eval(&self.class.routing_key)
			else
    self.class.routing_key
			end
end

#store_extra_attributes(attributes) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/tochtli/message.rb', line 57

def store_extra_attributes(attributes)
  @extra_attributes ||= {}
  if attributes
    attributes.each do |name, value|
      unless allowed_writer_methods.include?("#{name}=")
        @extra_attributes[name] = value
      end
    end
  end
end

#to_hashObject



98
99
100
101
102
103
104
# File 'lib/tochtli/message.rb', line 98

def to_hash
  attributes.inject({}) do |hash, (name, value)|
      value = value.map(&:to_hash) if value.is_a?(Array)
      hash[name] = value
      hash
  end
end

#to_jsonObject



106
107
108
# File 'lib/tochtli/message.rb', line 106

def to_json
  JSON.dump(to_hash)
end

#validateObject



81
82
83
84
# File 'lib/tochtli/message.rb', line 81

def validate
  validate_extra_attributes
  validate_attributes_presence
end

#validate_attributes_presenceObject



74
75
76
77
78
79
# File 'lib/tochtli/message.rb', line 74

def validate_attributes_presence
  nil_attributes = attribute_set.select { |a| a.required? && self[a.name].nil? }.map(&:name)
  unless nil_attributes.empty?
    add_error "Required attributes: #{nil_attributes.map(&:to_s).join(', ')} not specified"
  end
end

#validate_extra_attributesObject



68
69
70
71
72
# File 'lib/tochtli/message.rb', line 68

def validate_extra_attributes
  if self.class.extra_attributes_policy != :ignore && !@extra_attributes.empty?
    add_error "Unexpected attributes: #{@extra_attributes.keys.map(&:to_s).join(', ')}"
  end
end