Module: Bustle::Concern::ResourceCollection::ClassMethods

Defined in:
lib/bustle/concerns/resource_collection.rb

Instance Method Summary collapse

Instance Method Details

#add(resource) ⇒ Object



13
14
15
16
17
# File 'lib/bustle/concerns/resource_collection.rb', line 13

def add(resource)
  add!(resource)
rescue
  get(resource)
end

#add!(resource) ⇒ Object



6
7
8
9
10
11
# File 'lib/bustle/concerns/resource_collection.rb', line 6

def add!(resource)
  self::RESOURCE_NAME.constantize.to_adapter.create!(
    :resource_class => resource.class.name,
    :resource_id    => resource.id
  )
end

#get(resource) ⇒ Object



19
20
21
22
23
24
# File 'lib/bustle/concerns/resource_collection.rb', line 19

def get(resource)
  self::RESOURCE_NAME.constantize.to_adapter.find_first(
    :resource_class => resource.class.name,
    :resource_id    => resource.id
  )
end

#remove(resource) ⇒ Object



36
37
38
# File 'lib/bustle/concerns/resource_collection.rb', line 36

def remove(resource)
  get(resource).destroy
end

#remove!(resource) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/bustle/concerns/resource_collection.rb', line 26

def remove!(resource)
  r = get(resource)

  if r.nil?
    raise "#{resource} does not exist therefore cannot be removed."
  else
    r.destroy
  end
end