Module: Namecheap::Config

Extended by:
Config
Included in:
Config
Defined in:
lib/namecheap/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#defaultsObject

Returns the value of attribute defaults.



7
8
9
# File 'lib/namecheap/config.rb', line 7

def defaults
  @defaults
end

#settingsObject

Returns the value of attribute settings.



7
8
9
# File 'lib/namecheap/config.rb', line 7

def settings
  @settings
end

Instance Method Details

#from_hash(options = {}) ⇒ Object

Configure namecheap from a hash. This is usually called after parsing a yaml config file such as mongoid.yml.

Examples:

Configure Namecheap.

config.from_hash({})

Parameters:

  • options (Hash) (defaults to: {})

    The settings to use.



49
50
51
52
53
# File 'lib/namecheap/config.rb', line 49

def from_hash(options = {})
  options.each_pair do |name, value|
    send("#{name}=", value) if respond_to?("#{name}=")
  end
end

#load!(path) ⇒ Object

Load the settings from a compliant namecheap.yml file. This can be used for easy setup with frameworks other than Rails.

Examples:

Configure Namecheap.

Namecheap.load!("/path/to/namecheap.yml")

Parameters:

  • path (String)

    The path to the file.



62
63
64
65
66
67
# File 'lib/namecheap/config.rb', line 62

def load!(path)
  settings = YAML.load(ERB.new(File.new(path).read).result)[ENVIRONMENT]
  if settings.present?
    from_hash(settings)
  end
end

#option(name, options = {}) ⇒ Object

Define a configuration option with a default.

Examples:

Define the option.

Config.option(:client_ip, :default => '127.0.0.1')

Parameters:

  • name (Symbol)

    The name of the configuration option.

  • options (Hash) (defaults to: {})

    Extras for the option.

Options Hash (options):

  • :default (Object)

    The default value.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/namecheap/config.rb', line 20

def option(name, options = {})
  defaults[name] = settings[name] = options[:default]

  class_eval "    def \#{name}\n      settings[\#{name.inspect}]\n    end\n\n    def \#{name}=(value)\n      settings[\#{name.inspect}] = value\n    end\n\n    def \#{name}?\n      \#{name}\n    end\n  RUBY\nend\n"

#resetObject

Reset the configuration options to the defaults.

Examples:

Reset the configuration options.

config.reset


73
74
75
# File 'lib/namecheap/config.rb', line 73

def reset
  settings.replace(defaults)
end