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 (protected)



46
47
48
49
50
51
52
53
54
# File 'lib/json_api_client/helpers/attributable.rb', line 46

def method_missing(method, *args, &block)
  if method.to_s =~ /^(.*)=$/
    set_attribute($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)


24
25
26
# File 'lib/json_api_client/helpers/attributable.rb', line 24

def persisted?
  attributes.has_key?(primary_key)
end

#query_paramsObject



28
29
30
# File 'lib/json_api_client/helpers/attributable.rb', line 28

def query_params
  attributes.except(primary_key)
end

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
# File 'lib/json_api_client/helpers/attributable.rb', line 36

def respond_to?(method, include_private = false)
  if (method.to_s =~ /^(.*)=$/) || has_attribute?(method)
    true
  else
    super
  end
end

#to_paramObject



32
33
34
# File 'lib/json_api_client/helpers/attributable.rb', line 32

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