Class: Rapid::Services::Environment

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

Overview

An environment object

Every Service in Rapid is provided with an Environment, which represents the Service's connection to the rest of Rapid.

An Environment allows Services to:

  • Query (get/put/post/delete) any part of the Rapid model, as long as the Service can authenticate with enough privileges to perform the query;

  • Directly modify (insert/replace/kill) its own part of the Rapid model;

  • Insert components (pre-made model sub-structures) into its own part of the Rapid model;

  • Perform authentication, either for itself or on behalf of an external client;

  • Register to, and deregister from, the model updates channel.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(authenticator, update_channel, view) ⇒ Environment

Initialises an Environment


35
36
37
38
39
40
# File 'lib/ury_rapid/services/environment.rb', line 35

def initialize(authenticator, update_channel, view)
  @authenticator  = authenticator
  @update_channel = update_channel
  @view           = view
  @handlers       = Hash.new(Rapid::Services::Requests::NullHandler.new)
end

Class Method Details

.for_root(authenticator, update_channel, root) ⇒ Object

Creates an Environment for the root service

This contains a View that has the tip of the Rapid model as both its local and global root.


46
47
48
49
50
# File 'lib/ury_rapid/services/environment.rb', line 46

def self.for_root(authenticator, update_channel, root)
  Environment.new(authenticator,
                  update_channel,
                  Rapid::Model::View.new(root, root))
end

Instance Method Details

#create_component(name, *args) ⇒ Object


85
86
87
88
89
90
# File 'lib/ury_rapid/services/environment.rb', line 85

def create_component(name, *args)
  Rapid::Model::ComponentCreatorWrapper.new(
    Rapid::Model::ComponentCreator.new,
    method(:register)
  ).send(name, *args)
end

#insert_component(url, name, *args) ⇒ Object


92
93
94
# File 'lib/ury_rapid/services/environment.rb', line 92

def insert_component(url, name, *args)
  insert(url, create_component(name, *args))
end

#insert_components(url, &block) ⇒ Object

Begins inserting multiple components into the local root at the given URL


102
103
104
# File 'lib/ury_rapid/services/environment.rb', line 102

def insert_components(url, &block)
  Rapid::Model::ComponentInserter.insert(url, self, method(:register), &block)
end

#replace_component(url, name, *args) ⇒ Object


96
97
98
# File 'lib/ury_rapid/services/environment.rb', line 96

def replace_component(url, name, *args)
  replace(url, create_component(name, *args))
end

#with_local_root(local_root) ⇒ Object

Creates a new Environment from this one, substituting the local root

This is usually used when creating a Service that is a child of an existing Service: fork off the parent Service's Environment, changing the View to point to the child Service's model.


64
65
66
# File 'lib/ury_rapid/services/environment.rb', line 64

def with_local_root(local_root)
  with_view(view.with_local_root(local_root))
end

#with_view(new_view) ⇒ Object

Creates a new Environment from this one, substituting the View

The new Environment will have no handlers registered.


55
56
57
# File 'lib/ury_rapid/services/environment.rb', line 55

def with_view(new_view)
  Environment.new(authenticator, update_channel, new_view)
end