📜 ⬆️ ⬇️

Work with data in Yaml

The other day I solved the problem of storing data in a yaml file, with the ability for users to edit this data. Everything was a little complicated by the fact that users had different access rights. For each row that we store, information should be available on which users have access to edit this row.

Here is an example of such a file with settings:

--- api_url: value: example.com/api role: 4 api_login: value: user role: 4 api_password: value: 12355 role: 4 


In this case, we have three variables that store information about accessing the API. Each key is a hash to store the value of the variable and the minimum role of the user who has access to this data.
')
Now we will write a class that can read and edit this data, taking into account the user's rights in the system.

 require 'yaml' class Setting CONFIG_FILE = 'settings.yml' class << self def get(key) load_settings[key.to_s]['value'] end def update(params, role) settings = load_settings params.each do |key, value| key = key.to_s settings[key]['value'] = value.to_s if has_access?(role, key) end save_settings(settings) end def has_access?(role, key) return unless setting = load_settings[key] role >= setting['role'] end def load_settings @config ||= YAML.load(File.read(file_path)) end private def save_settings(settings) File.write(file_path, settings.to_yaml) end def file_path "config/#{CONFIG_FILE}" end end end 


Data acquisition by keys can be carried out as follows:
 Setting.get('api_login') Setting.get('api_password') … 


Data updating is carried out taking into account user rights, that is, no checks outside the class will be required. You can simply transfer the data hash and updates will be made according to the data that is available to the user for editing. This allows you to safely transfer all data entered by the user:
 Setting.update(params[:settings], current_user.role) 


We transfer the hash with the information that needs to be updated and the current user rights.

The method that stores the path to the file with the settings can be set more conveniently if you do this in Rails:
 Rails.root.join('config', CONFIG_FILE) 

Source: https://habr.com/ru/post/202588/


All Articles