class Cyrax::Repository
Attributes
Public Class Methods
# 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
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
# 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
# File lib/cyrax/repository.rb, line 102 def default_attributes finder_or_run(:default_attributes) end
# File lib/cyrax/repository.rb, line 105 def default_attributes! {} end
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
# File lib/cyrax/repository.rb, line 98 def delete!(resource) resource.destroy end
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
# File lib/cyrax/repository.rb, line 79 def find!(id) scope.find(id) end
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
# File lib/cyrax/repository.rb, line 69 def find_all! defined?(ActiveRecord) && scope.is_a?(ActiveRecord::Relation) ? scope.load : scope.all end
# File lib/cyrax/repository.rb, line 17 def finder(name, *attrs) block = finders[name] instance_exec(*attrs, &block) end
# File lib/cyrax/repository.rb, line 22 def finder?(name) finders.has_key?(name) end
# File lib/cyrax/repository.rb, line 26 def finder_or_run(name, *attrs) finder?(name) ? finder(name, *attrs) : send("#{name}!", *attrs) end
# File lib/cyrax/repository.rb, line 109 def method_missing(method, *args, &block) return super unless finder?(method) finder(method, *args) end
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
# File lib/cyrax/repository.rb, line 88 def save!(resource) resource.save end
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
# File lib/cyrax/repository.rb, line 43 def scope! resource_class end