Module: Confiture::Configuration::ClassExtension

Defined in:
lib/confiture/configuration.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object (private)



141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/confiture/configuration.rb', line 141

def method_missing(meth, *args)
  meth = "#{meth}"
  if meth =~ /.+=/ && args.size == 1
    key = meth[0..-2].to_sym
    validate_key!(key)
    data.options[key] = args.last
  elsif args.size == 0
    key = meth.to_sym
    validate_key!(key)
    data.options[key]
  else
    super
  end
end

Instance Method Details

#configure(options = {}, reset = false) ⇒ Object

Rails initializer configuration:

Confiture::Configuration.configure do |config|
  config.secret = 'your-secret'
  config.key    = 'your-key'
end

You may pass options as a hash as well:

Confiture::Configuration.configure :secret => 'your-secret', :key => 'your-key'

Or configure everything using YAML:

Confiture::Configuration.configure :yaml => 'config/asin.yml'

Confiture::Configuration.configure :yaml => 'config/asin.yml' do |config, yml|
  config.key = yml[Rails.env]['key']
end

Options:

yaml|yml

path to a yaml file with configuration



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/confiture/configuration.rb', line 56

def configure(options={},reset=false)
  init_config(reset)
  if yml_path = options[:yaml] || options[:yml]
    yml = File.open(yml_path) { |file| YAML.load(file) }
    if block_given?
      yield self, yml
    else
      yml.each do |key, value|
        send(:"#{key}=", value)
      end
    end
  elsif block_given?
    yield self
  else
    options.each do |key, value|
      send(:"#{key}=", value)
    end
  end
  self
end

#confiture_allowed_keys(*allowed_keys) ⇒ Object

Set a list of allowed configuration options. If set, trying to access an option that is not configured will result in an ArgumentError.



21
22
23
# File 'lib/confiture/configuration.rb', line 21

def confiture_allowed_keys(*allowed_keys)
  @allowed_keys = allowed_keys
end

#confiture_defaults(defaults) ⇒ Object

Set a hash of defaults. Defaults will be used while configuring the options, but may be overridden.



14
15
16
# File 'lib/confiture/configuration.rb', line 14

def confiture_defaults(defaults)
  @defaults = defaults
end

#confiture_mandatory_keys(*mandatory_keys) ⇒ Object

Set a list of mandatory configuration options. If set, trying to access an option that is not configured properly will result in an ArgumentError. The validation can be triggered manually by calling validate!



29
30
31
# File 'lib/confiture/configuration.rb', line 29

def confiture_mandatory_keys(*mandatory_keys)
  @mandatory_keys = mandatory_keys
end

#reset!Object

Resets configuration to defaults



88
89
90
# File 'lib/confiture/configuration.rb', line 88

def reset!
  init_config(true)
end

#valid?Boolean

Validates the configuration. All mandatory keys have to be not blank.

Returns:

  • (Boolean)


102
103
104
# File 'lib/confiture/configuration.rb', line 102

def valid?
  @mandatory_keys.nil? || @mandatory_keys.none? { |key| blank?(key) }
end

#valid_key?(key) ⇒ Boolean

Validates if a given key is valid for configuration.

Returns:

  • (Boolean)


116
117
118
# File 'lib/confiture/configuration.rb', line 116

def valid_key?(key)
  @allowed_keys.nil? || @allowed_keys.include?(key)
end

#validate!Object

Raises an ArgumentError if the configuration is not valid.



94
95
96
97
98
# File 'lib/confiture/configuration.rb', line 94

def validate!
  unless valid?
    raise ArgumentError.new("you are missing mandatory configuration options. please set #{@mandatory_keys}")
  end
end

#validate_key!(key) ⇒ Object

Raises an ArgumentError if the given key is not allowed as a configuration option.



108
109
110
111
112
# File 'lib/confiture/configuration.rb', line 108

def validate_key!(key)
  unless valid_key?(key)
    raise ArgumentError.new("#{key} is not allowed, use one of #{@allowed_keys}")
  end
end

#with_config(options = {}) ⇒ Object

Run a block of code with temporary configuration.



79
80
81
82
83
84
# File 'lib/confiture/configuration.rb', line 79

def with_config(options={})
  current_data = data
  configure(options, true)
ensure
  self.data = current_data
end