Module: ActionService::Container::ClassMethods

Defined in:
lib/action_service/container.rb

Instance Method Summary collapse

Instance Method Details

#add_service_definition_callback(&block) ⇒ Object

:nodoc:



70
71
72
# File 'lib/action_service/container.rb', line 70

def add_service_definition_callback(&block) # :nodoc:
  write_inheritable_array("service_definition_callbacks", [block])
end

#has_service?(name) ⇒ Boolean

Returns true if #service was used to declare a service with the given name.

Returns:

  • (Boolean)


62
63
64
# File 'lib/action_service/container.rb', line 62

def has_service?(name)
  services.has_key?(name.to_sym)
end

#service(name, object = nil, &block) ⇒ Object

Declares a service that will provides access to the exported API of the given service object. object must be an ActionService::Base derivative.

Example

class ApiController < ApplicationController
  service_dispatching_mode :delegated

  service :person, PersonApi.new
end

This will generate a /api/person action in the controller. All exported methods of object will be available for invocation by clients sending protocol messages to /api/person with HTTP POST.

You can also declare a service that defers the service object creation until request time. To do this, pass a block instead of a service object instance. The block will be executed in instance context, so it will have access to controller instance variables. Its return value should be the service object.

Deferred Example

class ApiController < ApplicationController
  service_dispatching_mode :delegated

  service(:person) { PersonApi.new }
end


46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/action_service/container.rb', line 46

def service(name, object=nil, &block)
  if (object && block_given?) || (object.nil? && block.nil?)
    raise(ContainerError, "either service, or a block must be given")
  end
  name = name.to_sym
  if block_given?
    info = { name => { :block => block } }
  else
    info = { name => { :object => object } }
  end
  write_inheritable_hash("action_services", info)
  call_service_definition_callbacks(self, name, info)
end

#servicesObject

:nodoc:



66
67
68
# File 'lib/action_service/container.rb', line 66

def services # :nodoc:
  read_inheritable_attribute("action_services") || {}
end