Class: Lightspeed::Collection
- Inherits:
-
Object
- Object
- Lightspeed::Collection
show all
- Defined in:
- lib/lightspeed/collection.rb
Direct Known Subclasses
Accounts, Categories, Customers, Employees, Images, Inventories, ItemAttributeSets, ItemMatrices, Items, Orders, PriceLevels, SaleLines, Sales, Shops, SpecialOrders, Vendors
Constant Summary
collapse
- PER_PAGE =
the max page of records returned in a request
100
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#account ⇒ Object
-
#all(params: {}) ⇒ Object
-
#all_loaded ⇒ Object
-
#as_json ⇒ Object
(also: #to_h)
-
#base_path ⇒ Object
-
#client ⇒ Object
-
#create(attributes = {}) ⇒ Object
-
#destroy(id) ⇒ Object
-
#each(per_page: PER_PAGE, params: {}, &block) ⇒ Object
-
#each_loaded ⇒ Object
-
#each_page(per_page: PER_PAGE, params: {}, &block) ⇒ Object
-
#enum(per_page: PER_PAGE, params: {}) ⇒ Object
-
#enum_page(per_page: PER_PAGE, params: {}) ⇒ Object
-
#find(id) ⇒ Object
-
#first(params: {}) ⇒ Object
-
#first_loaded ⇒ Object
-
#initialize(context:, attributes: nil) ⇒ Collection
constructor
A new instance of Collection.
-
#inspect ⇒ Object
-
#load_relations_default ⇒ Object
-
#page(n, per_page: PER_PAGE, params: {}) ⇒ Object
-
#size(params: {}) ⇒ Object
(also: #length)
-
#size_loaded ⇒ Object
-
#to_json ⇒ Object
-
#unload ⇒ Object
-
#update(id, attributes = {}) ⇒ Object
Constructor Details
#initialize(context:, attributes: nil) ⇒ Collection
Returns a new instance of Collection.
12
13
14
15
|
# File 'lib/lightspeed/collection.rb', line 12
def initialize(context:, attributes: nil)
self.context = context
instantiate(attributes)
end
|
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
10
11
12
|
# File 'lib/lightspeed/collection.rb', line 10
def context
@context
end
|
#resources ⇒ Object
Returns the value of attribute resources.
10
11
12
|
# File 'lib/lightspeed/collection.rb', line 10
def resources
@resources
end
|
Class Method Details
.collection_name ⇒ Object
104
105
106
|
# File 'lib/lightspeed/collection.rb', line 104
def self.collection_name
name.demodulize
end
|
.resource_class ⇒ Object
112
113
114
|
# File 'lib/lightspeed/collection.rb', line 112
def self.resource_class
"Lightspeed::#{resource_name}".constantize
end
|
.resource_name ⇒ Object
108
109
110
|
# File 'lib/lightspeed/collection.rb', line 108
def self.resource_name
collection_name.singularize
end
|
Instance Method Details
#account ⇒ Object
17
18
19
|
# File 'lib/lightspeed/collection.rb', line 17
def account
context.account
end
|
#all(params: {}) ⇒ Object
58
59
60
|
# File 'lib/lightspeed/collection.rb', line 58
def all(params: {})
enum_page(params: params).to_a.flatten(1)
end
|
#all_loaded ⇒ Object
46
47
48
|
# File 'lib/lightspeed/collection.rb', line 46
def all_loaded
each_loaded.to_a
end
|
#as_json ⇒ Object
Also known as:
to_h
124
125
126
127
|
# File 'lib/lightspeed/collection.rb', line 124
def as_json
return if all_loaded.empty?
{ resource_name => all_loaded.as_json }
end
|
#base_path ⇒ Object
116
117
118
|
# File 'lib/lightspeed/collection.rb', line 116
def base_path
"#{account.base_path}/#{resource_name}"
end
|
#client ⇒ Object
25
26
27
28
|
# File 'lib/lightspeed/collection.rb', line 25
def client
return context if context.is_a?(Lightspeed::Client)
account.client
end
|
#create(attributes = {}) ⇒ Object
92
93
94
|
# File 'lib/lightspeed/collection.rb', line 92
def create(attributes = {})
instantiate(post(body: Yajl::Encoder.encode(attributes))).first
end
|
#destroy(id) ⇒ Object
100
101
102
|
# File 'lib/lightspeed/collection.rb', line 100
def destroy(id)
instantiate(delete(id)).first
end
|
#each(per_page: PER_PAGE, params: {}, &block) ⇒ Object
84
85
86
|
# File 'lib/lightspeed/collection.rb', line 84
def each(per_page: PER_PAGE, params: {}, &block)
enum(per_page: per_page, params: params).each(&block)
end
|
#each_loaded ⇒ Object
41
42
43
44
|
# File 'lib/lightspeed/collection.rb', line 41
def each_loaded
@resources ||= {}
@resources.each_value
end
|
#each_page(per_page: PER_PAGE, params: {}, &block) ⇒ Object
62
63
64
|
# File 'lib/lightspeed/collection.rb', line 62
def each_page(per_page: PER_PAGE, params: {}, &block)
enum_page(per_page: per_page, params: params).each(&block)
end
|
#enum(per_page: PER_PAGE, params: {}) ⇒ Object
76
77
78
79
80
81
82
|
# File 'lib/lightspeed/collection.rb', line 76
def enum(per_page: PER_PAGE, params: {})
Enumerator.new do |yielder|
each_page(per_page: per_page, params: params) do |page|
page.each { |resource| yielder << resource }
end
end
end
|
#enum_page(per_page: PER_PAGE, params: {}) ⇒ Object
66
67
68
69
70
71
72
73
74
|
# File 'lib/lightspeed/collection.rb', line 66
def enum_page(per_page: PER_PAGE, params: {})
Enumerator.new do |yielder|
loop.with_index do |_, n|
resources = page(n, per_page: per_page, params: params)
yielder << resources
raise StopIteration if resources.length < per_page
end
end
end
|
#find(id) ⇒ Object
88
89
90
|
# File 'lib/lightspeed/collection.rb', line 88
def find(id)
first(params: { resource_class.id_field => id }) || handle_not_found(id)
end
|
#first(params: {}) ⇒ Object
30
31
32
33
|
# File 'lib/lightspeed/collection.rb', line 30
def first(params: {})
params = params.merge(limit: 1)
instantiate(get(params: params)).first
end
|
#first_loaded ⇒ Object
50
51
52
|
# File 'lib/lightspeed/collection.rb', line 50
def first_loaded
each_loaded.first
end
|
#inspect ⇒ Object
120
121
122
|
# File 'lib/lightspeed/collection.rb', line 120
def inspect
"#<#{self.class.name} API#{base_path}>"
end
|
#load_relations_default ⇒ Object
139
140
141
|
# File 'lib/lightspeed/collection.rb', line 139
def load_relations_default
'all'
end
|
#page(n, per_page: PER_PAGE, params: {}) ⇒ Object
134
135
136
137
|
# File 'lib/lightspeed/collection.rb', line 134
def page(n, per_page: PER_PAGE, params: {})
params = params.merge(limit: per_page, offset: per_page * n)
instantiate(get(params: params))
end
|
#size(params: {}) ⇒ Object
Also known as:
length
35
36
37
38
|
# File 'lib/lightspeed/collection.rb', line 35
def size(params: {})
params = params.merge(limit: 1, load_relations: nil)
get(params: params)['@attributes']['count'].to_i
end
|
#size_loaded ⇒ Object
54
55
56
|
# File 'lib/lightspeed/collection.rb', line 54
def size_loaded
@resources.size
end
|
#to_json ⇒ Object
130
131
132
|
# File 'lib/lightspeed/collection.rb', line 130
def to_json
Yajl::Encoder.encode(as_json)
end
|
#unload ⇒ Object
21
22
23
|
# File 'lib/lightspeed/collection.rb', line 21
def unload
@resources = {}
end
|
#update(id, attributes = {}) ⇒ Object
96
97
98
|
# File 'lib/lightspeed/collection.rb', line 96
def update(id, attributes = {})
instantiate(put(id, body: Yajl::Encoder.encode(attributes))).first
end
|