Class: Serf::ParcelFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/serf/parcel_factory.rb

Overview

Creates Parcels as Hashie::Mash objects with headers and messages.

The headers this factory sets are:

  • kind

  • uuid

  • parent_uuid

  • origin_uuid

The message field:

  • message

The UUID fields are created using the uuidable processed from the parent parcel.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ ParcelFactory

Returns a new instance of ParcelFactory.



27
28
29
30
31
32
# File 'lib/serf/parcel_factory.rb', line 27

def initialize(*args)
  opts = Optser.extract_options! args

  @mash_class = opts.get :mash_class, Hashie::Mash
  @uuidable = opts.get(:uuidable) { Serf::Util::Uuidable.new }
end

Instance Attribute Details

#mash_classObject (readonly)

Returns the value of attribute mash_class.



24
25
26
# File 'lib/serf/parcel_factory.rb', line 24

def mash_class
  @mash_class
end

#uuidableObject (readonly)

Returns the value of attribute uuidable.



25
26
27
# File 'lib/serf/parcel_factory.rb', line 25

def uuidable
  @uuidable
end

Instance Method Details

#create(*args) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/serf/parcel_factory.rb', line 34

def create(*args)
  opts = Optser.extract_options! args

  # Get parameters
  kind = opts.get :kind
  parent = opts.get :parent, {}
  headers = opts.get :headers, {}
  message = opts.get :message, {}

  # Create a new parcel, with the header fields as base of object.
  # Merge in the new UUIDs, overwriting anything set in headers.
  # Merge in the kind and message, overwriting anything already set.
  parcel = mash_class.new(headers)
  parcel.merge! uuidable.create_uuids(parent)
  parcel.merge! kind: kind, message: message

  return parcel
end