lib/ssm_config.rb in ssm_config-0.1.1 vs lib/ssm_config.rb in ssm_config-1.1.0

- old
+ new

@@ -1,37 +1,56 @@ -class SsmConfig - VERSION = "0.1.1" +require './lib/ssm_config/ssm_storage/db.rb' +require './lib/ssm_config/ssm_storage/yml.rb' +require './lib/ssm_config/ssm_storage/empty.rb' +require 'active_support/core_ext/hash/indifferent_access' +require 'active_support/time' +require './lib/ssm_config/errors.rb' - class << self +module SsmConfig + VERSION = '1.1.0'.freeze + REFRESH_TIME = (30.minutes).freeze + class << self def method_missing(meth, *args, &block) - config_file = Rails.root.join('config', "#{meth}.yml") + result = populate(meth) + super if result == false + result + end - if File.exists?(config_file) - write_config_accessor_for(meth) - self.send(meth) - else - super - end + def respond_to_missing?(meth, *_args) + determine_query(meth).present? end + def last_processed_time + @last_processed_time ||= ActiveSupport::HashWithIndifferentAccess.new + end + private - def parse_config_file(filename) - YAML.load(ERB.new(File.read("#{Rails.root}/config/#{filename}")).result).symbolize_keys + def determine_query(meth) + query_database = SsmConfig::SsmStorage::Db.new(meth) + query_yml = SsmConfig::SsmStorage::Yml.new(meth) + return query_database if query_database.table_exists? + return query_yml if query_yml.file_exists? + nil end - def parse_config_file_with_env(filename) - yaml_loaded = YAML.load(ERB.new(File.read("#{Rails.root}/config/#{filename}")).result) - (yaml_loaded[Rails.env] || yaml_loaded['any']).try(:with_indifferent_access) + def populate(meth) + query = determine_query(meth) + + return false if query.blank? + self.last_processed_time[meth] = Time.zone.now + write_config_accessor_for(meth) unless method_defined?(meth.to_sym) + instance_variable_set("@#{meth}".to_sym, nil) + self.send(meth, query) end def write_config_accessor_for(meth) - self.instance_eval %Q{ - def #{meth} - @#{meth} ||= parse_config_file_with_env('#{meth}.yml') - end - } + self.instance_eval %{ + def #{meth}(obj = SsmConfig::SsmStorage::Empty.new) + return self.send(:populate, "#{meth}") if self.last_processed_time["#{meth}".to_sym] < Time.zone.now - REFRESH_TIME + @#{meth} ||= obj&.hash + end + }, __FILE__, __LINE__ - 5 end - end end