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.



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

#contextObject

Returns the value of attribute context.



8
9
10
# File 'lib/lightspeed/collection.rb', line 8

def context
  @context
end

#resourcesObject

Returns the value of attribute resources.



8
9
10
# File 'lib/lightspeed/collection.rb', line 8

def resources
  @resources
end

Class Method Details

.collection_nameObject



102
103
104
# File 'lib/lightspeed/collection.rb', line 102

def self.collection_name
  name.demodulize
end

.resource_classObject



110
111
112
# File 'lib/lightspeed/collection.rb', line 110

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

.resource_nameObject



106
107
108
# File 'lib/lightspeed/collection.rb', line 106

def self.resource_name
  collection_name.singularize
end

Instance Method Details

#accountObject



15
16
17
# File 'lib/lightspeed/collection.rb', line 15

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



44
45
46
# File 'lib/lightspeed/collection.rb', line 44

def all_loaded
  each_loaded.to_a
end

#as_jsonObject 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_pathObject



114
115
116
# File 'lib/lightspeed/collection.rb', line 114

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

#clientObject



23
24
25
26
# File 'lib/lightspeed/collection.rb', line 23

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



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_loadedObject



48
49
50
# File 'lib/lightspeed/collection.rb', line 48

def first_loaded
  each_loaded.first
end

#inspectObject



118
119
120
# File 'lib/lightspeed/collection.rb', line 118

def inspect
  "#<#{self.class.name} API#{base_path}>"
end

#load_relations_defaultObject



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_loadedObject



52
53
54
# File 'lib/lightspeed/collection.rb', line 52

def size_loaded
  @resources.size
end

#to_jsonObject



128
129
130
# File 'lib/lightspeed/collection.rb', line 128

def to_json
  Yajl::Encoder.encode(as_json)
end

#unloadObject



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