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



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

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

#create(params = {}) ⇒ Object



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

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

#create!(params = {}) ⇒ Object



52
53
54
# File 'lib/strobe/resource/collection.rb', line 52

def create!(params = {})
  new(params).tap { |inst| inst.save! }
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



56
57
58
59
60
61
62
63
64
# File 'lib/strobe/resource/collection.rb', line 56

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



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

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



40
41
42
43
44
45
46
# File 'lib/strobe/resource/collection.rb', line 40

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

#resource_nameObject



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

def resource_name
  singular_resource_name.pluralize
end

#resource_uriObject



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

def resource_uri
  uri_prefix
end

#singular_resource_nameObject



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

def singular_resource_name
  base_const_name.underscore
end