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.
10
11
12
13
|
# File 'lib/lightspeed/collection.rb', line 10
def initialize(context:, attributes: nil)
self.context = context
instantiate(attributes)
end
|
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
8
9
10
|
# File 'lib/lightspeed/collection.rb', line 8
def context
@context
end
|
#resources ⇒ Object
Returns the value of attribute resources.
8
9
10
|
# File 'lib/lightspeed/collection.rb', line 8
def resources
@resources
end
|
Class Method Details
.collection_name ⇒ Object
102
103
104
|
# File 'lib/lightspeed/collection.rb', line 102
def self.collection_name
name.demodulize
end
|
.resource_class ⇒ Object
110
111
112
|
# File 'lib/lightspeed/collection.rb', line 110
def self.resource_class
"Lightspeed::#{resource_name}".constantize
end
|
.resource_name ⇒ Object
106
107
108
|
# File 'lib/lightspeed/collection.rb', line 106
def self.resource_name
collection_name.singularize
end
|
Instance Method Details
#account ⇒ Object
15
16
17
|
# File 'lib/lightspeed/collection.rb', line 15
def account
context.account
end
|
#all(params: {}) ⇒ Object
56
57
58
|
# File 'lib/lightspeed/collection.rb', line 56
def all(params: {})
enum_page(params: params).to_a.flatten(1)
end
|
#all_loaded ⇒ Object
44
45
46
|
# File 'lib/lightspeed/collection.rb', line 44
def all_loaded
each_loaded.to_a
end
|
#as_json ⇒ Object
Also known as:
to_h
122
123
124
125
|
# File 'lib/lightspeed/collection.rb', line 122
def as_json
return if all_loaded.empty?
{ resource_name => all_loaded.as_json }
end
|
#base_path ⇒ Object
114
115
116
|
# File 'lib/lightspeed/collection.rb', line 114
def base_path
"#{account.base_path}/#{resource_name}"
end
|
#client ⇒ Object
23
24
25
26
|
# File 'lib/lightspeed/collection.rb', line 23
def client
return context if context.is_a?(Lightspeed::Client)
account.client
end
|
#create(attributes = {}) ⇒ Object
90
91
92
|
# File 'lib/lightspeed/collection.rb', line 90
def create(attributes = {})
instantiate(post(body: Yajl::Encoder.encode(attributes))).first
end
|
#destroy(id) ⇒ Object
98
99
100
|
# File 'lib/lightspeed/collection.rb', line 98
def destroy(id)
instantiate(delete(id)).first
end
|
#each(per_page: PER_PAGE, params: {}, &block) ⇒ Object
82
83
84
|
# File 'lib/lightspeed/collection.rb', line 82
def each(per_page: PER_PAGE, params: {}, &block)
enum(per_page: per_page, params: params).each(&block)
end
|
#each_loaded ⇒ Object
39
40
41
42
|
# File 'lib/lightspeed/collection.rb', line 39
def each_loaded
@resources ||= {}
@resources.each_value
end
|
#each_page(per_page: PER_PAGE, params: {}, &block) ⇒ Object
60
61
62
|
# File 'lib/lightspeed/collection.rb', line 60
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
74
75
76
77
78
79
80
|
# File 'lib/lightspeed/collection.rb', line 74
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
64
65
66
67
68
69
70
71
72
|
# File 'lib/lightspeed/collection.rb', line 64
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
86
87
88
|
# File 'lib/lightspeed/collection.rb', line 86
def find(id)
first(params: { resource_class.id_field => id }) || handle_not_found(id)
end
|
#first(params: {}) ⇒ Object
28
29
30
31
|
# File 'lib/lightspeed/collection.rb', line 28
def first(params: {})
params = params.merge(limit: 1)
instantiate(get(params: params)).first
end
|
#first_loaded ⇒ Object
48
49
50
|
# File 'lib/lightspeed/collection.rb', line 48
def first_loaded
each_loaded.first
end
|
#inspect ⇒ Object
118
119
120
|
# File 'lib/lightspeed/collection.rb', line 118
def inspect
"#<#{self.class.name} API#{base_path}>"
end
|
#load_relations_default ⇒ Object
137
138
139
|
# File 'lib/lightspeed/collection.rb', line 137
def load_relations_default
'all'
end
|
#page(n, per_page: PER_PAGE, params: {}) ⇒ Object
132
133
134
135
|
# File 'lib/lightspeed/collection.rb', line 132
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
33
34
35
36
|
# File 'lib/lightspeed/collection.rb', line 33
def size(params: {})
params = params.merge(limit: 1, load_relations: nil)
get(params: params)['@attributes']['count'].to_i
end
|
#size_loaded ⇒ Object
52
53
54
|
# File 'lib/lightspeed/collection.rb', line 52
def size_loaded
@resources.size
end
|
#to_json ⇒ Object
128
129
130
|
# File 'lib/lightspeed/collection.rb', line 128
def to_json
Yajl::Encoder.encode(as_json)
end
|
#unload ⇒ Object
19
20
21
|
# File 'lib/lightspeed/collection.rb', line 19
def unload
@resources = {}
end
|
#update(id, attributes = {}) ⇒ Object
94
95
96
|
# File 'lib/lightspeed/collection.rb', line 94
def update(id, attributes = {})
instantiate(put(id, body: Yajl::Encoder.encode(attributes))).first
end
|