Class: Tickethub::Collection

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

Constant Summary collapse

DEFAULT_LIMIT =
25.freeze
DEFAULT_OFFSET =
0.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Collection.



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
40
41
42
43
# File 'lib/tickethub/collection.rb', line 13

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.collection_methods.merge(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.call(endpoint, row)
    end

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

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



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

def cache
  @cache
end

#countObject (readonly)

Returns the value of attribute count.



11
12
13
# File 'lib/tickethub/collection.rb', line 11

def count
  @count
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



11
12
13
# File 'lib/tickethub/collection.rb', line 11

def endpoint
  @endpoint
end

#paramsObject (readonly)

Returns the value of attribute params.



11
12
13
# File 'lib/tickethub/collection.rb', line 11

def params
  @params
end

Instance Method Details

#[](search) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/tickethub/collection.rb', line 134

def [](search)
  case search
  when Fixnum
    self.offset(search).first
  when Hash
    self.filter(search).first
  when Range
    self.offset(search.min).first(search.max)
  when Array
    self.filter(id: search)
  when String
    endpoint = self.endpoint[@klass.path, CGI::escape(search)]
    @klass.call endpoint, endpoint.get(params)
  else
    raise ArgumentError, 'invalid search value type'
  end
end

#any?(&block) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/tickethub/collection.rb', line 125

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

#create(attributes = {}) ⇒ Object



152
153
154
155
156
# File 'lib/tickethub/collection.rb', line 152

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

#empty?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/tickethub/collection.rb', line 121

def empty?
  count.zero?
end

#filter(value) ⇒ Object



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

def filter(value)
  self.class.new endpoint, @klass,
    params.merge(filters: filters.merge(value))
end

#filtersObject



81
82
83
# File 'lib/tickethub/collection.rb', line 81

def filters
  (params[:filters] || {}).dup
end

#lastObject



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

def last
  offset(count - 1).first
end

#limit(value = nil) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/tickethub/collection.rb', line 58

def limit(value = nil)
  if value.nil?
    return (@limit || params[:limit] || DEFAULT_LIMIT).to_i
  else
    self.class.new endpoint, @klass,
      params.merge(limit: value)
  end
end

#offset(value = nil) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/tickethub/collection.rb', line 67

def offset(value = nil)
  if value.nil?
    return (@offset || params[:offset] || DEFAULT_OFFSET).to_i
  else
    self.class.new endpoint, @klass,
      params.merge(offset: value)
  end
end

#order(value = nil) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/tickethub/collection.rb', line 89

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



45
46
47
48
49
50
51
52
# File 'lib/tickethub/collection.rb', line 45

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]
  return self
end

#scope(path, params = {}) ⇒ Object



103
104
105
106
# File 'lib/tickethub/collection.rb', line 103

def scope(path, params = {})
  self.class.new endpoint[path], @klass,
    self.params.merge(params)
end

#search(value) ⇒ Object



85
86
87
# File 'lib/tickethub/collection.rb', line 85

def search(value)
  self.filter(search: value)
end