Class: ContextualConfig::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/contextual_config/configuration.rb

Overview

Configuration class for gem-wide settings

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



11
12
13
14
15
16
17
18
19
# File 'lib/contextual_config/configuration.rb', line 11

def initialize
  @cache_enabled = false
  @cache_ttl = 300 # 5 minutes in seconds
  @cache_store = nil
  @default_priority = 100
  @enable_logging = false
  @logger = nil
  @timing_evaluation_enabled = true
end

Instance Attribute Details

#cache_enabledObject

Returns the value of attribute cache_enabled.



8
9
10
# File 'lib/contextual_config/configuration.rb', line 8

def cache_enabled
  @cache_enabled
end

#cache_storeObject

Returns the value of attribute cache_store.



8
9
10
# File 'lib/contextual_config/configuration.rb', line 8

def cache_store
  @cache_store
end

#cache_ttlObject

Returns the value of attribute cache_ttl.



8
9
10
# File 'lib/contextual_config/configuration.rb', line 8

def cache_ttl
  @cache_ttl
end

#default_priorityObject

Returns the value of attribute default_priority.



8
9
10
# File 'lib/contextual_config/configuration.rb', line 8

def default_priority
  @default_priority
end

#enable_loggingObject

Returns the value of attribute enable_logging.



8
9
10
# File 'lib/contextual_config/configuration.rb', line 8

def enable_logging
  @enable_logging
end

#loggerObject

Returns the value of attribute logger.



8
9
10
# File 'lib/contextual_config/configuration.rb', line 8

def logger
  @logger
end

#timing_evaluation_enabledObject

Returns the value of attribute timing_evaluation_enabled.



8
9
10
# File 'lib/contextual_config/configuration.rb', line 8

def timing_evaluation_enabled
  @timing_evaluation_enabled
end

Instance Method Details

#cache_enabled?Boolean

Cache configuration

Returns:

  • (Boolean)


22
23
24
# File 'lib/contextual_config/configuration.rb', line 22

def cache_enabled?
  @cache_enabled && cache_store_available?
end

#cache_store_available?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/contextual_config/configuration.rb', line 26

def cache_store_available?
  @cache_store.respond_to?(:read) && @cache_store.respond_to?(:write)
end

#effective_loggerObject

Get effective logger



40
41
42
43
44
45
# File 'lib/contextual_config/configuration.rb', line 40

def effective_logger
  return @logger if logger_available?
  return Rails.logger if defined?(Rails) && Rails.logger

  Logger.new($stdout)
end

#enable_logging?Boolean

Logging configuration

Returns:

  • (Boolean)


31
32
33
# File 'lib/contextual_config/configuration.rb', line 31

def enable_logging?
  @enable_logging && logger_available?
end

#logger_available?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/contextual_config/configuration.rb', line 35

def logger_available?
  @logger.respond_to?(:info) && @logger.respond_to?(:error)
end

#reset!Object

Reset to defaults



66
67
68
# File 'lib/contextual_config/configuration.rb', line 66

def reset!
  initialize
end

#timing_evaluation_enabled?Boolean

Timing evaluation configuration

Returns:

  • (Boolean)


48
49
50
# File 'lib/contextual_config/configuration.rb', line 48

def timing_evaluation_enabled?
  @timing_evaluation_enabled
end

#validate!Object

Validate configuration

Raises:



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/contextual_config/configuration.rb', line 53

def validate!
  if @cache_enabled && !cache_store_available?
    raise(ConfigurationError, 'Cache is enabled but cache_store is not properly configured')
  end

  raise(ConfigurationError, 'cache_ttl must be a positive number') if cache_ttl <= 0

  raise(ConfigurationError, 'default_priority must be non-negative') if default_priority.negative?

  true
end