Module: Quandl::Support::Attributes

Extended by:
ActiveSupport::Concern
Defined in:
lib/quandl/support/attributes.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#attributesObject



64
65
66
# File 'lib/quandl/support/attributes.rb', line 64

def attributes
  @attributes ||= {}
end

#attributes=(new_attrs) ⇒ Object



55
56
57
58
59
60
61
62
# 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

#initialize(*args) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# 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

#read_attribute(key) ⇒ Object (protected)



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

def read_attribute(key)
  @attributes[key.to_s]
end

#write_attribute(key, value) ⇒ Object (protected)



74
75
76
77
# File 'lib/quandl/support/attributes.rb', line 74

def write_attribute(key, value)
  @attributes ||= {}
  @attributes[key.to_s] = value
end