module LittleWeasel::Metadata::Metadatable

This module defines methods to support objects that manage other objects that manage metadata related to a dictionary/ies.

Attributes

metadata[R]

rubocop: enable Lint/UnusedMethodArgument

Public Class Methods

included(base) click to toggle source
# File lib/LittleWeasel/metadata/metadatable.rb, line 11
def self.included(base)
  base.extend ClassMethods
end

Public Instance Methods

init(params: nil) click to toggle source

This method should UNCONDITIONALLY update the local metadata, using the metadata_key and notify all observers (if any) to initialize themselves as well.

This method should be chainable (return self).

@example

. # Example of a root-level dictionary metadata object (e.g. . # Metadata::DictionaryMetadata)

def init(_params: nil)
  self.metadata = {}
  notify action: :init
  refresh_local_metadata
  unless count_observers.zero? || metadata.present?
    raise 'Observers were called to #init but the dictionary cache metadata was not initialized'
  end

  self
end

. # Example of a metadata observable object (e.g. . # Metadata::InvalidWords::InvalidWordsMetadata)

def init(params: nil)
  self.metadata = Services::InvalidWordsService.new(dictionary_words).execute
  self
end

rubocop: disable Lint/UnusedMethodArgument

# File lib/LittleWeasel/metadata/metadatable.rb, line 57
def init(params: nil)
  raise Errors::MustOverrideError
end
metadata_key() click to toggle source
# File lib/LittleWeasel/metadata/metadatable.rb, line 24
def metadata_key
  self.class.metadata_key
end
refresh(params: nil) click to toggle source

This method should refresh the local metadata from the dictionary cache, if metadata exists in the dictionary cache for the given metatata_key. Otherwise, init should be called to initialize this object. The idea is that metadata should be shared across metadata objects of the same type that use the same metadata_key.

This method should be chainable (return self).

@example

def refresh(params: nil)
  refresh_local_metadata
  init unless metadata.present?
  self
end

rubocop: disable Lint/UnusedMethodArgument

# File lib/LittleWeasel/metadata/metadatable.rb, line 78
def refresh(params: nil)
  raise Errors::MustOverrideError
end

Private Instance Methods

metadata=(value) click to toggle source

This method should set the metadata in the dictionary cache, using the appropriate metadata key for this object (or nil if the root metadata object) AND set the @metadata local attribute so that a local copy is available for use.

When instantiating this object, init (see init comments). If an updated copy of the metadata is needed refresh (see comments) should be called.

@example

def metadata=(value)
  dictionary_cache_service = LittleWeasel::Services::DictionaryCacheService.new(
    dictionary_key: dictionary_key, dictionary_cache: dictionary_cache)
  dictionary_cache_service.dictionary_metadata_set(
    metadata_key: METADATA_KEY, value: value)
  @metadata = value
end
# File lib/LittleWeasel/metadata/metadatable.rb, line 105
def metadata=(value)
  @metadata = value
  update_dictionary_metadata value: value
end
refresh_local_metadata() click to toggle source

This method updates the local metadata ONLY. Use this method if you need to update the local metadata from the dictionary cache metadata.

Override this method in metadata observable classes as needed.

# File lib/LittleWeasel/metadata/metadatable.rb, line 114
def refresh_local_metadata
  @metadata = dictionary_metadata_service.dictionary_metadata
end
update_dictionary_metadata(value:) click to toggle source

This method should update the dictionary metadata for the the object when it is called.

@example

def update_dictionary_metadata(value:)
  dictionary_cache_service = LittleWeasel::Services::DictionaryCacheService.new(
    dictionary_key: dictionary_key, dictionary_cache: dictionary_cache)
  dictionary_cache_service.dictionary_metadata_set(
    metadata_key: metadata_key, value: value)
end

rubocop: disable Lint/UnusedMethodArgument

# File lib/LittleWeasel/metadata/metadatable.rb, line 130
def update_dictionary_metadata(value:)
  raise Errors::MustOverrideError
end