Class: IronBank::Resource

Inherits:
Object
  • Object
show all
Extended by:
Associations::ClassMethods, Metadata, Queryable
Includes:
Associations
Defined in:
lib/iron_bank/resource.rb

Overview

An Iron Bank RESTful resource.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Associations::ClassMethods

with_many, with_one

Methods included from Metadata

excluded_fields, fields, query_custom_fields, query_fields, reset, schema, single_resource_query_fields, with_schema

Methods included from Queryable

all, find, find_each, first, where

Methods included from Associations

#with_memoization

Constructor Details

#initialize(remote = {}) ⇒ Resource

Returns a new instance of Resource.



27
28
29
# File 'lib/iron_bank/resource.rb', line 27

def initialize(remote = {})
  @remote = remote
end

Instance Attribute Details

#remoteObject (readonly)

Returns the value of attribute remote.



25
26
27
# File 'lib/iron_bank/resource.rb', line 25

def remote
  @remote
end

Class Method Details

.object_nameObject



12
13
14
# File 'lib/iron_bank/resource.rb', line 12

def self.object_name
  name.split("::").last
end

.with_cacheObject



20
21
22
23
# File 'lib/iron_bank/resource.rb', line 20

def self.with_cache
  include IronBank::Cacheable
  extend IronBank::Cacheable::ClassMethods
end

.with_local_recordsObject



16
17
18
# File 'lib/iron_bank/resource.rb', line 16

def self.with_local_records
  extend IronBank::Local
end

Instance Method Details

#==(other) ⇒ Object

Two resources are equals if their remote (from Zuora) data are similar



48
49
50
# File 'lib/iron_bank/resource.rb', line 48

def ==(other)
  other.is_a?(IronBank::Resource) ? remote == other.remote : false
end

#idObject

Every Zuora object has an ID, so we can safely declare it for each resource



33
34
35
# File 'lib/iron_bank/resource.rb', line 33

def id
  remote[:id]
end

#inspectObject



37
38
39
40
41
42
43
44
45
# File 'lib/iron_bank/resource.rb', line 37

def inspect
  # NOTE: In Ruby, the IDs of objects start from the second bit on the right
  # but in "value space" (used by the original `inspect` implementation)
  # they start from the third bit on the right. Hence the bitsfhit operation
  # here.
  # https://stackoverflow.com/questions/2818602/in-ruby-why-does-inspect-print-out-some-kind-of-object-id-which-is-different
  ruby_id = "#{self.class.name}:0x#{(object_id << 1).to_s(16)} id=\"#{id}\""
  respond_to?(:name) ? "#<#{ruby_id} name=\"#{name}\">" : "#<#{ruby_id}>"
end

#reloadObject



52
53
54
55
56
# File 'lib/iron_bank/resource.rb', line 52

def reload
  remove_instance_vars
  @remote = self.class.find(id).remote
  self
end

#remove_instance_varsObject



58
59
60
61
62
63
# File 'lib/iron_bank/resource.rb', line 58

def remove_instance_vars
  # Substract predefined variables from the instance variables
  (instance_variables - [:@remote]).each do |var|
    remove_instance_variable(:"#{var}")
  end
end