Module: JsonApiClient::Helpers::DynamicAttributes

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

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
53
54
# File 'lib/json_api_client/helpers/dynamic_attributes.rb', line 40

def method_missing(method, *args, &block)
  normalized_method = if key_formatter
                        key_formatter.unformat(method.to_s)
                      else
                        method.to_s
                      end

  if normalized_method =~ /^(.*)=$/
    set_attribute($1, args.first)
  elsif has_attribute?(method)
    attributes[method]
  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 (method.to_s =~ /^(.*)=$/) || has_attribute?(method)
    true
  else
    super
  end
end