Class: Strobe::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/strobe/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Collection.



7
8
9
# File 'lib/strobe/collection.rb', line 7

def initialize(klass, params = {})
  @klass, @params = klass, params
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



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

def klass
  @klass
end

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  if Array === other
    to_a === other
  else
    super
  end
end

#[](index) ⇒ Object



33
34
35
# File 'lib/strobe/collection.rb', line 33

def [](index)
  to_a[index]
end

#allObject



29
30
31
# File 'lib/strobe/collection.rb', line 29

def all
  self
end

#eachObject



25
26
27
# File 'lib/strobe/collection.rb', line 25

def each
  to_a.each { |r| yield r }
end

#lengthObject



37
38
39
# File 'lib/strobe/collection.rb', line 37

def length
  to_a.length
end

#new(params = {}) ⇒ Object



41
42
43
# File 'lib/strobe/collection.rb', line 41

def new(params = {})
  klass.new(@params.merge(params))
end

#to_aObject



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/strobe/collection.rb', line 11

def to_a
  @to_a ||= begin
    resp = Strobe.connection.get klass.resource_uri, params

    if resp.status == 200
      resp.body[klass.resource_name].map do |hash|
        klass.new(hash)
      end
    else
      raise "Something went wrong"
    end
  end
end