Module: JsonApiClient::Helpers::Attributable

Extended by:
ActiveSupport::Concern
Included in:
Resource
Defined in:
lib/json_api_client/helpers/attributable.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



24
25
26
27
28
29
30
31
32
# File 'lib/json_api_client/helpers/attributable.rb', line 24

def method_missing(method, *args, &block)
  if match = method.to_s.match(/^(.*)=$/)
    set_attribute(match[1], args.first)
  elsif has_attribute?(method)
    attributes[method]
  else
    super
  end
end

Instance Method Details

#attributes=(attrs = {}) ⇒ Object



14
15
16
17
# File 'lib/json_api_client/helpers/attributable.rb', line 14

def attributes=(attrs = {})
  @attributes ||= {}.with_indifferent_access
  @attributes.merge!(attrs)
end

#persisted?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/json_api_client/helpers/attributable.rb', line 34

def persisted?
  attributes.has_key?(primary_key)
end

#query_paramsObject



38
39
40
# File 'lib/json_api_client/helpers/attributable.rb', line 38

def query_params
  attributes.except(primary_key)
end

#to_paramObject



42
43
44
# File 'lib/json_api_client/helpers/attributable.rb', line 42

def to_param
  attributes.fetch(primary_key, "").to_s
end

#update_attributes(attrs = {}) ⇒ Object



19
20
21
22
# File 'lib/json_api_client/helpers/attributable.rb', line 19

def update_attributes(attrs = {})
  self.attributes = attrs
  save
end