Class: CFA::BaseModel

Inherits:
Object
  • Object
show all
Defined in:
lib/cfa/base_model.rb

Overview

A base class for models. Represents a configuration file as an object with domain-specific attributes/methods. For persistent storage, use load and save, Non-responsibilities: actual storage and parsing (both delegated). There is no caching involved.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser, file_path, file_handler: nil) ⇒ BaseModel

Returns a new instance of BaseModel.

Parameters:

  • parser (.parse, .serialize, .empty)

    parser that can convert object to string and vice versa. It have to provide methods ‘string #serialize(object)`, `object #parse(string)` and `object #empty` For example see AugeasParser

  • file_path (String)

    expected path passed to file_handler

  • file_handler (.read, .write) (defaults to: nil)

    an object able to read/write a string. It has to provide methods ‘string read(string)` and `write(string, string)`. For an example see MemoryFile. If unspecified or nil, default_file_handler is asked.



17
18
19
20
21
22
23
# File 'lib/cfa/base_model.rb', line 17

def initialize(parser, file_path, file_handler: nil)
  @file_handler = file_handler || BaseModel.default_file_handler
  @parser = parser
  @file_path = file_path
  @loaded = false
  self.data = parser.empty
end

Class Method Details

.default_file_handlerObject

Gets default file handler used when nil passed as file_handler in constructor



57
58
59
# File 'lib/cfa/base_model.rb', line 57

def self.default_file_handler
  @default_file_handler ||= File
end

.default_file_handler=(value) ⇒ Object

Sets default file handler. Useful when needed to change default like if whole program use non standard file reading.

Parameters:

  • value

    for value specification see constructor



64
65
66
# File 'lib/cfa/base_model.rb', line 64

def self.default_file_handler=(value)
  @default_file_handler = value
end

Instance Method Details

#generic_get(key) ⇒ Object

Note:

prefer to use specialized methods of children

powerfull method that gets unformatted any value in config.



45
46
47
# File 'lib/cfa/base_model.rb', line 45

def generic_get(key)
  data[key]
end

#generic_set(key, value) ⇒ Object

Note:

prefer to use specialized methods of children

powerfull method that sets any value in config. It try to be smart to at first modify existing value, then replace commented out code and if even that doesn’t work, then append it at the end



39
40
41
# File 'lib/cfa/base_model.rb', line 39

def generic_set(key, value)
  modify(key, value) || uncomment(key, value) || add_new(key, value)
end

#loadObject



30
31
32
33
# File 'lib/cfa/base_model.rb', line 30

def load
  self.data = @parser.parse(@file_handler.read(@file_path))
  @loaded = true
end

#loaded?Boolean

rubocop:disable Style/TrivialAccessors Returns if configuration was already loaded

Returns:

  • (Boolean)


51
52
53
# File 'lib/cfa/base_model.rb', line 51

def loaded?
  @loaded
end

#save(changes_only: false) ⇒ Object



25
26
27
28
# File 'lib/cfa/base_model.rb', line 25

def save(changes_only: false)
  merge_changes if changes_only
  @file_handler.write(@file_path, @parser.serialize(data))
end