Class: Dradis::Plugins::Settings

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

Defined Under Namespace

Modules: Adapters

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace, adapter: :db) ⇒ Settings

Returns a new instance of Settings.



5
6
7
8
9
10
# File 'lib/dradis/plugins/settings.rb', line 5

def initialize(namespace, adapter: :db)
  @namespace = namespace
  @dirty_options ||= {}
  @default_options ||= { enabled: true }.with_indifferent_access
  assign_adapter(adapter)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &blk) ⇒ Object (private)

—————————————————- Method missing magic



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dradis/plugins/settings.rb', line 57

def method_missing(name, *args, &blk)
  if name.to_s =~ /^default_(.*)=$/
    @default_options[$1.to_sym] = args.first
  elsif name.to_s =~ /=$/
    @dirty_options[$`.to_sym] = args.first
  elsif @default_options.key?(name)
    dirty_or_stored_or_default(name)
  else
    super
  end
end

Instance Attribute Details

#namespaceObject (readonly)

Returns the value of attribute namespace.



3
4
5
# File 'lib/dradis/plugins/settings.rb', line 3

def namespace
  @namespace
end

Instance Method Details

#allObject



16
17
18
19
20
21
22
23
24
# File 'lib/dradis/plugins/settings.rb', line 16

def all
  @default_options.except(:enabled).map do |key, value|
    {
      name: key.to_sym,
      value: value = dirty_or_stored_or_default(key.to_sym),
      default: is_default?(key, value)
    }
  end.sort_by{ |o| o[:name] }
end

#is_default?(key, value) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/dradis/plugins/settings.rb', line 48

def is_default?(key, value)
  value.to_s == @default_options[key.to_sym].to_s
end

#reset_defaults!Object



41
42
43
44
45
46
# File 'lib/dradis/plugins/settings.rb', line 41

def reset_defaults!
  @dirty_options = {}
  @default_options.each do |key, value|
    delete(key) if exists?(key)
  end
end

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/dradis/plugins/settings.rb', line 12

def respond_to?(name)
  super || @dirty_options.key?(name.to_sym)
end

#saveObject



26
27
28
29
30
31
32
# File 'lib/dradis/plugins/settings.rb', line 26

def save
  @dirty_options.reject do |k, v|
    v.present? && v == read(k)
  end.each do |k, v|
    write(k, v)
  end
end

#update_settings(opts = {}) ⇒ Object



34
35
36
37
38
39
# File 'lib/dradis/plugins/settings.rb', line 34

def update_settings(opts = {})
  opts.select{ |k, v| @default_options.key?(k) }.each do |k, v|
    @dirty_options[k.to_sym] = v
  end
  save
end