Class: Defaultable::Settings

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = nil, skip_defaults = false, enable_registry = true) ⇒ Settings

Returns a new instance of Settings.



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

def initialize(hash=nil, skip_defaults=false, enable_registry=true)
  @table    = {}
  @registry = Registry.new

  if !skip_defaults && !self.class.defaults.nil?
  	self.registry_enabled = false
    recursive_hash_assignment self.class.defaults
  end

  # Enable the registry so we know what actually was set beyond defaults
  self.registry_enabled = enable_registry
  
  if !hash.nil? && hash.kind_of?(Hash)
    recursive_hash_assignment hash
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/defaultable/settings.rb', line 26

def method_missing(message, *args, &block)
  # puts "Message: #{message}", "Args: #{args}", "\n"

  if !(message.to_s =~ /^(.*)=$/) && @table.has_key?(message.to_s)
    @table[message.to_s]
  elsif message.to_s =~ /^(.*)\?$/
    @table.has_key?(message.to_s.gsub(/\?$/, ''))
  elsif message.to_s =~ /^(.*)=$/
    key = message.to_s.gsub(/=$/, '')
    value = args.first

    if self.registry_enabled
    	self.registry.add(key, value)
    end

    if value.kind_of?(Defaultable::Settings)
    	value.root_key = key
    	value.parent   = self
    end

    if self.parent
      self.registry.add(key, value)
    	update_parent_registries
    end

    @table[key] = value
  end
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



7
8
9
# File 'lib/defaultable/settings.rb', line 7

def parent
  @parent
end

#registryObject (readonly)

Returns the value of attribute registry.



5
6
7
# File 'lib/defaultable/settings.rb', line 5

def registry
  @registry
end

#registry_enabledObject

Returns the value of attribute registry_enabled.



4
5
6
# File 'lib/defaultable/settings.rb', line 4

def registry_enabled
  @registry_enabled
end

#root_keyObject

Returns the value of attribute root_key.



7
8
9
# File 'lib/defaultable/settings.rb', line 7

def root_key
  @root_key
end

Class Method Details

.set_defaults(settings, env = nil) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/defaultable/settings.rb', line 105

def set_defaults(settings, env=nil)
  case settings
  when Hash
    self.defaults = settings
  when String
    yaml = YAML.load_file(settings)
    yaml = yaml[env] if env
    self.defaults = yaml
  else
    raise ArgumentError, "You must supply a hash or a file name for default settings"
  end
end

Instance Method Details

#as_hashObject



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/defaultable/settings.rb', line 55

def as_hash
  @table.inject({}) do |hash, (key, val)|
    if val.kind_of?(Defaultable::Settings)
      val = val.as_hash
    else
      val
    end

    hash[key] = val
    hash
  end
end

#empty?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/defaultable/settings.rb', line 68

def empty?
  @table.length == 0
end

#key_aware_assignment(key, hash) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/defaultable/settings.rb', line 82

def key_aware_assignment(key, hash)
  if self.send("#{key}?")
    instance = self.send(key)

    if instance.kind_of?(Defaultable::Settings)
      instance.recursive_hash_assignment(hash)
      self.send("#{key}=", instance)
    else
      self.send("#{key}=", hash)
    end
  else
    self.send("#{key}=", self.class.new(hash, true, self.registry_enabled))
  end
end

#recursive_hash_assignment(hash) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/defaultable/settings.rb', line 72

def recursive_hash_assignment(hash)
  hash.each do |key, value|
    if value.kind_of?(Hash)
      key_aware_assignment(key, value)
    else
      self.send("#{key}=", value)
    end
  end
end

#update_parent_registriesObject



97
98
99
100
101
102
# File 'lib/defaultable/settings.rb', line 97

def update_parent_registries
  if self.parent && self.root_key
    self.parent.registry.add(self.root_key, self)
    self.parent.update_parent_registries
  end
end