# File lib/contextio/api/resource.rb, line 137 def primary_key @primary_key end
module ContextIO::API::Resource::DeclarativeClassSyntax
This module contains helper methods for ‘API::Resource`s’ class definitions. It gets ‘extend`ed into a class when `API::Resource` is `include`d.
Public Instance Methods
@!attribute [r] association_name
@return [Symbol] The association name registered for this resource.
# File lib/contextio/api/resource.rb, line 143 def association_name @association_name end
@!attribute [r] associations
@return [Array<String] An array of the belong_to associations for the collection
# File lib/contextio/api/resource.rb, line 150 def associations @associations ||= [] end
Private Instance Methods
Declares the association name for the resource.
@param [String, Symbol] association_name
The name.
# File lib/contextio/api/resource.rb, line 167 def association_name=(association_name) @association_name = association_name.to_sym ContextIO::API::AssociationHelpers.register_resource(self, @association_name) end
Declares that this resource is related to a single instance of another resource. This related resource will be lazily created as it can be, but in some cases may cause an API
call.
@param [Symbol] association_name
The name of the association for the
class in question. Singular classes will have singular names registered. For instance, :message should reger to the Message resource.
# File lib/contextio/api/resource.rb, line 201 def belongs_to(association_name) define_method(association_name) do if instance_variable_defined?("@#{association_name}") instance_variable_get("@#{association_name}") else association_attrs = api_attributes[association_name.to_s] association_class = Contextio::API::AssociationHelpers.class_for_association_name(association_name) if association_attrs && !association_attrs.empty? instance_variable_set("@#{association_name}", association_class.new(api, association_attrs)) else nil end end end associations << association_name.to_sym end
Declares that this resource is related to a collection of another resource. These related resources will be lazily created as they can be, but in some cases may cause an API
call.
@param [Symbol] association_name
The name of the association for the
class in question. Collection classes will have plural names registered. For instance, :messages should reger to the MessageCollection resource.
# File lib/contextio/api/resource.rb, line 228 def has_many(association_name) define_method(association_name) do association_class = ContextIO::API::AssociationHelpers.class_for_association_name(association_name) instance_variable_get("@#{association_name}") || instance_variable_set( "@#{association_name}", association_class.new( api, self.class.association_name => self, attribute_hashes: api_attributes[association_name.to_s] ) ) end associations << association_name.to_sym end
Declares a list of attributes to be lazily loaded from the API
. Getter methods are written for each attribute. If the user asks for one and the object in question doesn’t have it already, then it will look for it in the api_attributes Hash.
@example an example of the generated methods
def some_attribute return @some_attribute if instance_variable_defined?(@some_attribute) api_attributes["some_attribute"] end
@param [Array<String, Symbol>] attributes Attribute names.
# File lib/contextio/api/resource.rb, line 184 def lazy_attributes(*attributes) attributes.each do |attribute_name| define_method(attribute_name) do return instance_variable_get("@#{attribute_name}") if instance_variable_defined?("@#{attribute_name}") api_attributes[attribute_name.to_s] end end end
Declares the primary key used to build the resource URL. Consumed by ‘Resource#validate_options`.
@param [String, Symbol] key Primary key name.
# File lib/contextio/api/resource.rb, line 160 def primary_key=(key) @primary_key = key end