Class: Restfully::Configuration
- Inherits:
-
Object
- Object
- Restfully::Configuration
- Defined in:
- lib/restfully/configuration.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #delete(key) ⇒ Object
-
#expand ⇒ Object
Attempt to expand the configuration if a :configuration_file options is present.
-
#initialize(opts = {}) ⇒ Configuration
constructor
A new instance of Configuration.
- #logger ⇒ Object
- #merge(config = {}) ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Configuration
Returns a new instance of Configuration.
7 8 9 10 11 12 |
# File 'lib/restfully/configuration.rb', line 7 def initialize(opts = {}) @options = opts.symbolize_keys @options[:retry_on_error] ||= 5 @options[:wait_before_retry] ||= 5 end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/restfully/configuration.rb', line 5 def @options end |
Class Method Details
.load(file) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/restfully/configuration.rb', line 68 def self.load(file) if file.nil? raise ArgumentError, "file can't be nil" else new(YAML.load_file(File.(file))) end end |
Instance Method Details
#[](key) ⇒ Object
56 57 58 |
# File 'lib/restfully/configuration.rb', line 56 def [](key) @options[key.to_sym] end |
#[]=(key, value) ⇒ Object
60 61 62 |
# File 'lib/restfully/configuration.rb', line 60 def []=(key, value) @options[key.to_sym] = value end |
#delete(key) ⇒ Object
64 65 66 |
# File 'lib/restfully/configuration.rb', line 64 def delete(key) @options.delete(key.to_sym) end |
#expand ⇒ Object
Attempt to expand the configuration if a :configuration_file options is present. Existing options take precedence over those defined in the configuration file.
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/restfully/configuration.rb', line 44 def file = ENV['RESTFULLY_CONFIG'] || @options[:configuration_file] if file file = File.(file) if File.file?(file) && File.readable?(file) logger.info "Loading configuration from #{file}..." @options = self.class.load(file).merge(self). end end self end |
#logger ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/restfully/configuration.rb', line 14 def logger @options[:logger] ||= begin l = Logger.new(STDERR) l.level = Logger::WARN l end end |
#merge(config = {}) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/restfully/configuration.rb', line 22 def merge(config = {}) if config.respond_to?(:to_hash) @options.merge!(config.to_hash.symbolize_keys) do |key, oldval, newval| case oldval when Array then oldval.push(newval).flatten.uniq when Hash then oldval.merge(newval) else newval end end self else raise ArgumentError, "Don't know how to merge #{config.class}." end end |
#to_hash ⇒ Object
37 38 39 |
# File 'lib/restfully/configuration.rb', line 37 def to_hash @options end |