Module: JsonApiClient::Helpers::DynamicAttributes

Included in:
Linking::Links, MetaData, Relationships::Relations, Resource
Defined in:
lib/json_api_client/helpers/dynamic_attributes.rb

Defined Under Namespace

Classes: DefaultKeyFormatter

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (protected)



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

Instance Method Details

#[](key) ⇒ Object



18
19
20
# File 'lib/json_api_client/helpers/dynamic_attributes.rb', line 18

def [](key)
  read_attribute(key)
end

#[]=(key, value) ⇒ Object



22
23
24
# File 'lib/json_api_client/helpers/dynamic_attributes.rb', line 22

def []=(key, value)
  set_attribute(key, value)
end

#attributesObject



5
6
7
# File 'lib/json_api_client/helpers/dynamic_attributes.rb', line 5

def attributes
  @attributes
end

#attributes=(attrs = {}) ⇒ Object



9
10
11
12
13
14
15
16
# 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) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# 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) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
# 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