class Cyrax::Repository

Attributes

accessor[RW]
finders[RW]
options[RW]
params[RW]
resource_class[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/cyrax/repository.rb, line 9
def initialize(options = {})
  @options = options
  @accessor = options[:as]
  @params = options[:params]
  @resource_class = options[:resource_class]
  @finders = options[:finders] || {}
end

Public Instance Methods

build(id, attributes = {}) click to toggle source

Instantiates the resource @param id [int] ID or nil if you want a new object @param attributes [hash] Attributes you want to add to the resource @return [object] The object

# File lib/cyrax/repository.rb, line 51
def build(id, attributes = {})
  finder_or_run(:build, id, attributes)
end
build!(id, attributes = {}) click to toggle source
# File lib/cyrax/repository.rb, line 54
def build!(id, attributes = {})
  if id.present?
    resource = find(id)
    resource.attributes = attributes
    resource
  else
    scope.new(default_attributes.merge(attributes))
  end
end
default_attributes() click to toggle source
# File lib/cyrax/repository.rb, line 102
def default_attributes
  finder_or_run(:default_attributes)
end
default_attributes!() click to toggle source
# File lib/cyrax/repository.rb, line 105
def default_attributes!
  {}
end
delete(resource) click to toggle source

Removes a resource Calls destroy method on resource @param resource [object] The resource to destroy

# File lib/cyrax/repository.rb, line 95
def delete(resource)
  finder_or_run(:delete, resource)
end
delete!(resource) click to toggle source
# File lib/cyrax/repository.rb, line 98
def delete!(resource)
  resource.destroy
end
find(id) click to toggle source

Finds and returns a single item from the DB @param id [int] ID of item @return [object] The object

# File lib/cyrax/repository.rb, line 76
def find(id)
  finder_or_run(:find, id)
end
find!(id) click to toggle source
# File lib/cyrax/repository.rb, line 79
def find!(id)
  scope.find(id)
end
find_all() click to toggle source

Finds and returns a multiple items within the scope from the DB @return [Array] Array of objects

# File lib/cyrax/repository.rb, line 66
def find_all
  finder_or_run(:find_all)
end
find_all!() click to toggle source
# File lib/cyrax/repository.rb, line 69
def find_all!
  defined?(ActiveRecord) && scope.is_a?(ActiveRecord::Relation) ? scope.load : scope.all
end
finder(name, *attrs) click to toggle source
# File lib/cyrax/repository.rb, line 17
def finder(name, *attrs)
  block = finders[name]
  instance_exec(*attrs, &block)
end
finder?(name) click to toggle source
# File lib/cyrax/repository.rb, line 22
def finder?(name)
  finders.has_key?(name)
end
finder_or_run(name, *attrs) click to toggle source
# File lib/cyrax/repository.rb, line 26
def finder_or_run(name, *attrs)
  finder?(name) ? finder(name, *attrs) : send("#{name}!", *attrs)
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/cyrax/repository.rb, line 109
def method_missing(method, *args, &block)
  return super unless finder?(method)
  finder(method, *args)
end
save(resource) click to toggle source

Saves a resource @param resource [object] The resource to save

# File lib/cyrax/repository.rb, line 85
def save(resource)
  finder_or_run(:save, resource)
end
save!(resource) click to toggle source
# File lib/cyrax/repository.rb, line 88
def save!(resource)
  resource.save
end
scope() click to toggle source

Returns the resource class - e.g. Product by default. If you want your repository to scope with something interesting, you should override this in your repository by defining the method and returning your own scope

@example Overriding scope

class Products::Repository < Cyrax::Repository
  def scope
    accessor.products
  end
end
# File lib/cyrax/repository.rb, line 40
def scope
  finder_or_run(:scope)
end
scope!() click to toggle source
# File lib/cyrax/repository.rb, line 43
def scope!
  resource_class
end