Class: LittleWeasel::Metadata::DictionaryMetadata

Overview

This class manages metadata objects related to dictionaries. Metadata objects defined in LittleWeasel::Configuration#metadata_observers are added as observers, provided they are in a state to observe (see Metadata::Metadatable, Metadata::InvalidWords::InvalidWordsMetadata, etc.).

Constant Summary

Constants included from LittleWeasel::Modules::DictionaryCacheKeys

LittleWeasel::Modules::DictionaryCacheKeys::DICTIONARIES, LittleWeasel::Modules::DictionaryCacheKeys::DICTIONARY_CACHE, LittleWeasel::Modules::DictionaryCacheKeys::DICTIONARY_ID, LittleWeasel::Modules::DictionaryCacheKeys::DICTIONARY_OBJECT, LittleWeasel::Modules::DictionaryCacheKeys::DICTIONARY_REFERENCES, LittleWeasel::Modules::DictionaryCacheKeys::SOURCE

Instance Attribute Summary collapse

Attributes included from LittleWeasel::Modules::DictionaryMetadataServicable

#dictionary_cache, #dictionary_key, #dictionary_metadata

Attributes included from LittleWeasel::Modules::DictionaryKeyable

#dictionary_key

Attributes included from LittleWeasel::Modules::DictionaryCacheServicable

#dictionary_cache, #dictionary_key

Attributes included from Metadatable

#metadata

Instance Method Summary collapse

Methods included from LittleWeasel::Modules::DictionaryMetadataServicable

#dictionary_metadata_service

Methods included from LittleWeasel::Modules::DictionaryMetadataValidatable

#validate_dictionary_metadata, validate_dictionary_metadata

Methods included from LittleWeasel::Modules::DictionaryCacheValidatable

#validate_dictionary_cache, validate_dictionary_cache

Methods included from LittleWeasel::Modules::DictionaryKeyValidatable

#validate_dictionary_key, validate_dictionary_key

Methods included from LittleWeasel::Modules::DictionaryCacheServicable

#dictionary_cache_service

Methods included from LittleWeasel::Modules::DictionaryCacheKeys

#initialize_dictionary_cache, initialize_dictionary_cache, #initialized_dictionary_cache, initialized_dictionary_cache

Methods included from LittleWeasel::Modules::Configurable

#config, included

Methods included from LittleWeasel::Modules::ClassNameToSymbol

included, #to_sym

Methods included from MetadataObservableValidatable

#valid_metadata_observable?, #validate_metadata_observable

Methods included from Metadatable

included, #metadata_key, #refresh_local_metadata

Constructor Details

#initialize(dictionary_words:, dictionary_key:, dictionary_cache:, dictionary_metadata:) ⇒ DictionaryMetadata

Returns a new instance of DictionaryMetadata.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/LittleWeasel/metadata/dictionary_metadata.rb', line 34

def initialize(dictionary_words:, 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. = 

  unless dictionary_words.is_a? Hash
    raise ArgumentError,
      "Argument dictionary_words is not a Hash: #{dictionary_words.class.name}."
  end

  self.dictionary_words = dictionary_words
  self.observers = {}

  refresh
end

Instance Attribute Details

#dictionary_wordsObject

Returns the value of attribute dictionary_words.



32
33
34
# File 'lib/LittleWeasel/metadata/dictionary_metadata.rb', line 32

def dictionary_words
  @dictionary_words
end

#observersObject

Returns the value of attribute observers.



32
33
34
# File 'lib/LittleWeasel/metadata/dictionary_metadata.rb', line 32

def observers
  @observers
end

Instance Method Details

#add_observer(observer, func = :update) ⇒ Object



117
118
119
120
121
122
# File 'lib/LittleWeasel/metadata/dictionary_metadata.rb', line 117

def add_observer(observer, func = :update)
   observer

  super
  observers[observer.] = observer
end

#add_observers(force: false) {|observer_classes| ... } ⇒ Object

Yields:

  • (observer_classes)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/LittleWeasel/metadata/dictionary_metadata.rb', line 83

def add_observers(force: false)
  delete_observers if force

  raise 'Observers have already been added; use #add_observers(force: true) instead' if count_observers.positive?

  observer_classes = config.
  yield observer_classes if block_given?

  observer_classes.each do |o|
    # If the medatata observer is not in a state to observe,
    # or is turned "off", skip it...
    #
    # See Metadata::MetadataObserverable.observe? comments.
    next unless o.observe?

    # If this observer has already beed added, don't add it again.
    next if observers.key? o.

    observer = o.new(dictionary_metadata_object: self,
      dictionary_words: dictionary_words,
      dictionary_key: dictionary_key,
      dictionary_cache: dictionary_cache,
      dictionary_metadata: )

    # Only add metadata objects that are capable of observing
    # (i.e. #observe?).
    add_observer observer if observer.observe?
  end
  # This is how each metadata object gets initialized. Only notify if
  # there are any observers.
  notify(action: :init) if count_observers.positive?
  self
end

#delete_observer(observer) ⇒ Object



124
125
126
127
128
129
# File 'lib/LittleWeasel/metadata/dictionary_metadata.rb', line 124

def delete_observer(observer)
   observer

  super
  observers.delete(observer.)
end

#delete_observersObject



131
132
133
134
# File 'lib/LittleWeasel/metadata/dictionary_metadata.rb', line 131

def delete_observers
  super
  self.observers = {}
end

#init(_params: nil) ⇒ Object



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

def init(_params: nil)
  .init(metadata_key: )
  self. = {}
  notify action: :init
  
  self
end

#notify(action:, params: nil) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/LittleWeasel/metadata/dictionary_metadata.rb', line 75

def notify(action:, params: nil)
  if count_observers.positive?
    changed
    notify_observers action, params
  end
  self
end

#refresh(_params: nil) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/LittleWeasel/metadata/dictionary_metadata.rb', line 63

def refresh(_params: nil)
  
  if .present?
    # If there is metadata in the dictionary cache, notify the observers
    # to use it...
    notify action: :refresh
  else
    # ...otherwise, notify the observers to initialize themselves.
    init
  end
end

#update_dictionary_metadata(value:) ⇒ Object (private)



140
141
142
# File 'lib/LittleWeasel/metadata/dictionary_metadata.rb', line 140

def (value:)
  .(value: value, metadata_key: )
end