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 = {}, data = nil) ⇒ 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 = {}, data = nil) ⇒ Collection
Returns a new instance of Collection.
7 8 9 10 |
# File 'lib/strobe/collection.rb', line 7 def initialize(klass, params = {}, data=nil) @klass, @params = klass, params @to_a = data.map { |hash| klass.new(hash) } if data 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
73 74 75 76 77 78 79 |
# File 'lib/strobe/collection.rb', line 73 def ==(other) if Array === other to_a === other else super end end |
#[](index) ⇒ Object
45 46 47 |
# File 'lib/strobe/collection.rb', line 45 def [](index) to_a[index] end |
#all ⇒ Object
41 42 43 |
# File 'lib/strobe/collection.rb', line 41 def all self end |
#create(params = {}) ⇒ Object
65 66 67 |
# File 'lib/strobe/collection.rb', line 65 def create(params = {}) klass.create(@params.merge(params)) end |
#create!(params = {}) ⇒ Object
69 70 71 |
# File 'lib/strobe/collection.rb', line 69 def create!(params = {}) klass.create!(@params.merge(params)) end |
#each ⇒ Object
33 34 35 |
# File 'lib/strobe/collection.rb', line 33 def each to_a.each { |r| yield r } end |
#empty? ⇒ Boolean
37 38 39 |
# File 'lib/strobe/collection.rb', line 37 def empty? to_a.empty? end |
#first ⇒ Object
53 54 55 |
# File 'lib/strobe/collection.rb', line 53 def first to_a.first end |
#last ⇒ Object
57 58 59 |
# File 'lib/strobe/collection.rb', line 57 def last to_a.last end |
#length ⇒ Object
49 50 51 |
# File 'lib/strobe/collection.rb', line 49 def length to_a.length end |
#new(params = {}) ⇒ Object
61 62 63 |
# File 'lib/strobe/collection.rb', line 61 def new(params = {}) klass.new(@params.merge(params)) end |
#reload! ⇒ Object
81 82 83 |
# File 'lib/strobe/collection.rb', line 81 def reload! @to_a = nil end |
#to_a ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/strobe/collection.rb', line 16 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
12 13 14 |
# File 'lib/strobe/collection.rb', line 12 def where(params) Strobe::Collection.new(klass, params) end |