Class: Rapid::Model::View

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ury_rapid/model/view.rb

Overview

A view into the Rapid model

A View tracks two different model roots: the 'global root', which is the top of the entire model, and the 'local root', which is the top of the part of the model the View's user is authorised to update directly. For example, a Service generally has a View whose local root is that Service's sub-model.

The global/local distinction is primarily one of security: only the owner of a section of model can actually get at it directly, and only authorised services/clients can interact with other parts of the model (and, thus, other services).

From the global root, a View can perform indirect queries (#get, #post, #put, and #delete). These are named by analogy to HTTP and REST APIs, primarily for historical reasons; they require the View's caller to have sufficient privileges (via a PrivilegeSet); and they do not directly update the model but instead request that whatever handler code is attached to the affected parts of the model is activated.

From the local root, a View can grab model objects directly by URL (#find), as well as perform direct updates on the model structure (#insert, #replace, and #kill). These do not require privileges, and directly manipulate the model without any handlers being called.

Instance Method Summary collapse

Constructor Details

#initialize(global_root, local_root) ⇒ View

Initialises the model view


42
43
44
45
# File 'lib/ury_rapid/model/view.rb', line 42

def initialize(global_root, local_root)
  @global_root = global_root
  @local_root  = local_root
end

Instance Method Details

#find(url, &block) ⇒ ModelObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Finds a model object in the local root given its URL


113
114
115
# File 'lib/ury_rapid/model/view.rb', line 113

def find(url, &block)
  find_in(@local_root, url, &block)
end

#get(url) ⇒ Object

TODO: This is a security risk! Maybe have a GetWrapper class that restricts what can be done


85
86
87
# File 'lib/ury_rapid/model/view.rb', line 85

def get(url)
  global_find(url) { |object| yield object }
end

#log(severity, message) ⇒ void

This method returns an undefined value.

Logs an error message in the Rapid log

Examples:

Log an error.

view.log(:error, 'The system is down!')

79
80
81
# File 'lib/ury_rapid/model/view.rb', line 79

def log(severity, message)
  global_find('log') { |log| log.insert(severity, message) }
end

#with_local_root(local_root) ⇒ View

Creates a View of the same global root, but a different local root

This is usually used in the situation where a service A has a view VA, and is creating a child service B. VB should contain a reference to the same global root, as it is on the same model as A and VA, but B should not be able to update all of A's model space, so its view VB is VA but with the local root set to B's sub-model.


60
61
62
# File 'lib/ury_rapid/model/view.rb', line 60

def with_local_root(local_root)
  View.new(@global_root, local_root)
end