Class: CaRuby::PersistenceService

Inherits:
Object
  • Object
show all
Defined in:
lib/caruby/database/persistence_service.rb

Overview

A PersistenceService is a database mediator which implements the #query #create, #update and #delete methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}) ⇒ PersistenceService

Creates a new PersistenceService with the specified application service name and options.

Parameters:

  • the (String)

    caBIG application service name

  • opts ({Symbol => Object}) (defaults to: {})

    the options

Options Hash (opts):

  • :host (String)

    the service host (default localhost)

  • :version (String)

    the caTissue version identifier



21
22
23
24
25
26
27
28
29
30
# File 'lib/caruby/database/persistence_service.rb', line 21

def initialize(name, opts={})
  @name = name
  ver_opt = opts[:version]
  @version = ver_opt.to_s.to_version if ver_opt
  @host = opts[:host] || 'localhost'
  @port = opts[:port] || 8080
  @url = "http://#{@host}:#{@port}/#{@name}/http/remoteService"
  @timer = Jinx::Stopwatch.new
  logger.debug { "Created persistence service #{name} at #{@host}:#{@port}." }
end

Instance Attribute Details

#nameObject (readonly)

The service name.



10
11
12
# File 'lib/caruby/database/persistence_service.rb', line 10

def name
  @name
end

#timerObject (readonly)

The Stopwatch which captures the time spent in database operations performed by the application service.



13
14
15
# File 'lib/caruby/database/persistence_service.rb', line 13

def timer
  @timer
end

Instance Method Details

#app_serviceObject

Returns the ApplicationService remote instance.

Returns:

  • the CaCORE service provider wrapped by this PersistenceService



72
73
74
# File 'lib/caruby/database/persistence_service.rb', line 72

def app_service
  ApplicationService.for(@url)
end

#create(obj) ⇒ Object

Submits the create to the application service and returns the created object.



52
53
54
55
# File 'lib/caruby/database/persistence_service.rb', line 52

def create(obj)
  logger.debug { "Submitting create #{obj.pp_s(:single_line)} to application service #{name}..." }
  dispatch { |svc| svc.create_object(obj) }
end

#delete(obj) ⇒ Object

Submits the delete to the application service.



64
65
66
67
# File 'lib/caruby/database/persistence_service.rb', line 64

def delete(obj)
  logger.debug { 'Deleting #{obj}.' }
  dispatch { |svc| svc.remove_object(obj) }
end

#query(template_or_hql, *path) ⇒ Object

Returns an array of objects fetched from the database which match the given template_or_hql.

If template_or_hql is a String, then the HQL is submitted to the service.

Otherwise, the template_or_hql is a query template domain object following the given attribute path. The query condition is determined by the values set in the template. Every non-nil attribute in the template is used as a select condition.



44
45
46
# File 'lib/caruby/database/persistence_service.rb', line 44

def query(template_or_hql, *path)
  String === template_or_hql ? query_hql(template_or_hql) : query_template(template_or_hql, path)
end

#update(obj) ⇒ Object

Submits the update to the application service and returns obj.



58
59
60
61
# File 'lib/caruby/database/persistence_service.rb', line 58

def update(obj)
   logger.debug { "Submitting update #{obj.pp_s(:single_line)} to application service #{name}..." }
   dispatch { |svc| svc.update_object(obj) }
end