Class: Twterm::AbstractPersistableConfiguration Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/twterm/abstract_persistable_configuration.rb

Overview

This class is abstract.

Direct Known Subclasses

Preferences

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration = {}) ⇒ AbstractPersistableConfiguration

Returns a new instance of AbstractPersistableConfiguration.

Parameters:

  • configuration (Hash<Symbol, Object>) (defaults to: {})

    A hash object to initialize configuration



5
6
7
# File 'lib/twterm/abstract_persistable_configuration.rb', line 5

def initialize(configuration = {})
  @configuration = configuration
end

Class Method Details

.defaultObject

This method is abstract.

Returns the default instance for the configuration.

Raises:

  • (NotImplementedError)


23
24
25
# File 'lib/twterm/abstract_persistable_configuration.rb', line 23

def self.default
  raise NotImplementedError, '`.default` method must be implemented'
end

.has_same_structure?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/twterm/abstract_persistable_configuration.rb', line 28

def self.has_same_structure?
  true
end

.structureHash

This method is abstract.

A tree-like object representing the structure of the configuration. Each node must respond to #=== method which determines whether a value is acceptable.

Returns:

  • (Hash)

    A tree-like hash object

Raises:

  • (NotImplementedError)


58
59
60
# File 'lib/twterm/abstract_persistable_configuration.rb', line 58

def self.structure
  raise NotImplementedError, '`.structure` method must be implemented'
end

Instance Method Details

#[]Object

This method is abstract.

Gets a value associated to the given key.

Raises:

  • (NotImplementedError)


11
12
13
# File 'lib/twterm/abstract_persistable_configuration.rb', line 11

def [](*)
  raise NotImplementedError, '`#[]` method must be implemented'
end

#[]=Object

This method is abstract.

Sets the given value to the given key.

Raises:

  • (NotImplementedError)


17
18
19
# File 'lib/twterm/abstract_persistable_configuration.rb', line 17

def []=(*)
  raise NotImplementedError, '`#[]=` method must be implemented'
end

#complete_missing_items!self

Returns:

  • (self)


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/twterm/abstract_persistable_configuration.rb', line 33

def complete_missing_items!
  traverse(self.class.default.to_h) do |path, default_value|
    value = dig(configuration, path)

    if value.nil? || !(dig!(self.class.structure, path) === value)
      bury!(path, default_value)
    end
  end

  self
end

#to_hHash

This method is abstract.

Converts to a hash object.

Returns:

  • (Hash)

    Configuration as a hash

Raises:

  • (NotImplementedError)


49
50
51
# File 'lib/twterm/abstract_persistable_configuration.rb', line 49

def to_h
  raise NotImplementedError, '`#to_h` method must be implemented'
end