Class: CFA::BaseModel
- Inherits:
-
Object
- Object
- CFA::BaseModel
- 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 Attribute Summary collapse
-
.default_file_handler ⇒ Object
Gets default file handler used when nil passed as file_handler in constructor.
Class Method Summary collapse
-
.attributes(attrs) ⇒ Object
Generates accessors for trivial key-value attributes.
Instance Method Summary collapse
-
#generic_get(key, tree = data) ⇒ Object
powerfull method that gets unformatted any value in config.
-
#generic_set(key, value, tree = data) ⇒ Object
powerfull method that sets any value in config.
-
#initialize(parser, file_path, file_handler: nil) ⇒ BaseModel
constructor
A new instance of BaseModel.
-
#load ⇒ void
Reads a String using file_handler and parses it with parser, storing the result in data.
-
#loaded? ⇒ Boolean
Returns if configuration was already loaded.
-
#save ⇒ void
Serializes data using parser and writes the resulting String using file_handler.
Constructor Details
#initialize(parser, file_path, file_handler: nil) ⇒ BaseModel
Returns a new instance of BaseModel.
25 26 27 28 29 30 31 |
# File 'lib/cfa/base_model.rb', line 25 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 Attribute Details
.default_file_handler ⇒ Object
Gets default file handler used when nil passed as file_handler in constructor
83 84 85 |
# File 'lib/cfa/base_model.rb', line 83 def self.default_file_handler @default_file_handler ||= File end |
Class Method Details
.attributes(attrs) ⇒ Object
Generates accessors for trivial key-value attributes
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/cfa/base_model.rb', line 106 def self.attributes(attrs) attrs.each_pair do |method_name, key| define_method(method_name) do tree_value_plain(generic_get(key)) end define_method(:"#{method_name.to_s}=") do |value| tree_value_change(key, value) end end end |
Instance Method Details
#generic_get(key, tree = data) ⇒ Object
prefer to use specialized methods of children
powerfull method that gets unformatted any value in config.
72 73 74 |
# File 'lib/cfa/base_model.rb', line 72 def generic_get(key, tree = data) tree[key] end |
#generic_set(key, value, tree = data) ⇒ Object
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
65 66 67 68 |
# File 'lib/cfa/base_model.rb', line 65 def generic_set(key, value, tree = data) modify(key, value, tree) || uncomment(key, value, tree) || add_new(key, value, tree) end |
#load ⇒ void
This method returns an undefined value.
Reads a String using file_handler and parses it with parser, storing the result in data.
55 56 57 58 59 |
# File 'lib/cfa/base_model.rb', line 55 def load @parser.file_name = @file_path if @parser.respond_to?(:file_name=) self.data = @parser.parse(@file_handler.read(@file_path)) @loaded = true end |
#loaded? ⇒ Boolean
Returns if configuration was already loaded
77 78 79 |
# File 'lib/cfa/base_model.rb', line 77 def loaded? @loaded end |
#save ⇒ void
This method returns an undefined value.
Serializes data using parser and writes the resulting String using file_handler.
42 43 44 45 |
# File 'lib/cfa/base_model.rb', line 42 def save @parser.file_name = @file_path if @parser.respond_to?(:file_name=) @file_handler.write(@file_path, @parser.serialize(data)) end |