Class: Strobe::Collection
- Inherits:
-
Object
- Object
- Strobe::Collection
- Includes:
- Enumerable
- Defined in:
- lib/strobe/collection.rb
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](index) ⇒ Object
- #all ⇒ Object
- #create(params = {}) ⇒ Object
- #create!(params = {}) ⇒ Object
- #each ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(klass, params = {}) ⇒ Collection
constructor
A new instance of Collection.
- #length ⇒ Object
- #new(params = {}) ⇒ Object
- #reload! ⇒ Object
- #to_a ⇒ Object
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
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
5 6 7 |
# File 'lib/strobe/collection.rb', line 5 def klass @klass end |
#params ⇒ Object (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
58 59 60 61 62 63 64 |
# File 'lib/strobe/collection.rb', line 58 def ==(other) if Array === other to_a === other else super end end |
#[](index) ⇒ Object
38 39 40 |
# File 'lib/strobe/collection.rb', line 38 def [](index) to_a[index] end |
#all ⇒ Object
34 35 36 |
# File 'lib/strobe/collection.rb', line 34 def all self end |
#create(params = {}) ⇒ Object
50 51 52 |
# File 'lib/strobe/collection.rb', line 50 def create(params = {}) klass.create(@params.merge(params)) end |
#create!(params = {}) ⇒ Object
54 55 56 |
# File 'lib/strobe/collection.rb', line 54 def create!(params = {}) klass.create!(@params.merge(params)) end |
#each ⇒ Object
26 27 28 |
# File 'lib/strobe/collection.rb', line 26 def each to_a.each { |r| yield r } end |
#empty? ⇒ Boolean
30 31 32 |
# File 'lib/strobe/collection.rb', line 30 def empty? to_a.empty? end |
#length ⇒ Object
42 43 44 |
# File 'lib/strobe/collection.rb', line 42 def length to_a.length end |
#new(params = {}) ⇒ Object
46 47 48 |
# File 'lib/strobe/collection.rb', line 46 def new(params = {}) klass.new(@params.merge(params)) end |
#reload! ⇒ Object
66 67 68 |
# File 'lib/strobe/collection.rb', line 66 def reload! @to_a = nil end |
#to_a ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/strobe/collection.rb', line 11 def to_a @to_a ||= begin resp = Strobe.connection.get klass.resource_uri, params resp.validate! if resp.status == 200 resp.body[klass.resource_name].map do |hash| klass.new(hash) end else raise "Something went wrong" end end end |