Module: DeclarativeEnum

Defined in:
lib/declarative_enum.rb

Overview

Extending this module will give you the ability of defining enum values in a declarative way.

module DismissalReasons
  extend DeclarativeEnum

  key  :dismissal_reason
  name 'DismissalReasonOfVulnerability'

  description <<~TEXT
    This enum holds the user selected dismissal reason
    when they are dismissing the vulnerabilities
  TEXT

  define do
    acceptable_risk value: 0, description: N_('The vulnerability is known but is considered to be an acceptable business risk.')
    false_positive value: 1, description: N_('An error in reporting the presence of a vulnerability in a system when the vulnerability is not present.')
    used_in_tests value: 2, description: N_('The finding is not a vulnerability because it is part of a test or is test data.')
  end

Then we can use this module to register enums for our Active Record models like so,

class VulnerabilityFeedback
  declarative_enum DismissalReasons
end

Also we can use this module to create GraphQL Enum types like so,

module Types

module Vulnerabilities
  class DismissalReasonEnum < BaseEnum
    declarative_enum DismissalReasons
  end
end

end

rubocop:disable Gitlab/ModuleWithInstanceVariables

Defined Under Namespace

Classes: Builder

Instance Method Summary collapse

Instance Method Details

#define(&block) ⇒ Object

Raises:

  • (LocalJumpError)

79
80
81
82
83
# File 'lib/declarative_enum.rb', line 79

def define(&block)
  raise LocalJumpError, 'No block given' unless block

  @definition = Builder.new(definition, block).build
end

#definitionObject

We can use this method later to apply some sanity checks but for now, returning a Hash without any check is enough.


87
88
89
# File 'lib/declarative_enum.rb', line 87

def definition
  @definition.to_h
end

#description(new_description = nil) ⇒ Object


60
61
62
63
64
# File 'lib/declarative_enum.rb', line 60

def description(new_description = nil)
  @description = new_description if new_description

  @description
end

#key(new_key = nil) ⇒ Object


48
49
50
51
52
# File 'lib/declarative_enum.rb', line 48

def key(new_key = nil)
  @key = new_key if new_key

  @key
end

#name(new_name = nil) ⇒ Object


54
55
56
57
58
# File 'lib/declarative_enum.rb', line 54

def name(new_name = nil)
  @name = new_name if new_name

  @name
end

#prepended(base) ⇒ Object

This `prepended` hook will merge the enum definition of the prepended module into the base module to be used by `prepend_mod_with` helper method.


44
45
46
# File 'lib/declarative_enum.rb', line 44

def prepended(base)
  base.definition.merge!(definition)
end

#translated_descriptionsObject

Return list of dynamically translated descriptions.

It is required to define descriptions with `N_(…)`.

See github.com/grosser/fast_gettext#n_-and-nn_-make-dynamic-translations-available-to-the-parser


75
76
77
# File 'lib/declarative_enum.rb', line 75

def translated_descriptions
  definition.transform_values { |definition| _(definition[:description]) }
end

#valuesObject


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

def values
  definition.transform_values { |definition| definition[:value] }
end