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
- #first ⇒ Object
-
#initialize(klass, params = {}) ⇒ Collection
constructor
A new instance of Collection.
- #last ⇒ Object
- #length ⇒ Object
- #new(params = {}) ⇒ Object
- #reload! ⇒ Object
- #to_a ⇒ Object
- #where(params) ⇒ 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
72 73 74 75 76 77 78 |
# File 'lib/strobe/collection.rb', line 72 def ==(other) if Array === other to_a === other else super end end |
#[](index) ⇒ Object
44 45 46 |
# File 'lib/strobe/collection.rb', line 44 def [](index) to_a[index] end |
#all ⇒ Object
40 41 42 |
# File 'lib/strobe/collection.rb', line 40 def all self end |
#create(params = {}) ⇒ Object
64 65 66 |
# File 'lib/strobe/collection.rb', line 64 def create(params = {}) klass.create(@params.merge(params)) end |
#create!(params = {}) ⇒ Object
68 69 70 |
# File 'lib/strobe/collection.rb', line 68 def create!(params = {}) klass.create!(@params.merge(params)) end |
#each ⇒ Object
32 33 34 |
# File 'lib/strobe/collection.rb', line 32 def each to_a.each { |r| yield r } end |
#empty? ⇒ Boolean
36 37 38 |
# File 'lib/strobe/collection.rb', line 36 def empty? to_a.empty? end |
#first ⇒ Object
52 53 54 |
# File 'lib/strobe/collection.rb', line 52 def first to_a.first end |
#last ⇒ Object
56 57 58 |
# File 'lib/strobe/collection.rb', line 56 def last to_a.last end |
#length ⇒ Object
48 49 50 |
# File 'lib/strobe/collection.rb', line 48 def length to_a.length end |
#new(params = {}) ⇒ Object
60 61 62 |
# File 'lib/strobe/collection.rb', line 60 def new(params = {}) klass.new(@params.merge(params)) end |
#reload! ⇒ Object
80 81 82 |
# File 'lib/strobe/collection.rb', line 80 def reload! @to_a = nil end |
#to_a ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/strobe/collection.rb', line 15 def to_a @to_a ||= begin resp = Strobe.connection.get klass.collection_uri, params resp.validate! if resp.status == 200 denormalized = klass.denormalize_params(resp.body) denormalized.map do |hash| klass.new(hash) end else raise ServerError.new("Something went wrong", :request => resp.request, :response => resp) end end end |
#where(params) ⇒ Object
11 12 13 |
# File 'lib/strobe/collection.rb', line 11 def where(params) Strobe::Collection.new(klass, params) end |