Class: Quandl::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/quandl/configuration.rb

Direct Known Subclasses

Logger::GrayLog2::Configuration

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Configuration

Returns a new instance of Configuration.



43
44
45
46
47
48
# File 'lib/quandl/configuration.rb', line 43

def initialize(*args)
  attrs = args.extract_options!
  self.attributes
  self.assign_attributes(attrs)
  from_file(self.class.config_file, self.class.config_env) if self.class.config_file.present? && self.class.config_env.present?
end

Class Attribute Details

.config_env ⇒ Object

Returns the value of attribute config_env.



7
8
9
# File 'lib/quandl/configuration.rb', line 7

def config_env
  @config_env
end

.config_file ⇒ Object

Returns the value of attribute config_file.



7
8
9
# File 'lib/quandl/configuration.rb', line 7

def config_file
  @config_file
end

Class Method Details

.attribute_names ⇒ Object



37
38
39
# File 'lib/quandl/configuration.rb', line 37

def attribute_names
  @attribute_names ||= []
end

.configures(*names) ⇒ Object



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

def configures(*names)
  define_attributes(*names)
end

.define_attribute(name) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/quandl/configuration.rb', line 22

def define_attribute(name)
  name = name.to_sym
  define_method(name) do
    read_attribute(name)
  end
  define_method("#{name}=") do |value|
    write_attribute(name, value)
  end
  define_method("#{name}?") do
    read_attribute(name).present?
  end
  # store an array of defined attriubte names
  attribute_names << name unless attribute_names.include?(name)
end

.define_attributes(*names) ⇒ Object



18
19
20
# File 'lib/quandl/configuration.rb', line 18

def define_attributes(*names)
  Array(names).each{|name| define_attribute(name) }
end

.from_file(file, env) ⇒ Object



9
10
11
12
# File 'lib/quandl/configuration.rb', line 9

def from_file(file, env)
  self.config_file = file
  self.config_env = env
end

Instance Method Details

#assign_attributes(hash) ⇒ Object



56
57
58
59
60
# File 'lib/quandl/configuration.rb', line 56

def assign_attributes(hash)
  hash.each do |k,v|
    send("#{k}=", v) if self.class.attribute_names.include?(k.to_sym) && respond_to?("#{k}=")
  end
end

#attributes ⇒ Object



66
67
68
# File 'lib/quandl/configuration.rb', line 66

def attributes
  @attributes ||= self.class.attribute_names.inject({}){|m,k| m[k] ||= nil; m }
end

#attributes=(value) ⇒ Object



70
71
72
# File 'lib/quandl/configuration.rb', line 70

def attributes=(value)
  @attributes = value.symbolize_keys! if value.is_a?(Hash)
end

#from_file(path, env) ⇒ Object

Raises:

  • (ArgumentError)


50
51
52
53
54
# File 'lib/quandl/configuration.rb', line 50

def from_file(path, env)
  config = YAML.load_file( path )[ env ]
  raise ArgumentError, "Unknown env #{env}" if config.nil?
  config.each{|k,v| self.send("#{k}=",v) if respond_to?("#{k}=") }
end

#inspect ⇒ Object



62
63
64
# File 'lib/quandl/configuration.rb', line 62

def inspect
  "#{self.class.name} " + attributes.inspect
end

#read_attribute(attribute) ⇒ Object



78
79
80
# File 'lib/quandl/configuration.rb', line 78

def read_attribute(attribute)
  @attributes[:"#{attribute}"]
end

#write_attribute(attribute, value) ⇒ Object



74
75
76
# File 'lib/quandl/configuration.rb', line 74

def write_attribute(attribute, value)
  @attributes[:"#{attribute}"] = value
end