Module: Moneybird::Traits::FindAll

Instance Method Summary collapse

Instance Method Details

#all(params = {}) ⇒ Object



4
5
6
# File 'lib/moneybird/traits/find_all.rb', line 4

def all(params = {})
  each(params).to_a
end

#each(params = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/moneybird/traits/find_all.rb', line 15

def each(params = {})
  return enum_for(:each, params) unless block_given?
  if preloaded_data
    preloaded_data.each do |data|
      yield data
    end
  else
    each_page(params).each do |resources|
      resources.each do |resource|
        yield resource
      end
    end
  end
end

#each_page(params = {}) ⇒ Object



8
9
10
11
12
13
# File 'lib/moneybird/traits/find_all.rb', line 8

def each_page(params = {})
  return enum_for(:each_page, params) unless block_given?
  client.get_each_page(path, params).map do |resources|
    yield resources.map { |resource| build resource }
  end
end

#first(params = {}) ⇒ Object



36
37
38
39
# File 'lib/moneybird/traits/find_all.rb', line 36

def first(params = {})
  params = { per_page: 1 }.merge params
  build client.get(path, params).first
end

#first_page(params = {}) ⇒ Object



30
31
32
33
34
# File 'lib/moneybird/traits/find_all.rb', line 30

def first_page(params = {})
  client.get(path, params).map do |resource|
    build resource
  end
end

#map(params = {}) ⇒ Object



41
42
43
44
45
46
# File 'lib/moneybird/traits/find_all.rb', line 41

def map(params = {})
  return enum_for(:map, params) unless block_given?
  each(params).map do |resource|
    yield resource
  end
end