class JSONAPI::ConfigManager

Manages user configuration options

Public Class Methods

new() click to toggle source

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

[](res_name) click to toggle source

Alias to get @param (see get) @param (see get)

# File lib/easy/jsonapi/config_manager.rb, line 73
def [](res_name)
  get(res_name)
end
[]=(res_name, config) click to toggle source

Alias to set @param (see set) @param (see set)

# File lib/easy/jsonapi/config_manager.rb, line 60
def []=(res_name, config)
  set(res_name, config)
end
add(res_name, config) click to toggle source

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
configs() click to toggle source

@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
default?() click to toggle source

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
each(&block) click to toggle source

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
get(res_name) click to toggle source

@param (see remove) @return [JSONAPI::ConfigManager::Config | nil] The appropriate Item object if it exists

# File lib/easy/jsonapi/config_manager.rb, line 66
def get(res_name)
  @config_manager[res_name.to_sym]
end
include?(res_name) click to toggle source

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
insert(res_name, config) click to toggle source

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(res_name) click to toggle source

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
set(res_name, config) click to toggle source

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
size() click to toggle source

@return [Integer] The number of config in the config_manager

# File lib/easy/jsonapi/config_manager.rb, line 88
def size
  configs.size
end
to_s() click to toggle source

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_configs_default?() click to toggle source

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
method_missing(method_name, *args, &block) click to toggle source

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

Calls superclass method
# 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
respond_to_missing?(method_name, *) click to toggle source

Whether or not method missing should be called.

Calls superclass method
# File lib/easy/jsonapi/config_manager.rb, line 132
def respond_to_missing?(method_name, *)
  @config_manager.include?(method_name) || super
end