Class: Tochtli::Message
- Inherits:
-
Object
show all
- Extended by:
- Uber::InheritableAttr
- Includes:
- SimpleValidation
- Defined in:
- lib/tochtli/message.rb
Instance Attribute Summary collapse
#errors
Class Method Summary
collapse
Instance Method Summary
collapse
#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
(attributes)
end
|
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
12
13
14
|
# File 'lib/tochtli/message.rb', line 12
def id
@id
end
|
#properties ⇒ Object
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.
required = options.fetch(:validate, true)
attributes.each do |name|
attribute name, String, required: required
end
end
|
.generate_id ⇒ Object
86
87
88
|
# File 'lib/tochtli/message.rb', line 86
def self.generate_id
SecureRandom.uuid
end
|
38
39
40
|
# File 'lib/tochtli/message.rb', line 38
def self.
self. = :ignore
end
|
.optional_attributes(*attributes) ⇒ Object
33
34
35
36
|
# File 'lib/tochtli/message.rb', line 33
def self.optional_attributes(*attributes)
options = attributes.
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.
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
(attributes)
end
|
#routing_key ⇒ Object
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
|
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/tochtli/message.rb', line 57
def (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_hash ⇒ Object
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_json ⇒ Object
106
107
108
|
# File 'lib/tochtli/message.rb', line 106
def to_json
JSON.dump(to_hash)
end
|
#validate ⇒ Object
81
82
83
84
|
# File 'lib/tochtli/message.rb', line 81
def validate
validate_attributes_presence
end
|
#validate_attributes_presence ⇒ Object
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
|
68
69
70
71
72
|
# File 'lib/tochtli/message.rb', line 68
def
if self.class. != :ignore && !@extra_attributes.empty?
add_error "Unexpected attributes: #{@extra_attributes.keys.map(&:to_s).join(', ')}"
end
end
|