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)



58
59
60
61
62
63
64
65
66
# File 'lib/json_api_client/helpers/attributable.rb', line 58

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

#[](key) ⇒ Object



40
41
42
# File 'lib/json_api_client/helpers/attributable.rb', line 40

def [](key)
  read_attribute(key)
end

#[]=(key, value) ⇒ Object



44
45
46
# File 'lib/json_api_client/helpers/attributable.rb', line 44

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

#attributes=(attrs = {}) ⇒ Object



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

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

  return @attributes unless attrs.present?
  attrs.each do |key, value|
    set_attribute(key, value)
  end
end

#persisted?Boolean

Returns:

  • (Boolean)


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

def persisted?
  attributes.has_key?(primary_key)
end

#query_paramsObject



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

def query_params
  attributes.except(primary_key)
end

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

Returns:

  • (Boolean)


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

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

#to_paramObject



36
37
38
# File 'lib/json_api_client/helpers/attributable.rb', line 36

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

#update_attributes(attrs = {}) ⇒ Object



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

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