Class: LUSI::API::Core::Lookup::LookupService
- Inherits:
-
Object
- Object
- LUSI::API::Core::Lookup::LookupService
- Defined in:
- lib/lusi_api/core/lookup.rb
Overview
Collects a number of lookup tables into a single service
Constant Summary collapse
- SERVICES =
Configuration for lookup services The structure is: {
service: { available: include this service in the list of available services if true class: the LUSI API class returned by the lookup table (default: LUSI::API::Core::Code) create: create method name creates: the list of services created by this service depends: the list of services this service depends on load: load method name params: the LUSI API class #get_instance parameters }}
The available option, if specifies, dictates whether the service is included in the list of available services. The default is true.
The class option, if specified, is the Class instance of the object class stored in the lookup table. If no class is specified, the default is LUSI::API::Core::Code
The create option, if specified, is a symbol (method name on the LookupService instance) or callable. The create method should accept an arbitrary parameter list (method(*params)) and return a configured lookup table instance or functional equivalent. If no create method is specified, the default is to create a LookupTable instance as follows:
LUSILookupTable.new(@api, *params, loadable: true)where params is the list from the params option (see below)
The creates option, if specified, is a list of services created by this service. This allows a single service definition to create multiple related lookup services which can be iterated in the list of available services.
The depends option, if specified, specifies a list of lookup services on which the current lookup service depends. These services are automatically loaded before the current lookup service.
The load option, if specified, is a symbol (method name on the LookupService instance) or callable. The load method should accepts two parameters: If no load method is specified, the default is to call the lookup table’s load method.
The params option specifies the parameters to the create method or LookupTable constructor as a list:
[ lusi-api-url-path, lusi-api-endpoint, lusi-api-method, xml-root, result-class ] { absence_reason: { params: ['LUSIReference', 'Lookup.asmx', 'GetAbsenceReasons', 'xmlns:AbsenceReason'] }, address_country: { class: LUSI::API::Country::AddressCountry, }, event_contact_type: { params: ['LUSIReference', 'Lookup.asmx', 'GetEventContactTypes', 'xmlns:EventContactType'] }, leave_reason: { params: ['LUSIReference', 'Lookup.asmx', 'GetLeaveReasons', 'xmlns:LeaveReason'] }, location_of_tuition: { params: ['LUSIReference', 'Lookup.asmx', 'GetLocationsOfTuition', 'xmlns:LocationOfTuition'] }, nationality: { class: LUSI::API::Country::Nationality }, organisation: { available: false, create: :create_organisation, creates: [:department, :faculty, :institution] }, staff_course_role: { class: LUSI::API::Person::StaffCourseRole }, staff_relationship: { params: ['LUSIReference', 'Lookup.asmx', 'GetStaffRelationships', 'xmlns:StaffRelationship'] }, student_category: { params: ['LUSIReference', 'Lookup.asmx', 'GetStudentCategories', 'xmlns:StudentCategory'] }, subject_area: { params: ['LUSIReference', 'Lookup.asmx', 'GetSubjectAreas', 'xmlns:SubjectArea'] }, week: { class: LUSI::API::Calendar::Week, depends: [:year], load: :load_weeks }, year: { class: LUSI::API::Calendar::Year } }
- @@services =
The list of available services - built lazily
nil
Instance Method Summary collapse
-
#clear ⇒ Object
Clears all lookup tables from the LookupService instance.
-
#each {|service, lookup_table| ... } ⇒ Object
Iterates over each lookup table.
-
#has?(*services) ⇒ Boolean
Returns true if all specified services are defined Parameters are the service names to be checked.
-
#initialize(api = nil, **services) ⇒ void
constructor
Initialises a new LookupService instance.
-
#length(service = nil) ⇒ Integer
Returns the number of configured lookup tables.
-
#load(**services) ⇒ void
Loads specified lookup services, or all services if none are specified.
-
#lookup(service, key, default = nil) ⇒ Object
Fetches a key from the specified lookup table.
-
#service(service) ⇒ LUSI::API::Core::Lookup::LookupTable
Returns the specified lookup table.
-
#services(all = false) {|service| ... } ⇒ Object
Returns a list of configured services.
Constructor Details
#initialize(api = nil, **services) ⇒ void
Initialises a new LookupService instance
356 357 358 359 360 |
# File 'lib/lusi_api/core/lookup.rb', line 356 def initialize(api = nil, **services) @api = api clear load(**services) unless services.nil? || services.empty? end |
Instance Method Details
#clear ⇒ Object
Clears all lookup tables from the LookupService instance
363 364 365 366 |
# File 'lib/lusi_api/core/lookup.rb', line 363 def clear @lookups = {} @organisation = nil end |
#each {|service, lookup_table| ... } ⇒ Object
Iterates over each lookup table
373 374 375 |
# File 'lib/lusi_api/core/lookup.rb', line 373 def each @lookups.each { |service, lookup| yield(service, lookup) } end |
#has?(*services) ⇒ Boolean
Returns true if all specified services are defined Parameters are the service names to be checked
380 381 382 383 |
# File 'lib/lusi_api/core/lookup.rb', line 380 def has?(*services) services.each { |service| return false unless @lookups.include?(service) } true end |
#length(service = nil) ⇒ Integer
Returns the number of configured lookup tables
391 392 393 394 395 396 397 398 |
# File 'lib/lusi_api/core/lookup.rb', line 391 def length(service = nil) if service lookup = @lookups[service] lookup ? lookup.length : 0 else @lookups.length end end |
#load(**services) ⇒ void
This method returns an undefined value.
Loads specified lookup services, or all services if none are specified. Each named parameter specifies a configuration for the lookup service: service: config False parameter values cause that service to be ignored.
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
# File 'lib/lusi_api/core/lookup.rb', line 404 def load(**services) # If no services are specified, load all services with default configuration if services.nil? || services.empty? services = {} SERVICES.each_key { |key| services[key] = true } end # Create the services services.each do |service, config| # Ignore invalid and unconfigured services create_lookup_service(service, config, services) if config && SERVICES.has_key?(service) end end |
#lookup(service, key, default = nil) ⇒ Object
Fetches a key from the specified lookup table
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 |
# File 'lib/lusi_api/core/lookup.rb', line 425 def lookup(service, key, default = nil) lookup_method = get_callable(service, :lookup) if lookup_method lookup_method.call(service, key, default) else lookup = @lookups[service] if lookup.is_a?(Hash) lookup[key] elsif lookup.is_a?(Method) || lookup.is_a?(Proc) lookup.call(service, key, default) else default end end end |
#service(service) ⇒ LUSI::API::Core::Lookup::LookupTable
Returns the specified lookup table
444 445 446 |
# File 'lib/lusi_api/core/lookup.rb', line 444 def service(service) @lookups[service] end |
#services(all = false) {|service| ... } ⇒ Object
Returns a list of configured services
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 |
# File 'lib/lusi_api/core/lookup.rb', line 452 def services(all = false) if all # Return all services defined in SERVICES return @@services if @@services @@services = [] SERVICES.each do |service, config| if config.fetch(:available, true) @@services.push(service) yield(service) if block_given? end config.fetch(:creates, []).each do |created| @@services.push(created) yield(created) if block_given? end end @@services else # Return all services configured in @lookups result = @lookups.keys result.each { |service| yield(service) } if block_given? result end end |