Class: Fiona::Settings

Inherits:
Object
  • Object
show all
Defined in:
lib/fiona/settings.rb

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Settings

Returns a new instance of Settings.

Yields:

  • (_self)

Yield Parameters:



4
5
6
7
8
9
10
11
12
# File 'lib/fiona/settings.rb', line 4

def initialize(&block)
  @state = :initializing
  @defaults = {}

  yield(self)

  merge_settings
  @state = :initialized
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/fiona/settings.rb', line 49

def method_missing(m, *args, &block)
  m = m.to_sym

  if @merged.include?(m)
    return @merged[m][:value]
  else
    raise 'Unknown key'
  end
end

Instance Method Details

#all_settingsObject



29
30
31
# File 'lib/fiona/settings.rb', line 29

def all_settings
  return @merged.clone
end

#default(key, value, scope = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fiona/settings.rb', line 14

def default(key, value, scope = nil)
  raise 'You must set defaults when initializing' unless @state == :initializing
  raise 'You must provide a key' if key.nil?
  raise 'Key must be a symbol' unless key.is_a?(Symbol)
  raise 'You must provide a scope' if scope.nil? && @defaults[key]
  raise 'You can not change a scope' if @defaults[key] && scope
  raise 'Scope must be :public or :private' if scope && ![:public, :private].include?(scope)

  setting = @defaults[key] ||= {}
  setting[:value] = value
  setting[:scope] = scope if scope

  return true
end

#private_settingsObject



37
38
39
# File 'lib/fiona/settings.rb', line 37

def private_settings
  return settings_for_scope(:private)
end

#public_settingsObject



33
34
35
# File 'lib/fiona/settings.rb', line 33

def public_settings
  return settings_for_scope(:public)
end

#reloadObject



45
46
47
# File 'lib/fiona/settings.rb', line 45

def reload
  return merge_settings
end

#settings_for_scope(scope) ⇒ Object



41
42
43
# File 'lib/fiona/settings.rb', line 41

def settings_for_scope(scope)
  return @merged.select { |k, v| v[:scope] == scope }
end