Skip to content

Commit

Permalink
Merge pull request #18 from aaronjensen/properties-hash
Browse files Browse the repository at this point in the history
Properties and local properties are separate hashes
  • Loading branch information
sbellware authored Mar 8, 2021
2 parents d561beb + 7c7cd93 commit 5a6571b
Show file tree
Hide file tree
Showing 33 changed files with 434 additions and 513 deletions.
1 change: 0 additions & 1 deletion lib/messaging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

require 'messaging/message'
require 'messaging/message/metadata'
require 'messaging/message/metadata/property'
require 'messaging/message/copy'
require 'messaging/message/follow'
require 'messaging/message/sequence_accessor'
Expand Down
1 change: 1 addition & 0 deletions lib/messaging/controls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require 'messaging/controls/id'
require 'messaging/controls/stream_name'
require 'messaging/controls/properties'
require 'messaging/controls/local_properties'
require 'messaging/controls/metadata'
require 'messaging/controls/message_data'
require 'messaging/controls/message'
Expand Down
39 changes: 39 additions & 0 deletions lib/messaging/controls/local_properties.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module Messaging
module Controls
module LocalProperties
def self.example
{
SomeLocalProperty.name => SomeLocalProperty.value
}
end

module SomeLocalProperty
def self.name
:some_local_property
end

def self.value
'some local property value'
end
end

module Random
def self.example
{
SomeLocalProperty.name => SomeLocalProperty.value
}
end

module SomeLocalProperty
def self.name
:some_local_property
end

def self.value
SecureRandom.hex
end
end
end
end
end
end
16 changes: 14 additions & 2 deletions lib/messaging/controls/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def self.example
data = self.data

data[:properties] = properties
data[:local_properties] = local_properties

Messaging::Message::Metadata.build(data)
end
Expand Down Expand Up @@ -65,6 +66,10 @@ def self.properties
Properties.example
end

def self.local_properties
LocalProperties.example
end

