module Quandl::Support::Attributes

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/quandl/support/attributes.rb, line 43
def initialize(*args)
  run_callbacks(:initialize) do
    attrs = args.extract_options!
    # apply _attributes directly to @attributes
    @attributes = attrs[:_attributes].is_a?(Hash) ? attrs.delete(:_attributes) : {}
    # apply attrs through write_attribute
    self.attributes = attrs if attrs.is_a?(Hash)
    # onwards
    super(*args) if defined?(super)
  end
end

Public Instance Methods

attributes() click to toggle source
# File lib/quandl/support/attributes.rb, line 64
def attributes
  @attributes ||= {}
end
attributes=(new_attrs) click to toggle source
# File lib/quandl/support/attributes.rb, line 55
def attributes=(new_attrs)
  new_attrs.stringify_keys.each do |attr_key, attr_value|
    # skip those attributes that are not defined
    next unless self.class.attributes.include?(attr_key) && self.respond_to?("#{attr_key}=")
    # pass to the attribute writer
    self.send( "#{attr_key}=", attr_value )
  end
end

Protected Instance Methods

read_attribute(key) click to toggle source
# File lib/quandl/support/attributes.rb, line 70
def read_attribute(key)
  @attributes[key.to_s]
end
write_attribute(key, value) click to toggle source
# File lib/quandl/support/attributes.rb, line 74
def write_attribute(key, value)
  @attributes ||= {}
  @attributes[key.to_s] = value
end