Module: Strobe::Resource::Collection::ClassMethods

Includes:
Enumerable
Defined in:
lib/strobe/resource/collection.rb

Instance Method Summary collapse

Instance Method Details

#collectionObject Also known as: all


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

def collection
  Strobe::Collection.new(self)
end

#collection_uriObject


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

def collection_uri
  [ uri_prefix, include_query_string ].compact.join('?')
end

#create(params = {}) ⇒ Object


60
61
62
# File 'lib/strobe/resource/collection.rb', line 60

def create(params = {})
  new(params).tap { |inst| inst.save }
end

#create!(params = {}) ⇒ Object


64
65
66
# File 'lib/strobe/resource/collection.rb', line 64

def create!(params = {})
  new(params).tap { |inst| inst.save! }
end

#denormalize_params(params) ⇒ Object


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

def denormalize_params(params)
  if coll = params.delete(pluralized_root)
    coll.map do |item|
      associations.each do |n, assoc|
        item = assoc.denormalize_params(item, params)
      end
      item
    end
  elsif item = params.delete(root)
    associations.each do |n, assoc|
      item = assoc.denormalize_params(item, params)
    end
    item
  end
end

#eachObject


18
19
20
# File 'lib/strobe/resource/collection.rb', line 18

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

#get(id, opts = {}) ⇒ Object


68
69
70
71
72
73
74
75
76
# File 'lib/strobe/resource/collection.rb', line 68

def get(id, opts = {})
  return nil unless id

  IdentityMap.identify self, id do
    inst = new :id => id
    break inst if opts[:lazy]
    inst.reload(opts) && inst
  end
end

#get!(id, opts = {}) ⇒ Object


78
79
80
81
82
83
84
85
86
# File 'lib/strobe/resource/collection.rb', line 78

def get!(id, opts = {})
  raise ResourceNotFoundError, "can not find resource will nil key" unless id

  IdentityMap.identify self, id do
    inst = new :id => id
    inst.reload!
    inst
  end
end

#key(property = nil, type = Integer) ⇒ Object


10
11
12
13
14
15
16
# File 'lib/strobe/resource/collection.rb', line 10

def key(property = nil, type = Integer)
  @key ||= Key.new(:id, Integer)

  return @key unless property

  @key = Key.new(property, type)
end

#new(params = {}) ⇒ Object


52
53
54
55
56
57
58
# File 'lib/strobe/resource/collection.rb', line 52

def new(params = {})
  inst = IdentityMap.identify(self, key.key_for(params)) do
    super({})
  end
  inst.merge!(params)
  inst
end

#pluralized_rootObject


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

def pluralized_root
  root.pluralize
end

#resource_nameObject


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

def resource_name
  singular_resource_name.pluralize
end

#resource_uri(id) ⇒ Object


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

def resource_uri(id)
  [ "#{uri_prefix}/#{id}", include_query_string ].compact.join('?')
end

#singular_resource_nameObject


22
23
24
# File 'lib/strobe/resource/collection.rb', line 22

def singular_resource_name
  base_const_name.underscore
end

#where(params) ⇒ Object


48
49
50
# File 'lib/strobe/resource/collection.rb', line 48

def where(params)
  Strobe::Collection.new(self, params)
end