Class: JSONAPI::ConfigManager
- Inherits:
-
Object
- Object
- JSONAPI::ConfigManager
- Includes:
- Enumerable
- Defined in:
- lib/easy/jsonapi/config_manager.rb,
lib/easy/jsonapi/config_manager/config.rb
Overview
Manages user configuration options
Defined Under Namespace
Classes: Config
Instance Method Summary collapse
-
#[](res_name) ⇒ Object
Alias to #get.
-
#[]=(res_name, config) ⇒ Object
Alias to #set.
-
#add(res_name, config) ⇒ Object
Add an config to the config_manager.
-
#configs ⇒ Array<Symbol>
The names of the resource types the configs belong to.
-
#default? ⇒ Boolean
Are any user configurations set?.
-
#each(&block) ⇒ Object
Yield the block given on all the config in the config_manager.
-
#get(res_name) ⇒ JSONAPI::ConfigManager::Config | nil
The appropriate Item object if it exists.
-
#include?(res_name) ⇒ Boolean
Does the config_manager’s internal hash include this res_name?.
-
#initialize ⇒ ConfigManager
constructor
Config Manager always has an internal global config.
-
#insert(res_name, config) ⇒ Object
Adds an config to config_manager’s internal hash.
-
#remove(res_name) ⇒ JSONAPI::ConfigManager::Config | nil
Remove an config from the config_manager.
-
#set(res_name, config) ⇒ Object
Overwrites the config associated w a given res_name, or adds an association if no config is already associated.
-
#size ⇒ Integer
The number of config in the config_manager.
-
#to_s ⇒ Object
Used to print out the config_manager object with better formatting return [String] The config_manager object contents represented as a formatted string.
Constructor Details
#initialize ⇒ ConfigManager
Config Manager always has an internal global config
13 14 15 16 |
# File 'lib/easy/jsonapi/config_manager.rb', line 13 def initialize @class_type = JSONAPI::ConfigManager::Config @config_manager = { global: JSONAPI::ConfigManager::Config.new } end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object (private)
Gets the config_manager object whose hash res_name matches the method_name called
126 127 128 129 |
# 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 |
Instance Method Details
#[](res_name) ⇒ Object
Alias to #get
73 74 75 |
# File 'lib/easy/jsonapi/config_manager.rb', line 73 def [](res_name) get(res_name) end |
#[]=(res_name, config) ⇒ Object
Alias to #set
60 61 62 |
# File 'lib/easy/jsonapi/config_manager.rb', line 60 def []=(res_name, config) set(res_name, config) end |
#add(res_name, config) ⇒ Object
Add an config to the config_manager
37 38 39 40 |
# 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 |
#configs ⇒ Array<Symbol>
Returns The names of the resource types the configs belong to.
93 94 95 96 97 98 99 100 101 102 |
# 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 |
#default? ⇒ Boolean
Are any user configurations set?
25 26 27 |
# File 'lib/easy/jsonapi/config_manager.rb', line 25 def default? (@config_manager.size == 1 && @config_manager[:global].default?) || all_configs_default? end |
#each(&block) ⇒ Object
Yield the block given on all the config in the config_manager
19 20 21 22 |
# File 'lib/easy/jsonapi/config_manager.rb', line 19 def each(&block) return @config_manager.each(&block) if block_given? to_enum(:each) end |
#get(res_name) ⇒ JSONAPI::ConfigManager::Config | nil
Returns The appropriate Item object if it exists.
66 67 68 |
# File 'lib/easy/jsonapi/config_manager.rb', line 66 def get(res_name) @config_manager[res_name.to_sym] end |
#include?(res_name) ⇒ Boolean
Does the config_manager’s internal hash include this res_name?
31 32 33 |
# File 'lib/easy/jsonapi/config_manager.rb', line 31 def include?(res_name) @config_manager.include?(res_name.to_sym) end |
#insert(res_name, config) ⇒ Object
Adds an config to config_manager’s internal hash
43 44 45 46 47 48 49 |
# 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(res_name) ⇒ JSONAPI::ConfigManager::Config | nil
Remove an config from the config_manager
80 81 82 83 84 85 |
# 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 |
#set(res_name, config) ⇒ Object
Overwrites the config associated w a given res_name, or adds an association if no config is already associated.
52 53 54 55 |
# 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 |
#size ⇒ Integer
Returns The number of config in the config_manager.
88 89 90 |
# File 'lib/easy/jsonapi/config_manager.rb', line 88 def size configs.size end |
#to_s ⇒ Object
Used to print out the config_manager object with better formatting return [String] The config_manager object contents represented as a formatted string
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# 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 |