Module: Rapid::Model::ModelObject

Extended by:
Forwardable
Includes:
Kankri::PrivilegeSubject, Updatable
Included in:
MockModelObject, CompositeModelObject, Constant, Item, Log
Defined in:
lib/ury_rapid/model/model_object.rb

Overview

An object in the Rapid playout system model

ModelObjects are composable entities, each assigned an ID by its parent, that form a model tree traversable by relative URLs.

Each ModelObject implements an interface based on the HTTP verbs GET, PUT, POST and DELETE, both directly and via URL (for accessing objects further down in the tree from the target).

With the exception of GET, each verb is further subdivided into a form that can trigger service handlers to translate model change requests into playout server actions, and a 'service' form that bypasses these handlers.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Updatable

#deregister_from_updates, #notify_delete, #notify_update, #register_for_updates, #register_update_channel

Instance Attribute Details

#handler_targetSymbol (readonly) Also known as: privilege_key

The identifier used to find handlers and privileges for this object

When @handler_target is nil (the default), this is the class name, stripped of its module and converted to a lowercase_underscored Symbol. If @handler_target is specified, however, that will be returned instead.

Examples:

Get the default handler target.

ModelObject.new.handler_target
#=> :model_object

Get a specified handler target.

ModelObject.new(:widget).handler_target
#=> :widget

136
137
138
# File 'lib/ury_rapid/model/model_object.rb', line 136

def handler_target
  @handler_target
end

Instance Method Details

#default_handler_targetSymbol

The default handler target for this class

When @handler_target is nil (the default), this is the class name, stripped of its module and converted to a lowercase_underscored Symbol. If @handler_target is specified, however, that will be returned instead.

Examples:

Get the default handler target.

ModelObject.new.handler_target
#=> :model_object

Get a specified handler target.

ModelObject.new(:widget).handler_target
#=> :widget

155
156
157
# File 'lib/ury_rapid/model/model_object.rb', line 155

def default_handler_target
  self.class.name.demodulize.underscore.intern
end

#default_idObject

Returns the default ID to give to POST payloads


160
161
162
# File 'lib/ury_rapid/model/model_object.rb', line 160

def default_id
  nil
end

#flatObject

The flat representation of this model object

Usually, this is just the mapping of #flat to this object's children. Objects should override this if they are not simply containers of other children.


64
65
66
# File 'lib/ury_rapid/model/model_object.rb', line 64

def flat
  children.transform_values(&:flat)
end

#get(privileges) ⇒ Object

GETs this model object

A GET is the retrieval of a flattened representation of a model object. See #flat (defined differently for different model objects) for information on what constitutes a flattened representation.


54
55
56
57
# File 'lib/ury_rapid/model/model_object.rb', line 54

def get(privileges)
  fail_if_cannot(:get, privileges)
  flat
end

#initialize(handler_target = nil, *args) ⇒ Object


26
27
28
29
30
# File 'lib/ury_rapid/model/model_object.rb', line 26

def initialize(handler_target = nil, *args)
  super(*args)
  @handler_target = handler_target || default_handler_target
  @update_channel = NoUpdateChannel.new
end

#insert(id, resource) ⇒ void

This method returns an undefined value.

Default implementation of insert

This will, by default, move the incoming resource to this object under the given ID, replacing any existing resource.


101
102
103
104
105
# File 'lib/ury_rapid/model/model_object.rb', line 101

def insert(id, resource)
  return if resource.nil?
  resource.move_to(self, id)
  resource.notify_update
end

#killvoid

This method returns an undefined value.

Default implementation of kill on model objects

This instructs the object's children to kill themselves. Since #each is a no-op on Compo::Branches::Leaf, this is safe to use with any model object.


114
115
116
# File 'lib/ury_rapid/model/model_object.rb', line 114

def kill
  each { |_, value| value.kill }
end

#post_to_parent(payload) ⇒ Object

Takes a PUT payload intended for this object and POSTs it to its parent


69
70
71
# File 'lib/ury_rapid/model/model_object.rb', line 69

def post_to_parent(payload)
  parent.post(payload)
end

#register_handler(handler) ⇒ self

Registers a handler to be called when this object is modified


40
41
42
43
# File 'lib/ury_rapid/model/model_object.rb', line 40

def register_handler(handler)
  @handler = handler
  self
end

#replace(resource) ⇒ void

This method returns an undefined value.

Default implementation of replace

This will just insert into the parent.


88
89
90
# File 'lib/ury_rapid/model/model_object.rb', line 88

def replace(resource)
  parent.insert(id, resource)
end