Class: PagesCore::Configuration::Base

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

Direct Known Subclasses

Pages

Defined Under Namespace

Classes: InvalidConfigurationKey, SettingStruct

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.setting(key, type, default = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/pages_core/configuration/base.rb', line 14

def self.setting(key, type, default = nil)
  settings[key] = SettingStruct.new(type, default)

  define_method key do |*args|
    args.any? ? set(key, *args) : get(key)
  end

  define_method "#{key}=" do |value|
    set(key, value)
  end

  define_method "#{key}?" do
    get(key) ? true : false
  end
end

.settingsObject



10
11
12
# File 'lib/pages_core/configuration/base.rb', line 10

def self.settings
  @settings ||= {}
end

Instance Method Details

#get(key) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/pages_core/configuration/base.rb', line 30

def get(key)
  raise InvalidConfigurationKey unless setting?(key)

  if configuration.key?(key)
    configuration[key]
  else
    self.class.settings[key].default
  end
end

#set(key, value) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/pages_core/configuration/base.rb', line 40

def set(key, value)
  raise InvalidConfigurationKey unless setting?(key)

  value = parse_value(key, value)
  unless valid_type?(key, value)
    raise(
      ArgumentError,
      "expected #{self.class.settings[key].type}, got #{value.class}"
    )
  end
  configuration[key] = value
end