Class: LittleWeasel::Services::DictionaryMetadataService

Inherits:
Object
  • Object
show all
Includes:
Modules::DictionaryCacheServicable, Modules::DictionaryKeyable, Modules::DictionaryMetadataValidatable
Defined in:
lib/LittleWeasel/services/dictionary_metadata_service.rb

Overview

This class provides methods for managing and manipulating the dictionary metadata associated with the given dictionary, (dictionary_key) for the supplied metadata_key.

Instance Attribute Summary collapse

Attributes included from Modules::DictionaryCacheServicable

#dictionary_cache, #dictionary_key

Attributes included from Modules::DictionaryKeyable

#dictionary_key

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Modules::DictionaryMetadataValidatable

#validate_dictionary_metadata, validate_dictionary_metadata

Methods included from Modules::DictionaryCacheServicable

#dictionary_cache_service

Methods included from Modules::DictionaryCacheValidatable

#validate_dictionary_cache, validate_dictionary_cache

Methods included from Modules::DictionaryKeyValidatable

#validate_dictionary_key, validate_dictionary_key

Constructor Details

#initialize(dictionary_key:, dictionary_cache:, dictionary_metadata:) ⇒ DictionaryMetadataService

Returns a new instance of DictionaryMetadataService.

Examples:

metadata Hash structure:


{
  <dictionary_id!> =>
    {
      :<metadata_key> => <metadata_object>
    },
    ...
  }
}


29
30
31
32
33
34
35
36
37
38
# File 'lib/LittleWeasel/services/dictionary_metadata_service.rb', line 29

def initialize(dictionary_key:, dictionary_cache:, dictionary_metadata:)
  validate_dictionary_key dictionary_key: dictionary_key
  self.dictionary_key = dictionary_key

  validate_dictionary_cache dictionary_cache: dictionary_cache
  self.dictionary_cache = dictionary_cache

   dictionary_metadata: 
  self. = 
end

Instance Attribute Details

#dictionary_metadataObject

Returns the value of attribute dictionary_metadata.



17
18
19
# File 'lib/LittleWeasel/services/dictionary_metadata_service.rb', line 17

def 
  @dictionary_metadata
end

Class Method Details

.init(dictionary_metadata:) ⇒ Object

This method initializes the dictionary_metadata object to its initialized state - all data is lost, but the object reference is maintained.



44
45
46
47
48
49
50
# File 'lib/LittleWeasel/services/dictionary_metadata_service.rb', line 44

def init(dictionary_metadata:)
  Modules::DictionaryMetadataValidatable. \
    dictionary_metadata: 

  .each_key { |key| .delete(key) }
  
end

.init?(dictionary_metadata:) ⇒ Boolean Also known as: initialized?

Returns true if the dictionary metadata is initialized; that is, if it’s in the same state the dictionary metadata would be in if #init were called.

Returns:

  • (Boolean)


55
56
57
58
59
60
61
# File 'lib/LittleWeasel/services/dictionary_metadata_service.rb', line 55

def init?(dictionary_metadata:)
  Modules::DictionaryMetadataValidatable. \
    dictionary_metadata: 

   = init(dictionary_metadata: {})
  .eql?()
end

Instance Method Details

#dictionary_idObject (private)



107
108
109
# File 'lib/LittleWeasel/services/dictionary_metadata_service.rb', line 107

def dictionary_id
  dictionary_cache_service.dictionary_id
end

#dictionary_id!Object (private)



111
112
113
# File 'lib/LittleWeasel/services/dictionary_metadata_service.rb', line 111

def dictionary_id!
  dictionary_cache_service.dictionary_id!
end

#dictionary_metadata?(metadata_key:) ⇒ Boolean

This method will return true if metadata exists for the dictionary associated with the given dictionary key, for the given metadata key.

Returns:

  • (Boolean)


77
78
79
# File 'lib/LittleWeasel/services/dictionary_metadata_service.rb', line 77

def dictionary_metadata?(metadata_key:)
  .dig(dictionary_id, )&.present? || false
end

#dictionary_metadata_init_ifObject (private)

This method initializes the metadata for the dictionary_id! if it is not already initialized. The metadata for the given dictionary_id! is returned.



101
102
103
104
105
# File 'lib/LittleWeasel/services/dictionary_metadata_service.rb', line 101

def 
  return [dictionary_id!] unless 

  [dictionary_id!] = {}
end

#dictionary_metadata_init_needed?Boolean (private)

Returns:

  • (Boolean)


94
95
96
# File 'lib/LittleWeasel/services/dictionary_metadata_service.rb', line 94

def 
  [dictionary_id!].blank?
end

#get_dictionary_metadata(metadata_key:) ⇒ Object



81
82
83
# File 'lib/LittleWeasel/services/dictionary_metadata_service.rb', line 81

def (metadata_key:)
  .dig(dictionary_id!, )
end

#init(metadata_key:) ⇒ Object

This method initializes the dictionary metadata for dictionary metadata associated with the dictionary_id! and metadata_key.



67
68
69
70
71
72
73
# File 'lib/LittleWeasel/services/dictionary_metadata_service.rb', line 67

def init(metadata_key:)
   = [dictionary_id!]
  &.delete()
   = 
  [] = nil
  self
end

#set_dictionary_metadata(value:, metadata_key:) ⇒ Object



85
86
87
88
# File 'lib/LittleWeasel/services/dictionary_metadata_service.rb', line 85

def (value:, metadata_key:)
  [dictionary_id!][] = value
  self
end