Class: Tickethub::Collection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, klass, params = {}) ⇒ Collection

Returns a new instance of Collection.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tickethub/collection.rb', line 8

def initialize(endpoint, klass, params = {})
  @params = params.dup
  @endpoint = endpoint
  @klass = klass

  klass.registered_types.each do |type, options|
    define_singleton_method type do
      instance_variable_defined?("@#{type}") ? instance_variable_get("@#{type}") :
        instance_variable_set("@#{type}", Tickethub::Collection.new(@endpoint[type], options[:klass]))
    end
  end

  klass.scopes.each do |key, block|
    define_singleton_method key, &block
  end

  super() do |yielder|
    self.reload! if @cache.nil?

    @cache.each do |row|
      yielder << @klass.load(@endpoint, row)
    end

    while @cache.length < @count
      offset = @offset + @cache.length
      response = @endpoint.get @params.merge(offset: @cache.length)
      response.decoded.each do |row| @cache << row
        yielder << @klass.load(@endpoint, row)
      end
    end
  end
end

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



5
6
7
# File 'lib/tickethub/collection.rb', line 5

def cache
  @cache
end

#countObject (readonly)

Returns the value of attribute count.



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

def count
  @count
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



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

def endpoint
  @endpoint
end

Instance Method Details

#[](*args) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/tickethub/collection.rb', line 127

def [](*args)
  case (key = args[0])
  when Fixnum
    self.offset(key).first
  when Hash
    self.filter(key).first
  when Range
    self.offset(key.min).first(key.max)
  when String
    options = { params: args[1] || {} }.merge args[2] || {}
    @klass.load @klass.endpoint[key, @endpoint[nil, options].options]
  else
    raise ArgumentError, 'invalid search value type'
  end
end

#any?(&block) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/tickethub/collection.rb', line 113

def any?(&block)
  block_given?? super : ! empty?
end

#create(attributes = {}) ⇒ Object



143
144
145
146
147
# File 'lib/tickethub/collection.rb', line 143

def create(attributes = {})
  @klass.load @endpoint, @endpoint.post(attributes).decoded
rescue Tickethub::ResourceInvalid => err
  @klass.load @endpoint, Tickethub::Response.new(err.response).decoded
end

#empty?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/tickethub/collection.rb', line 109

def empty?
  count.zero?
end

#filter(value) ⇒ Object



73
74
75
76
# File 'lib/tickethub/collection.rb', line 73

def filter(value)
  self.class.new @endpoint, @klass,
    @params.merge(filters: (@params[:filters] || {}).merge(value))
end

#filtersObject



78
79
80
# File 'lib/tickethub/collection.rb', line 78

def filters
  @params[:filters].dup
end

#lastObject



49
50
51
# File 'lib/tickethub/collection.rb', line 49

def last
  offset(count - 1).first
end

#limitObject



53
54
55
56
57
58
59
60
61
# File 'lib/tickethub/collection.rb', line 53

def limit(value = nil)
  if value.nil?
    reload! if @limit.nil?
    return @limit
  else
    self.class.new @endpoint, @klass,
      @params.merge(limit: value)
  end
end

#offset(value = nil) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/tickethub/collection.rb', line 63

def offset(value = nil)
  if value.nil?
    reload! if @offset.nil?
    return @offset
  else
    self.class.new @endpoint, @klass,
      @params.merge(offset: value)
  end
end

#order(value = nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/tickethub/collection.rb', line 82

def order(value = nil)
  return @params[:order].dup if value.nil?
  order = (@params[:order] || []) + (case value
    when Symbol, String then [value.to_s]
    when Hash
      value.collect do |key, direction|
        direction.to_s == 'desc' ? "-#{key}" : key
      end
    end)

  self.class.new @endpoint, @klass,
    @params.merge(order: order)
end

#reload!Object



41
42
43
44
45
46
47
# File 'lib/tickethub/collection.rb', line 41

def reload!
  @cache = (response = @endpoint.get @params).decoded
  @count, @offset, @limit =
    response.status == 206 ? 
      response.headers.values_at(*%w(x-total-count x-offset x-limit))
        .collect { |value| value[0].to_i } : [@cache.length, 0, @cache.length]
end

#scope(key, params = {}, options = {}) ⇒ Object



149
150
151
152
# File 'lib/tickethub/collection.rb', line 149

def scope(key, params = {}, options = {})
  Tickethub::Collection.new @endpoint[key, options.merge(params: params)], @klass,
    filters: @filters, order: @order
end