Class: FulfilApi::Resource

Inherits:
Object
  • Object
show all
Includes:
AttributeAssignable
Defined in:
lib/fulfil_api/resource.rb,
lib/fulfil_api/resource/relation.rb,
lib/fulfil_api/resource/attribute_type.rb,
lib/fulfil_api/resource/relation/naming.rb,
lib/fulfil_api/resource/relation/loadable.rb,
lib/fulfil_api/resource/attribute_assignable.rb,
lib/fulfil_api/resource/relation/query_methods.rb

Overview

The Resource represents a single resource returned by the API

endpoints of Fulfil.

Defined Under Namespace

Modules: AttributeAssignable Classes: AttributeType, Relation

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AttributeAssignable

#assign_attribute, #assign_attributes

Constructor Details

#initialize(attributes = {}) ⇒ Resource

Returns a new instance of Resource.



9
10
11
12
# File 'lib/fulfil_api/resource.rb', line 9

def initialize(attributes = {})
  @attributes = {}.with_indifferent_access
  assign_attributes(attributes)
end

Class Method Details

.relationFulfilApi::Resource::Relation

Note:

it makes use of the delegate_missing_to method from ActiveSupport to ensure that all unknown class methods for the FulfilApi::Resource are forwarded to the relation.

Builds a new Fulfil::Resource::Relation based on the current class to

enable us to chain requests to Fulfil without querying their API endpoints
multiple times in a row.

Examples:

forwarding of the .where class method

FulfilApi::Resource.set(name: "sale.sale").find_by(["id", "=", 100])

Returns:



29
30
31
# File 'lib/fulfil_api/resource.rb', line 29

def relation
  Relation.new(self)
end

Instance Method Details

#[](attribute_name) ⇒ Any?

Looks up the value for the given attribute name.

Parameters:

  • attribute_name (String, Symbol)

    The name of the attribute

Returns:

  • (Any, nil)


38
39
40
# File 'lib/fulfil_api/resource.rb', line 38

def [](attribute_name)
  @attributes[attribute_name]
end

#to_hHash

Returns all currently assigned attributes for a FulfilApi::Resource.

Returns:

  • (Hash)


45
46
47
# File 'lib/fulfil_api/resource.rb', line 45

def to_h
  @attributes
end