Class: Lightspeed::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/lightspeed/collection.rb

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

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

#contextObject

Returns the value of attribute context.



10
11
12
# File 'lib/lightspeed/collection.rb', line 10

def context
  @context
end

#resourcesObject

Returns the value of attribute resources.



10
11
12
# File 'lib/lightspeed/collection.rb', line 10

def resources
  @resources
end

Class Method Details

.collection_nameObject



108
109
110
# File 'lib/lightspeed/collection.rb', line 108

def self.collection_name
  name.demodulize
end

.resource_classObject



116
117
118
# File 'lib/lightspeed/collection.rb', line 116

def self.resource_class
  "Lightspeed::#{resource_name}".constantize
end

.resource_nameObject



112
113
114
# File 'lib/lightspeed/collection.rb', line 112

def self.resource_name
  collection_name.singularize
end

Instance Method Details

#accountObject



17
18
19
# File 'lib/lightspeed/collection.rb', line 17

def 
  context.
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_loadedObject



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_pathObject



120
121
122
# File 'lib/lightspeed/collection.rb', line 120

def base_path
  "#{.base_path}/#{resource_name}"
end

#clientObject



29
30
31
32
# File 'lib/lightspeed/collection.rb', line 29

def client
  return context if context.is_a?(Lightspeed::Client)
  .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_loadedObject



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_loadedObject



54
55
56
# File 'lib/lightspeed/collection.rb', line 54

def first_loaded
  each_loaded.first
end

#inspectObject



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_defaultObject



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_loadedObject



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

#unloadObject



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