Class: FormDurations::Worker

Inherits:
Object
  • Object
show all
Defined in:
app/services/form_durations/worker.rb

Overview

A service object for coordinating the efforts to determine an expiration duration for a given form. The Object goes about accomplishing this in a declarative manner via its public interface and hides the implementation details in a few private methods

Constant Summary collapse

REGEXP_ID_MATCHER =

The Regex matcher for getting the form name from a form_id, including custom form_ids. Example: Get HC-QSTNR from the form_id HC-QSTNR_123abc

/^[^_]*/
STANDARD_DURATION_NAME =

The default Registry key to use if none of the given form_ids match

'standard'
REGISTRY =

A Registry of UI Forms and their configuration key/values The Worker class bears the responsibility for maintaining the Hash

{
  'standard' => { klazz: StandardDuration, static: true },
  '21-526ez' => { klazz: AllClaimsDuration, static: true },
  'hc-qstnr' => { klazz: CustomDuration, static: false }
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(form_id: nil, days_till_expires: nil) ⇒ Worker

Returns a new instance of Worker.



50
51
52
53
54
55
# File 'app/services/form_durations/worker.rb', line 50

def initialize(form_id: nil, days_till_expires: nil)
  @form_id = form_id
  @days_till_expires = days_till_expires
  @config = build_duration_config
  @duration_instance = build_duration_instance
end

Instance Attribute Details

#configOpenStruct

Returns:

  • (OpenStruct)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/services/form_durations/worker.rb', line 17

class Worker
  ##
  # The Regex matcher for getting the form name from a form_id,
  # including custom form_ids. Example: Get HC-QSTNR from the
  # form_id HC-QSTNR_123abc
  #
  REGEXP_ID_MATCHER = /^[^_]*/
  ##
  # The default Registry key to use if none of the given form_ids match
  #
  STANDARD_DURATION_NAME = 'standard'
  ##
  # A Registry of UI Forms and their configuration key/values
  # The Worker class bears the responsibility for maintaining the Hash
  #
  REGISTRY = {
    'standard' => { klazz: StandardDuration, static: true },
    '21-526ez' => { klazz: AllClaimsDuration, static: true },
    'hc-qstnr' => { klazz: CustomDuration, static: false }
  }.freeze

  attr_reader :form_id, :days_till_expires, :config, :duration_instance

  ##
  # Builds a FormDurations::Worker instance from given options
  #
  # @param opts [Hash] a set of key value pairs.
  # @return [FormDurations::Worker] an instance of this class
  #
  def self.build(opts = {})
    new(**opts)
  end

  def initialize(form_id: nil, days_till_expires: nil)
    @form_id = form_id
    @days_till_expires = days_till_expires
    @config = build_duration_config
    @duration_instance = build_duration_instance
  end

  ##
  # Gets the expiration duration for a given form
  #
  # @return [ActiveSupport::Duration] an instance of ActiveSupport::Duration
  #
  def get_duration
    duration_instance.span
  end

  ##
  # List of imperative methods that hide the Worker classes implementation details
  # These could potentially be moved into their own Class in the future if necessary
  #
  private

  def build_duration_instance
    if static_duration?
      config.klazz.build
    else
      config.klazz.build(normalized_days_till_expires)
    end
  end

  def static_duration?
    config.static == true
  end

  def normalized_days_till_expires
    @normalized_days_till_expires ||= days_till_expires.to_s.to_i
  end

  def form_name
    normalized = form_id.to_s.downcase.match(REGEXP_ID_MATCHER)[0]

    return STANDARD_DURATION_NAME unless REGISTRY.key?(normalized)

    normalized
  end

  def build_duration_config
    OpenStruct.new(REGISTRY.fetch(form_name))
  end
end

#days_till_expiresActiveSupport::Duration

Returns:

  • (ActiveSupport::Duration)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/services/form_durations/worker.rb', line 17

class Worker
  ##
  # The Regex matcher for getting the form name from a form_id,
  # including custom form_ids. Example: Get HC-QSTNR from the
  # form_id HC-QSTNR_123abc
  #
  REGEXP_ID_MATCHER = /^[^_]*/
  ##
  # The default Registry key to use if none of the given form_ids match
  #
  STANDARD_DURATION_NAME = 'standard'
  ##
  # A Registry of UI Forms and their configuration key/values
  # The Worker class bears the responsibility for maintaining the Hash
  #
  REGISTRY = {
    'standard' => { klazz: StandardDuration, static: true },
    '21-526ez' => { klazz: AllClaimsDuration, static: true },
    'hc-qstnr' => { klazz: CustomDuration, static: false }
  }.freeze

  attr_reader :form_id, :days_till_expires, :config, :duration_instance

  ##
  # Builds a FormDurations::Worker instance from given options
  #
  # @param opts [Hash] a set of key value pairs.
  # @return [FormDurations::Worker] an instance of this class
  #
  def self.build(opts = {})
    new(**opts)
  end

  def initialize(form_id: nil, days_till_expires: nil)
    @form_id = form_id
    @days_till_expires = days_till_expires
    @config = build_duration_config
    @duration_instance = build_duration_instance
  end

  ##
  # Gets the expiration duration for a given form
  #
  # @return [ActiveSupport::Duration] an instance of ActiveSupport::Duration
  #
  def get_duration
    duration_instance.span
  end

  ##
  # List of imperative methods that hide the Worker classes implementation details
  # These could potentially be moved into their own Class in the future if necessary
  #
  private

  def build_duration_instance
    if static_duration?
      config.klazz.build
    else
      config.klazz.build(normalized_days_till_expires)
    end
  end

  def static_duration?
    config.static == true
  end

  def normalized_days_till_expires
    @normalized_days_till_expires ||= days_till_expires.to_s.to_i
  end

  def form_name
    normalized = form_id.to_s.downcase.match(REGEXP_ID_MATCHER)[0]

    return STANDARD_DURATION_NAME unless REGISTRY.key?(normalized)

    normalized
  end

  def build_duration_config
    OpenStruct.new(REGISTRY.fetch(form_name))
  end
end

#duration_instanceObject

Returns a matching duration object.

Returns:

  • a matching duration object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/services/form_durations/worker.rb', line 17

class Worker
  ##
  # The Regex matcher for getting the form name from a form_id,
  # including custom form_ids. Example: Get HC-QSTNR from the
  # form_id HC-QSTNR_123abc
  #
  REGEXP_ID_MATCHER = /^[^_]*/
  ##
  # The default Registry key to use if none of the given form_ids match
  #
  STANDARD_DURATION_NAME = 'standard'
  ##
  # A Registry of UI Forms and their configuration key/values
  # The Worker class bears the responsibility for maintaining the Hash
  #
  REGISTRY = {
    'standard' => { klazz: StandardDuration, static: true },
    '21-526ez' => { klazz: AllClaimsDuration, static: true },
    'hc-qstnr' => { klazz: CustomDuration, static: false }
  }.freeze

  attr_reader :form_id, :days_till_expires, :config, :duration_instance

  ##
  # Builds a FormDurations::Worker instance from given options
  #
  # @param opts [Hash] a set of key value pairs.
  # @return [FormDurations::Worker] an instance of this class
  #
  def self.build(opts = {})
    new(**opts)
  end

  def initialize(form_id: nil, days_till_expires: nil)
    @form_id = form_id
    @days_till_expires = days_till_expires
    @config = build_duration_config
    @duration_instance = build_duration_instance
  end

  ##
  # Gets the expiration duration for a given form
  #
  # @return [ActiveSupport::Duration] an instance of ActiveSupport::Duration
  #
  def get_duration
    duration_instance.span
  end

  ##
  # List of imperative methods that hide the Worker classes implementation details
  # These could potentially be moved into their own Class in the future if necessary
  #
  private

  def build_duration_instance
    if static_duration?
      config.klazz.build
    else
      config.klazz.build(normalized_days_till_expires)
    end
  end

  def static_duration?
    config.static == true
  end

  def normalized_days_till_expires
    @normalized_days_till_expires ||= days_till_expires.to_s.to_i
  end

  def form_name
    normalized = form_id.to_s.downcase.match(REGEXP_ID_MATCHER)[0]

    return STANDARD_DURATION_NAME unless REGISTRY.key?(normalized)

    normalized
  end

  def build_duration_config
    OpenStruct.new(REGISTRY.fetch(form_name))
  end
end

#form_idString

Returns:

  • (String)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/services/form_durations/worker.rb', line 17

class Worker
  ##
  # The Regex matcher for getting the form name from a form_id,
  # including custom form_ids. Example: Get HC-QSTNR from the
  # form_id HC-QSTNR_123abc
  #
  REGEXP_ID_MATCHER = /^[^_]*/
  ##
  # The default Registry key to use if none of the given form_ids match
  #
  STANDARD_DURATION_NAME = 'standard'
  ##
  # A Registry of UI Forms and their configuration key/values
  # The Worker class bears the responsibility for maintaining the Hash
  #
  REGISTRY = {
    'standard' => { klazz: StandardDuration, static: true },
    '21-526ez' => { klazz: AllClaimsDuration, static: true },
    'hc-qstnr' => { klazz: CustomDuration, static: false }
  }.freeze

  attr_reader :form_id, :days_till_expires, :config, :duration_instance

  ##
  # Builds a FormDurations::Worker instance from given options
  #
  # @param opts [Hash] a set of key value pairs.
  # @return [FormDurations::Worker] an instance of this class
  #
  def self.build(opts = {})
    new(**opts)
  end

  def initialize(form_id: nil, days_till_expires: nil)
    @form_id = form_id
    @days_till_expires = days_till_expires
    @config = build_duration_config
    @duration_instance = build_duration_instance
  end

  ##
  # Gets the expiration duration for a given form
  #
  # @return [ActiveSupport::Duration] an instance of ActiveSupport::Duration
  #
  def get_duration
    duration_instance.span
  end

  ##
  # List of imperative methods that hide the Worker classes implementation details
  # These could potentially be moved into their own Class in the future if necessary
  #
  private

  def build_duration_instance
    if static_duration?
      config.klazz.build
    else
      config.klazz.build(normalized_days_till_expires)
    end
  end

  def static_duration?
    config.static == true
  end

  def normalized_days_till_expires
    @normalized_days_till_expires ||= days_till_expires.to_s.to_i
  end

  def form_name
    normalized = form_id.to_s.downcase.match(REGEXP_ID_MATCHER)[0]

    return STANDARD_DURATION_NAME unless REGISTRY.key?(normalized)

    normalized
  end

  def build_duration_config
    OpenStruct.new(REGISTRY.fetch(form_name))
  end
end

Class Method Details

.build(opts = {}) ⇒ FormDurations::Worker

Builds a FormDurations::Worker instance from given options

Parameters:

  • opts (Hash) (defaults to: {})

    a set of key value pairs.

Returns:



46
47
48
# File 'app/services/form_durations/worker.rb', line 46

def self.build(opts = {})
  new(**opts)
end

Instance Method Details

#build_duration_configObject (private)



96
97
98
# File 'app/services/form_durations/worker.rb', line 96

def build_duration_config
  OpenStruct.new(REGISTRY.fetch(form_name))
end

#build_duration_instanceObject (private)



72
73
74
75
76
77
78
# File 'app/services/form_durations/worker.rb', line 72

def build_duration_instance
  if static_duration?
    config.klazz.build
  else
    config.klazz.build(normalized_days_till_expires)
  end
end

#form_nameObject (private)



88
89
90
91
92
93
94
# File 'app/services/form_durations/worker.rb', line 88

def form_name
  normalized = form_id.to_s.downcase.match(REGEXP_ID_MATCHER)[0]

  return STANDARD_DURATION_NAME unless REGISTRY.key?(normalized)

  normalized
end

#get_durationActiveSupport::Duration

Gets the expiration duration for a given form

Returns:

  • (ActiveSupport::Duration)

    an instance of ActiveSupport::Duration



62
63
64
# File 'app/services/form_durations/worker.rb', line 62

def get_duration
  duration_instance.span
end

#normalized_days_till_expiresObject (private)



84
85
86
# File 'app/services/form_durations/worker.rb', line 84

def normalized_days_till_expires
  @normalized_days_till_expires ||= days_till_expires.to_s.to_i
end

#static_duration?Boolean (private)

Returns:

  • (Boolean)


80
81
82
# File 'app/services/form_durations/worker.rb', line 80

def static_duration?
  config.static == true
end