Class: Lightspeed::Collection
- Inherits:
-
Object
- Object
- Lightspeed::Collection
show all
- Defined in:
- lib/lightspeed/collection.rb
Direct Known Subclasses
Accounts, Categories, Employees, Images, Inventories, ItemAttributeSets, ItemMatrices, Items, Orders, 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(*args) ⇒ 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_json(json) ⇒ Object
-
#load_relations_default ⇒ Object
-
#page(n, per_page: PER_PAGE, params: {}) ⇒ Object
-
#size(params: {}) ⇒ Object
(also: #length)
-
#size_loaded ⇒ Object
-
#to_json(*args) ⇒ 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
108
109
110
|
# File 'lib/lightspeed/collection.rb', line 108
def self.collection_name
name.demodulize
end
|
.resource_class ⇒ Object
116
117
118
|
# File 'lib/lightspeed/collection.rb', line 116
def self.resource_class
"Lightspeed::#{resource_name}".constantize
end
|
.resource_name ⇒ Object
112
113
114
|
# File 'lib/lightspeed/collection.rb', line 112
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
62
63
64
|
# File 'lib/lightspeed/collection.rb', line 62
def all(params: {})
enum_page(params: params).to_a.flatten(1)
end
|
#all_loaded ⇒ Object
50
51
52
|
# File 'lib/lightspeed/collection.rb', line 50
def all_loaded
each_loaded.to_a
end
|
#as_json(*args) ⇒ Object
Also known as:
to_h
128
129
130
131
|
# File 'lib/lightspeed/collection.rb', line 128
def as_json(*args)
return if all_loaded.empty?
{ resource_name => all_loaded.as_json(*args) }
end
|
#base_path ⇒ Object
120
121
122
|
# File 'lib/lightspeed/collection.rb', line 120
def base_path
"#{account.base_path}/#{resource_name}"
end
|
#client ⇒ Object
29
30
31
32
|
# File 'lib/lightspeed/collection.rb', line 29
def client
return context if context.is_a?(Lightspeed::Client)
account.client
end
|
#create(attributes = {}) ⇒ Object
96
97
98
|
# File 'lib/lightspeed/collection.rb', line 96
def create(attributes = {})
instantiate(post(body: attributes.to_json)).first
end
|
#destroy(id) ⇒ Object
104
105
106
|
# File 'lib/lightspeed/collection.rb', line 104
def destroy(id)
instantiate(delete(id)).first
end
|
#each(per_page: PER_PAGE, params: {}, &block) ⇒ Object
88
89
90
|
# File 'lib/lightspeed/collection.rb', line 88
def each(per_page: PER_PAGE, params: {}, &block)
enum(per_page: per_page, params: params).each(&block)
end
|
#each_loaded ⇒ Object
45
46
47
48
|
# File 'lib/lightspeed/collection.rb', line 45
def each_loaded
@resources ||= {}
@resources.each_value
end
|
#each_page(per_page: PER_PAGE, params: {}, &block) ⇒ Object
66
67
68
|
# File 'lib/lightspeed/collection.rb', line 66
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
80
81
82
83
84
85
86
|
# File 'lib/lightspeed/collection.rb', line 80
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
70
71
72
73
74
75
76
77
78
|
# File 'lib/lightspeed/collection.rb', line 70
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
92
93
94
|
# File 'lib/lightspeed/collection.rb', line 92
def find(id)
first(params: { resource_class.id_field => id }) || handle_not_found(id)
end
|
#first(params: {}) ⇒ Object
34
35
36
37
|
# File 'lib/lightspeed/collection.rb', line 34
def first(params: {})
params = params.merge(limit: 1)
instantiate(get(params: params)).first
end
|
#first_loaded ⇒ Object
54
55
56
|
# File 'lib/lightspeed/collection.rb', line 54
def first_loaded
each_loaded.first
end
|
#inspect ⇒ Object
124
125
126
|
# File 'lib/lightspeed/collection.rb', line 124
def inspect
"#<#{self.class.name} API#{base_path}>"
end
|
#load_json(json) ⇒ Object
25
26
27
|
# File 'lib/lightspeed/collection.rb', line 25
def load_json(json)
instantiate(JSON.parse(json))
end
|
#load_relations_default ⇒ Object
143
144
145
|
# File 'lib/lightspeed/collection.rb', line 143
def load_relations_default
'all'
end
|
#page(n, per_page: PER_PAGE, params: {}) ⇒ Object
138
139
140
141
|
# File 'lib/lightspeed/collection.rb', line 138
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
39
40
41
42
|
# File 'lib/lightspeed/collection.rb', line 39
def size(params: {})
params = params.merge(limit: 1, load_relations: nil)
get(params: params)['@attributes']['count'].to_i
end
|
#size_loaded ⇒ Object
58
59
60
|
# File 'lib/lightspeed/collection.rb', line 58
def size_loaded
@resources.size
end
|
#to_json(*args) ⇒ Object
134
135
136
|
# File 'lib/lightspeed/collection.rb', line 134
def to_json(*args)
as_json.to_json(*args)
end
|
#unload ⇒ Object
21
22
23
|
# File 'lib/lightspeed/collection.rb', line 21
def unload
@resources = {}
end
|
#update(id, attributes = {}) ⇒ Object
100
101
102
|
# File 'lib/lightspeed/collection.rb', line 100
def update(id, attributes = {})
instantiate(put(id, body: attributes.to_json)).first
end
|