Class: Droom::PreferencesHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/droom/user_config.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &blk) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/droom/user_config.rb', line 53

def method_missing(method_name, *args, &blk)
  return self.get(method_name, &blk) if has_path?(method_name)
  match = method_name.to_s.match(/(.*?)([?=]?)$/)
  case match[2]
  when "="
    self.set(match[1], args.first)
  when "?"
    !!self[match[1]]
  else
    default(method_name, *args, &blk)
  end
end

Instance Method Details

#get(path) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/droom/user_config.rb', line 12

def get(path)
  key, subkeys = split_path(path)
  if subkeys.any?
    self[key.to_sym].get(subkeys)
  else
    if self[key].is_a? Droom::Preference
      self[key].value
    else
      self[key]
    end
  end
end

#has_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
# File 'lib/droom/user_config.rb', line 39

def has_path?(path)
  key, subkeys = split_path(path)
  if subkeys.any?
    self[key.to_sym].has_path?(subkeys)
  else
    self.has_key?(key.to_sym)
  end
end

#set(path, value) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/droom/user_config.rb', line 25

def set(path, value)
  key, subkeys = split_path(path)
  if subkeys.any?
    self[key] ||= LazyHash.new({})
    self[key].set(subkeys, value)
  else
    if self[key].is_a? Droom::Preference
      self[key].set(value)
    else
      self[key] = value
    end
  end
end

#split_path(key) ⇒ Object



48
49
50
51
# File 'lib/droom/user_config.rb', line 48

def split_path(key)
  keys = path.is_a?(Array) ? path : path.to_s.split(':')
  [keys.shift, keys]
end