Class: Nylas::RestfulModelCollection

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

Direct Known Subclasses

ManagementModelCollection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class, api, filters = {}) ⇒ RestfulModelCollection

Returns a new instance of RestfulModelCollection.

Raises:

  • (StandardError)


8
9
10
11
12
13
# File 'lib/restful_model_collection.rb', line 8

def initialize(model_class, api, filters = {})
  raise StandardError.new unless api.class <= Nylas::API
  @model_class = model_class
  @filters = filters
  @_api = api
end

Instance Attribute Details

#filters ⇒ Object

Returns the value of attribute filters.



6
7
8
# File 'lib/restful_model_collection.rb', line 6

def filters
  @filters
end

Instance Method Details

#all ⇒ Object



36
37
38
# File 'lib/restful_model_collection.rb', line 36

def all
  range(0, Float::INFINITY)
end

#build(args) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/restful_model_collection.rb', line 74

def build(args)
  for key in args.keys
    args[key.to_s] = args[key]
  end
  model = @model_class.new(@_api)
  model.inflate(args)
  model
end

#count ⇒ Object



25
26
27
28
29
30
# File 'lib/restful_model_collection.rb', line 25

def count
  RestClient.get(url, params: @filters.merge(view: 'count')) { |response,request,result|
    json = Nylas.interpret_response(result, response)
    return json['count']
  }
end

#delete(item_or_id) ⇒ Object



64
65
66
67
# File 'lib/restful_model_collection.rb', line 64

def delete(item_or_id)
  item_or_id = item_or_id.id if item_or_id.is_a?(RestfulModel)
  RestClient.delete("#{url}/#{item_or_id}")
end

#each ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/restful_model_collection.rb', line 15

def each
  return enum_for(:each) unless block_given?

  get_model_collection do |items|
    items.each do |item|
      yield item
    end
  end
end

#find(id) ⇒ Object



69
70
71
72
# File 'lib/restful_model_collection.rb', line 69

def find(id)
  return nil unless id
  get_model(id)
end

#first ⇒ Object



32
33
34
# File 'lib/restful_model_collection.rb', line 32

def first
  get_model_collection.first
end

#inflate_collection(items = []) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/restful_model_collection.rb', line 83

def inflate_collection(items = [])
  models = []

  return unless items.is_a?(Array)
  items.each do |json|
    if @model_class < RestfulModel
      model = @model_class.new(@_api)
      model.inflate(json)
    else
      model = @model_class.new(json)
    end
    models.push(model)
  end
  models
end

#range(offset = 0, limit = 100) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/restful_model_collection.rb', line 56

def range(offset = 0, limit = 100)

  accumulated = get_model_collection(offset, limit)

  accumulated = accumulated[0..limit] if limit < Float::INFINITY
  accumulated
end

#url ⇒ Object



99
100
101
# File 'lib/restful_model_collection.rb', line 99

def url
  @_api.url_for_path("/#{@model_class.collection_name}")
end

#where(filters) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/restful_model_collection.rb', line 40

def where(filters)
  collection = self.clone

  # deep copy the object, otherwise filter is shared among all
  # the instances of the collection, which leads to confusing behaviour.
  # - karim
  if collection.filters == nil
    collection.filters = {}
  else
    collection.filters = Marshal.load(Marshal.dump(collection.filters))
  end

  collection.filters.merge!(filters)
  collection
end