def self.data
{
stream_name: stream_name,
Expand All @@ -79,7 +84,8 @@ def self.data

reply_stream_name: reply_stream_name,

properties: Properties.data,
properties: Properties.example,
local_properties: LocalProperties.example,

time: time,

Expand Down Expand Up @@ -115,6 +121,7 @@ def self.example
data = self.data

data[:properties] = properties
data[:local_properties] = local_properties

Messaging::Message::Metadata.build(data)
end
Expand Down Expand Up @@ -171,6 +178,10 @@ def self.properties
Properties::Random.example
end

def self.local_properties
LocalProperties::Random.example
end

def self.data
{
stream_name: stream_name,
Expand All @@ -185,7 +196,8 @@ def self.data

reply_stream_name: reply_stream_name,

properties: Properties::Random.data,
properties: Properties::Random.example,
local_properties: LocalProperties::Random.example,

time: time,

Expand Down
96 changes: 7 additions & 89 deletions lib/messaging/controls/properties.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,113 +2,31 @@ module Messaging
module Controls
module Properties
def self.example
[
SomeProperty.example,
SomeLocalProperty.example
]
end

def self.data
[
SomeProperty.data,
SomeLocalProperty.data
]
{
SomeProperty.name => SomeProperty.value
}
end

module SomeProperty
def self.example
Messaging::Message::Metadata::Property.new(name, value)
end

def self.data
{
name: name,
value: value
}
end

def self.name
'some_property'
:some_property
end

def self.value
'some property value'
end
end

module SomeLocalProperty
module Random
def self.example
Messaging::Message::Metadata::Property.new(name, value, true)
end

def self.data
{
name: name,
value: value,
local: true
SomeProperty.name => SomeProperty.value
}
end

def self.name
'some_local_property'
end

def self.value
'some local property value'
end
end

module Random
def self.example
[
SomeProperty.example,
SomeLocalProperty.example
]
end

def self.data
[
SomeProperty.data,
SomeLocalProperty.data
]
end

module SomeProperty
def self.example
Messaging::Message::Metadata::Property.new(name, value)
end

def self.data
{
name: name,
value: value
}
end

def self.name
'some_property'
end

def self.value
SecureRandom.hex
end
end

module SomeLocalProperty
def self.example
Messaging::Message::Metadata::Property.new(name, value, true)
end

def self.data
{
name: name,
value: value,
local: true
}
end

def self.name
'some_local_property'
:some_property
end

def self.value
Expand Down
7 changes: 3 additions & 4 deletions lib/messaging/message/copy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@ def copy(source, receiver=nil, copy: nil, include: nil, exclude: nil, metadata:
end

if metadata
metadata_include = source.metadata.class.attribute_names - [:properties]
metadata_include = source.metadata.class.attribute_names - [:properties, :local_properties]

SetAttributes.(receiver.metadata, source.metadata, include: metadata_include)

source.metadata.properties.each do |property|
receiver.metadata.properties << property.dup
end
receiver.metadata.properties = source.metadata.properties.dup
receiver.metadata.local_properties = source.metadata.local_properties.dup
end

receiver
Expand Down
72 changes: 42 additions & 30 deletions lib/messaging/message/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class Error < RuntimeError; end

attribute :reply_stream_name, String

attribute :properties, Array, default: -> { Array.new }
attribute :properties, Hash, default: -> { Hash.new }
attribute :local_properties, Hash, default: -> { Hash.new }

attribute :time, Time

Expand All @@ -60,12 +61,8 @@ def follow(preceding_metadata)

self.reply_stream_name = preceding_metadata.reply_stream_name

preceding_metadata.properties.each do |property|
if property.local?
next
end

properties << property.dup
preceding_metadata.properties.each do |name, value|
properties[name] = value
end
end

Expand Down Expand Up @@ -136,49 +133,64 @@ def correlated?(stream_name)
end
alias :correlates? :correlated?

def set_property(name, value, local: nil)
if not name.is_a?(String)
raise Error, "Property name must be a string: #{name.inspect}"
def set_property(name, value)
if not name.is_a?(Symbol)
raise Error, "Property name must be a symbol: #{name.inspect}"
end

local ||= false
properties[name] = value

delete_property(name)
value
end

def get_property(name)
if not name.is_a?(Symbol)
raise Error, "Property name must be a symbol: #{name.inspect}"
end

property = Property.new(name, value, local)
properties[name]
end

properties << property
def delete_property(name)
if not name.is_a?(Symbol)
raise Error, "Property name must be a symbol: #{name.inspect}"
end

property
properties.delete(name)
end

def set_local_property(name, value)
set_property(name, value, local: true)
def clear_properties
properties.clear
end

def get_property(name)
if not name.is_a?(String)
raise Error, "Property name must be a string: #{name.inspect}"
def set_local_property(name, value)
if not name.is_a?(Symbol)
raise Error, "Local property name must be a symbol: #{name.inspect}"
end

property = properties.find { |property| property.name == name }
property&.value
local_properties[name] = value

value
end

def delete_property(name)
if not name.is_a?(String)
raise Error, "Property name must be a string: #{name.inspect}"
def get_local_property(name)
if not name.is_a?(Symbol)
raise Error, "Local property name must be a symbol: #{name.inspect}"
end

i = properties.index { |property| property.name == name }
local_properties[name]
end

return nil if i.nil?
def delete_local_property(name)
if not name.is_a?(Symbol)
raise Error, "Local property name must be a symbol: #{name.inspect}"
end

properties.delete_at(i).value
local_properties.delete(name)
end

def clear_properties
properties.clear
def clear_local_properties
local_properties.clear
end

def self.source_attribute_names
Expand Down
22 changes: 0 additions & 22 deletions lib/messaging/message/metadata/property.rb

This file was deleted.

Loading

0 comments on commit 5a6571b

Please sign in to comment.