module JsonApiClient::Helpers::DynamicAttributes

Public Instance Methods

[](key) click to toggle source
# File lib/json_api_client/helpers/dynamic_attributes.rb, line 18
def [](key)
  read_attribute(key)
end
[]=(key, value) click to toggle source
# File lib/json_api_client/helpers/dynamic_attributes.rb, line 22
def []=(key, value)
  set_attribute(key, value)
end
attributes() click to toggle source
# File lib/json_api_client/helpers/dynamic_attributes.rb, line 5
def attributes
  @attributes
end
attributes=(attrs = {}) click to toggle source
# File lib/json_api_client/helpers/dynamic_attributes.rb, line 9
def attributes=(attrs = {})
  @attributes ||= ActiveSupport::HashWithIndifferentAccess.new

  return @attributes unless attrs.present?
  attrs.each do |key, value|
    send("#{key}=", value)
  end
end
has_attribute?(attr_name) click to toggle source
# File lib/json_api_client/helpers/dynamic_attributes.rb, line 34
def has_attribute?(attr_name)
  attributes.has_key?(attr_name)
end
respond_to_missing?(method, include_private = false) click to toggle source
Calls superclass method
# File lib/json_api_client/helpers/dynamic_attributes.rb, line 26
def respond_to_missing?(method, include_private = false)
  if has_attribute?(method) || method.to_s.end_with?('=')
    true
  else
    super
  end
end

Protected Instance Methods

key_formatter() click to toggle source
# File lib/json_api_client/helpers/dynamic_attributes.rb, line 66
def key_formatter
  self.class.respond_to?(:key_formatter) && self.class.key_formatter
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/json_api_client/helpers/dynamic_attributes.rb, line 40
def method_missing(method, *args, &block)
  if has_attribute?(method)
    return attributes[method]
  end

  normalized_method = safe_key_formatter.unformat(method.to_s)

  if normalized_method.end_with?('=')
    set_attribute(normalized_method[0..-2], args.first)
  else
    super
  end
end
read_attribute(name) click to toggle source
# File lib/json_api_client/helpers/dynamic_attributes.rb, line 54
def read_attribute(name)
  attributes.fetch(name, nil)
end
safe_key_formatter() click to toggle source
# File lib/json_api_client/helpers/dynamic_attributes.rb, line 62
def safe_key_formatter
  @safe_key_formatter ||= (key_formatter || DefaultKeyFormatter.new)
end
set_attribute(name, value) click to toggle source
# File lib/json_api_client/helpers/dynamic_attributes.rb, line 58
def set_attribute(name, value)
  attributes[name] = value
end