Class: ActiveValidation::Internal::Models::Manifest

Inherits:
Object
  • Object
show all
Defined in:
lib/active_validation/internal/models/manifest.rb,
lib/active_validation/internal/models/manifest/installer.rb

Defined Under Namespace

Classes: Installer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version:, base_klass:, checks: [], options: {}, **other) ⇒ Manifest

Returns a new instance of Manifest.

Examples:

Add new manifest:

new({  name: 'Cool Manifest',
   version: 42,
   base_klass: 'Bar',
   checks: [
      { method_name: "validates", argument: "name", options: { presence: true } }
   ]})

Parameters:

  • Manifest (Hash)

    options hash

  • manifest_hash (Hash)

    a customizable set of options



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/active_validation/internal/models/manifest.rb', line 28

def initialize(version:, base_klass:, checks: [], options: {}, **other)
  @version = ActiveValidation::Values::Version.new version
  @base_klass = base_klass.to_s
  @checks = Array(checks).map(&:to_internal_check)
  @other = ActiveSupport::OrderedOptions.new other

  @id = other[:id]
  @name = other[:name]
  @created_at = other[:created_at]
  @options = options.to_h.to_options!
end

Instance Attribute Details

#base_klassObject (readonly)

Returns the value of attribute base_klass.



9
10
11
# File 'lib/active_validation/internal/models/manifest.rb', line 9

def base_klass
  @base_klass
end

#checksObject (readonly)

Returns the value of attribute checks.



9
10
11
# File 'lib/active_validation/internal/models/manifest.rb', line 9

def checks
  @checks
end

#created_atObject (readonly)

Returns the value of attribute created_at.



9
10
11
# File 'lib/active_validation/internal/models/manifest.rb', line 9

def created_at
  @created_at
end

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/active_validation/internal/models/manifest.rb', line 9

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/active_validation/internal/models/manifest.rb', line 9

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/active_validation/internal/models/manifest.rb', line 9

def options
  @options
end

#otherObject (readonly)

Returns the value of attribute other.



9
10
11
# File 'lib/active_validation/internal/models/manifest.rb', line 9

def other
  @other
end

#versionObject (readonly)

Returns the value of attribute version.



9
10
11
# File 'lib/active_validation/internal/models/manifest.rb', line 9

def version
  @version
end

Instance Method Details

#==(other) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/active_validation/internal/models/manifest.rb', line 40

def ==(other)
  return true if id == other.id

  version == other.version &&
    base_klass == other.base_klass &&
    options     == other.options &&
    checks      == other.checks
end

#as_json(only: %i[version base_klass checks name id],, **options) ⇒ Hash

ActiveSupport#as_json interface Supported options:

:only [Array<Symbol>, Symbol] select only listed elements

Nested elements accept options:

:only [Array<Symbol>, Symbol] select only listed elements
:as   [Symbol, String] in place rename for the column

Examples:

as_json(only: :version, checks: { as: :checks_attributes,
                                  only: [:argument] })

Returns:

  • (Hash)


89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/active_validation/internal/models/manifest.rb', line 89

def as_json(only: %i[version base_klass checks name id], **options)
  only = Array(only)
  {}.tap do |acc|
    options.each_pair do |k, v|
      only.delete(k)
      as = v.delete(:as)
      key_name = (as || k).to_sym
      acc[key_name] = public_send(k).as_json(v)
    end
    only.each { |el| acc[el.to_sym] = public_send(el).as_json }
  end
end

#base_classObject



49
50
51
# File 'lib/active_validation/internal/models/manifest.rb', line 49

def base_class
  base_klass.constantize
end

#contextString

Formatted context, as it will be stored on ‘on` option with the validations

Returns:

  • (String)


110
111
112
# File 'lib/active_validation/internal/models/manifest.rb', line 110

def context
  @context ||= ActiveValidation.config.validation_context_formatter.call self
end

#installObject

rubocop:disable Naming/MemoizedInstanceVariableName



58
59
60
# File 'lib/active_validation/internal/models/manifest.rb', line 58

def install
  @installed ||= installer.install
end

#installed?TrueClass, FalseClass

Are the callbacks installed to the ‘base_class`?

Returns:

  • (TrueClass, FalseClass)


72
73
74
# File 'lib/active_validation/internal/models/manifest.rb', line 72

def installed?
  !!@installed
end

#to_hashObject



53
54
55
# File 'lib/active_validation/internal/models/manifest.rb', line 53

def to_hash
  as_json
end

#to_internal_manifestObject



102
103
104
# File 'lib/active_validation/internal/models/manifest.rb', line 102

def to_internal_manifest
  self
end

#uninstallObject

rubocop:enable Naming/MemoizedInstanceVariableName



63
64
65
66
67
# File 'lib/active_validation/internal/models/manifest.rb', line 63

def uninstall
  return false unless installed?

  @installed = installer.uninstall
end