class JSONAPI::ConfigManager
Manages user configuration options
Public Class Methods
Config
Manager always has an internal global config
# File lib/easy/jsonapi/config_manager.rb, line 13 def initialize @class_type = JSONAPI::ConfigManager::Config @config_manager = { global: JSONAPI::ConfigManager::Config.new } end
Public Instance Methods
Add an config to the config_manager @param config [Object]
# File lib/easy/jsonapi/config_manager.rb, line 37 def add(res_name, config) raise "Cannot add a config that is not #{@class_type}" unless config.is_a? @class_type insert(res_name, config) end
@return [Array<Symbol>] The names of the resource types the configs belong to
# File lib/easy/jsonapi/config_manager.rb, line 93 def configs c_arr = [] @config_manager.each_key do |res_type| c = self[res_type] unless c.default? c_arr << res_type end end c_arr end
Are any user configurations set?
# File lib/easy/jsonapi/config_manager.rb, line 25 def default? (@config_manager.size == 1 && @config_manager[:global].default?) || all_configs_default? end
Yield the block given on all the config in the config_manager
# File lib/easy/jsonapi/config_manager.rb, line 19 def each(&block) return @config_manager.each(&block) if block_given? to_enum(:each) end
Does the config_manager's internal hash include this res_name? @param res_name [String | Symbol] The res_name to search for in the hash
# File lib/easy/jsonapi/config_manager.rb, line 31 def include?(res_name) @config_manager.include?(res_name.to_sym) end
Adds an config to config_manager's internal hash
# File lib/easy/jsonapi/config_manager.rb, line 43 def insert(res_name, config) if include?(res_name.to_sym) raise "The resource type: #{res_name}, already has an config associated with it. " \ 'Remove existing config first.' end set(res_name, config) end
Remove an config from the config_manager @param (see include) @return [JSONAPI::ConfigManager::Config | nil] the deleted config object if it exists
# File lib/easy/jsonapi/config_manager.rb, line 80 def remove(res_name) if res_name.to_s == 'global' raise "Cannot remove global config" end @config_manager.delete(res_name.to_sym) end
Overwrites the config associated w a given res_name, or adds an association if no config is already associated.
# File lib/easy/jsonapi/config_manager.rb, line 52 def set(res_name, config) raise "Cannot add a config that is not #{@class_type}" unless config.is_a? @class_type @config_manager[res_name.to_sym] = config end
@return [Integer] The number of config in the config_manager
# File lib/easy/jsonapi/config_manager.rb, line 88 def size configs.size end
Used to print out the config_manager object with better formatting return [String] The config_manager object contents represented as a formatted string
# File lib/easy/jsonapi/config_manager.rb, line 106 def to_s to_return = '{ ' is_first = true each do |k, config| if is_first to_return += "#{k}: #{config}" is_first = false else to_return += ", #{k}: #{config}" end end to_return += ' }' end
Private Instance Methods
All of the included configs are set to default? @return [TrueClass | FalseClass]
# File lib/easy/jsonapi/config_manager.rb, line 138 def all_configs_default? res = true @config_manager.each_value { |c| res &= c.default? } res end
Gets the config_manager object whose hash res_name matches the method_name called @param method_name [Symbol] The name of the method called @param args If any arguments were passed to the method called @param block If a block was passed to the method called
# File lib/easy/jsonapi/config_manager.rb, line 126 def method_missing(method_name, *args, &block) super unless @config_manager.include?(method_name) get(method_name) end
Whether or not method missing should be called.
# File lib/easy/jsonapi/config_manager.rb, line 132 def respond_to_missing?(method_name, *) @config_manager.include?(method_name) || super end