Class: Wheel::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/wheel/store.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStore

Returns a new instance of Store.



23
24
25
# File 'lib/wheel/store.rb', line 23

def initialize
  reload
end

Class Method Details

.instanceObject



10
11
12
# File 'lib/wheel/store.rb', line 10

def instance
  @instance ||= new
end

.method_missing(method_name, *args, &block) ⇒ Object



14
15
16
# File 'lib/wheel/store.rb', line 14

def method_missing(method_name, *args, &block)
  instance.public_send(method_name, *args, &block)
end

.respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/wheel/store.rb', line 18

def respond_to_missing?(method_name, include_private = false)
  instance.respond_to?(method_name, include_private)
end

Instance Method Details

#[](key, context = {}) ⇒ Object



40
41
42
43
44
45
# File 'lib/wheel/store.rb', line 40

def [](key, context = {})
  config = @configs[key.to_s]
  return nil unless config

  config.evaluate(context)
end

#[]=(key, value) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/wheel/store.rb', line 47

def []=(key, value)
  config = Wheel::Config.find_or_initialize_by(key: key)
  config.update!(
    value_type: detect_value_type(value),
    default_value: value
  )
  @configs[key.to_s] = config
  value
end

#reloadObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/wheel/store.rb', line 27

def reload
  @configs = {}
  if defined?(ActiveRecord) && ActiveRecord::Base.connected? && 
     ActiveRecord::Base.connection.table_exists?('wheel_configs')
    Wheel::Config.find_each do |config|
      @configs[config.key] = config
    end
  end
rescue => e
  Rails.logger.warn "Failed to load Wheel configs: #{e.message}" if defined?(Rails)
  @configs
end