Module: JSONAPI::Consumer::Resource

Extended by:
ActiveSupport::Concern
Includes:
ConnectionConcern, AssociationConcern, AttributesConcern, FindersConcern, ObjectBuildConcern, SerializerConcern
Defined in:
lib/jsonapi/consumer/resource.rb,
lib/jsonapi/consumer/resource/finders_concern.rb,
lib/jsonapi/consumer/resource/attributes_concern.rb,
lib/jsonapi/consumer/resource/serializer_concern.rb,
lib/jsonapi/consumer/resource/association_concern.rb,
lib/jsonapi/consumer/resource/object_build_concern.rb

Defined Under Namespace

Modules: AssociationConcern, AttributesConcern, ClassMethods, FindersConcern, ObjectBuildConcern, SerializerConcern Classes: MisconfiguredAssociation

Instance Method Summary collapse

Methods included from ConnectionConcern

#destroy, #is_valid?, #save

Methods included from SerializerConcern

#add_link, #add_links, #serializable_hash, #to_json

Methods included from FindersConcern

#primary_key

Methods included from AssociationConcern

#association_names, #each_association

Methods included from AttributesConcern

#attributes=, #persisted?, #respond_to?, #respond_to_without_attributes?, #to_param, #update_attributes

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/jsonapi/consumer/resource.rb', line 76

def method_missing(method, *args, &block)
  if respond_to_without_attributes?(method, true)
    super
  else
    if method.to_s =~ /^(.*)=$/
      set_attribute($1, args.first)
    elsif has_attribute?(method)
      read_attribute(method)
    elsif has_association?(method)
      read_assocation(method)
    else
      super
    end
  end
end

Instance Method Details

#initialize(params = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/jsonapi/consumer/resource.rb', line 51

def initialize(params={})
  (params || {}).slice(*association_names).each do |key, value|
    send(:"#{key}=", value)
  end

  self.attributes = params.except(*association_names) if params
  @errors = ActiveModel::Errors.new(self)
  super()
end

#to_keyObject

Returns an Enumerable of all key attributes if any is set, regardless if the object is persisted or not. Returns nil if there are no key attributes.

(see ActiveModel::Conversion#to_key)



66
67
68
# File 'lib/jsonapi/consumer/resource.rb', line 66

def to_key
  to_param ? [to_param] : nil
end