class Tochtli::Message

Attributes

id[R]
properties[R]

Public Class Methods

attributes(*attributes) click to toggle source

Compatibility with version 0.3

# 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_id() click to toggle source
# File lib/tochtli/message.rb, line 86
def self.generate_id
  SecureRandom.uuid
end
ignore_extra_attributes() click to toggle source
# File lib/tochtli/message.rb, line 38
def self.ignore_extra_attributes
  self.extra_attributes_policy = :ignore
end
new(attributes={}, properties=nil) click to toggle source
Calls superclass method
# 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
optional_attributes(*attributes) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/tochtli/message.rb, line 14
def self.route_to(routing_key=nil, &block)
  self.routing_key = routing_key || block
end

Public Instance Methods

attributes=(attributes) click to toggle source
Calls superclass method
# File lib/tochtli/message.rb, line 52
def attributes=(attributes)
  super
  store_extra_attributes(attributes)
end
routing_key() click to toggle source
# 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) click to toggle source
# 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_hash() click to toggle source
# 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() click to toggle source
# File lib/tochtli/message.rb, line 106
def to_json
  JSON.dump(to_hash)
end
validate() click to toggle source
# File lib/tochtli/message.rb, line 81
def validate
  validate_extra_attributes
  validate_attributes_presence
end
validate_attributes_presence() click to toggle source
# 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_attributes() click to toggle source
# 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