module SaganCrafter::Settings

Public Class Methods

list() click to toggle source

list available settings

# File lib/sagan_crafter/settings.rb, line 40
def self.list
  @@registered_settings
end
print() click to toggle source
reset!() click to toggle source
# File lib/sagan_crafter/settings.rb, line 44
def self.reset!
  self.config do
    verbose false
    fqdnlogger  "PASSIVEDNS"
    iplogger    "CXTRACKER"
    normalizer  "tightstack"
    program     "tightstack"
    sql_table_name "fqdns"
    sql_file_location "/tmp/threat.db"
  end
end
to_h() click to toggle source
# File lib/sagan_crafter/settings.rb, line 56
def self.to_h
  c = {}
  SaganCrafter::Settings.list.each do |toggle|
    c[toggle.to_sym] = SaganCrafter::Settings.send(toggle)
  end
  return c
end

Public Instance Methods

config(&block) click to toggle source

And we define a wrapper for the configuration block, that we'll use to set up our set of options

# File lib/sagan_crafter/settings.rb, line 35
def config &block
  instance_eval(&block)
end
parameter(*names) click to toggle source

SaganCrafter provides a basic single-method DSL with .parameter method being used to define a set of available settings. This method takes one or more symbols, with each one being a name of the configuration option.

# File lib/sagan_crafter/settings.rb, line 11
def parameter(*names)
  names.each do |name|
    attr_accessor name

    @@registered_settings.push(name)

    # For each given symbol we generate accessor method that sets option's
    # value being called with an argument, or returns option's current value
    # when called without arguments
    undef_method name if method_defined? name

    define_method name do |*values|
      value = values.first
      if value
        send("#{name}=", value)
      else
        instance_variable_defined?("@#{name}") ?  instance_variable_get("@#{name}") : nil
      end
    end
  end
end