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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

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


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/pages_core/configuration/base.rb', line 10

def self.setting(key, type, default = nil)
  settings[key] = OpenStruct.new(type: type, default: 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


6
7
8
# File 'lib/pages_core/configuration/base.rb', line 6

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

Instance Method Details

#get(key) ⇒ Object


26
27
28
29
30
31
32
33
# File 'lib/pages_core/configuration/base.rb', line 26

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


35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pages_core/configuration/base.rb', line 35

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