Class: Qa::AuthorityWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/qa/authority_wrapper.rb

Overview

The intention of this wrapper is to provide a common interface that both linked and non-linked data can use. There are implementation differences between the two, but with this wrapper, the goal is to draw attention to those differences and insulate the end user from those issues.

One benefit in introducing this class is that when interacting with a questioning authority implementation you don’t need to consider “Hey when I instantiate an authority, is this linked data or not?” And what specifically are the parameter differences. You will need to perhaps include some additional values in the context if you don’t call this from a controller.

Since:

  • v5.11.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(authority:, subauthority:, context:) ⇒ AuthorityWrapper

Returns a new instance of AuthorityWrapper.

Parameters:

  • authority (#find, #search)
  • subauthority (#to_s)
  • context (#params, #search_header, #fetch_header)

Since:

  • v5.11.0



18
19
20
21
22
23
# File 'lib/qa/authority_wrapper.rb', line 18

def initialize(authority:, subauthority:, context:)
  @authority = authority
  @subauthority = subauthority
  @context = context
  configure!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object

Since:

  • v5.11.0



51
52
53
# File 'lib/qa/authority_wrapper.rb', line 51

def method_missing(method_name, *arguments, &block)
  authority.send(method_name, *arguments, &block)
end

Instance Attribute Details

#authorityObject (readonly)

Since:

  • v5.11.0



24
25
26
# File 'lib/qa/authority_wrapper.rb', line 24

def authority
  @authority
end

#contextObject (readonly)

Since:

  • v5.11.0



24
25
26
# File 'lib/qa/authority_wrapper.rb', line 24

def context
  @context
end

#subauthorityObject (readonly)

Since:

  • v5.11.0



24
25
26
# File 'lib/qa/authority_wrapper.rb', line 24

def subauthority
  @subauthority
end

Instance Method Details

#configure!Object

Since:

  • v5.11.0



59
60
61
# File 'lib/qa/authority_wrapper.rb', line 59

def configure!
  @context.subauthority = @subauthority if @context.respond_to?(:subauthority)
end

#find(value) ⇒ Object Also known as: fetch

context has params

Since:

  • v5.11.0



39
40
41
42
43
44
45
46
47
48
# File 'lib/qa/authority_wrapper.rb', line 39

def find(value)
  if linked_data?
    # should respond to fetch_header
    authority.find(value, request_header: context.fetch_header)
  elsif authority.method(:find).arity == 2
    authority.find(value, context)
  else
    authority.find(value)
  end
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)

Since:

  • v5.11.0



55
56
57
# File 'lib/qa/authority_wrapper.rb', line 55

def respond_to_missing?(method_name, include_private = false)
  authority.respond_to?(method_name, include_private)
end

#search(value) ⇒ Object

Since:

  • v5.11.0



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/qa/authority_wrapper.rb', line 26

def search(value)
  if linked_data?
    # should respond to search_header
    authority.search(value, request_header: context.search_header)
  elsif authority.method(:search).arity == 2
    # This context should respond to params; see lib/qa/authorities/discogs/generic_authority.rb
    authority.search(value, context)
  else
    authority.search(value)
  end
end