Class: Cyrax::Repository
- Inherits:
-
Object
- Object
- Cyrax::Repository
- Includes:
- HasActiveLogger::Mixin
- Defined in:
- lib/cyrax/repository.rb
Instance Attribute Summary collapse
-
#accessor ⇒ Object
Returns the value of attribute accessor.
-
#finders ⇒ Object
Returns the value of attribute finders.
-
#options ⇒ Object
Returns the value of attribute options.
-
#params ⇒ Object
Returns the value of attribute params.
-
#resource_class ⇒ Object
Returns the value of attribute resource_class.
Instance Method Summary collapse
-
#build(id, attributes = {}) ⇒ object
Instantiates the resource.
- #build!(id, attributes = {}) ⇒ Object
- #default_attributes ⇒ Object
- #default_attributes! ⇒ Object
-
#delete(resource) ⇒ Object
Removes a resource Calls destroy method on resource.
- #delete!(resource) ⇒ Object
-
#find(id) ⇒ object
Finds and returns a single item from the DB.
- #find!(id) ⇒ Object
-
#find_all ⇒ Array
Finds and returns a multiple items within the scope from the DB.
- #find_all! ⇒ Object
- #finder(name, *attrs) ⇒ Object
- #finder?(name) ⇒ Boolean
- #finder_or_run(name, *attrs) ⇒ Object
-
#initialize(options = {}) ⇒ Repository
constructor
A new instance of Repository.
- #method_missing(method, *args, &block) ⇒ Object
-
#save(resource) ⇒ Object
Saves a resource.
- #save!(resource) ⇒ Object
-
#scope ⇒ Object
Returns the resource class - e.g.
- #scope! ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Repository
Returns a new instance of Repository.
9 10 11 12 13 14 15 |
# File 'lib/cyrax/repository.rb', line 9 def initialize( = {}) @options = @accessor = [:as] @params = [:params] @resource_class = [:resource_class] @finders = [:finders] || {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
109 110 111 112 |
# File 'lib/cyrax/repository.rb', line 109 def method_missing(method, *args, &block) return super unless finder?(method) finder(method, *args) end |
Instance Attribute Details
#accessor ⇒ Object
Returns the value of attribute accessor.
4 5 6 |
# File 'lib/cyrax/repository.rb', line 4 def accessor @accessor end |
#finders ⇒ Object
Returns the value of attribute finders.
7 8 9 |
# File 'lib/cyrax/repository.rb', line 7 def finders @finders end |
#options ⇒ Object
Returns the value of attribute options.
3 4 5 |
# File 'lib/cyrax/repository.rb', line 3 def @options end |
#params ⇒ Object
Returns the value of attribute params.
5 6 7 |
# File 'lib/cyrax/repository.rb', line 5 def params @params end |
#resource_class ⇒ Object
Returns the value of attribute resource_class.
6 7 8 |
# File 'lib/cyrax/repository.rb', line 6 def resource_class @resource_class end |
Instance Method Details
#build(id, attributes = {}) ⇒ object
Instantiates the resource
51 52 53 |
# File 'lib/cyrax/repository.rb', line 51 def build(id, attributes = {}) finder_or_run(:build, id, attributes) end |
#build!(id, attributes = {}) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# 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 ⇒ Object
102 103 104 |
# File 'lib/cyrax/repository.rb', line 102 def default_attributes finder_or_run(:default_attributes) end |
#default_attributes! ⇒ Object
105 106 107 |
# File 'lib/cyrax/repository.rb', line 105 def default_attributes! {} end |
#delete(resource) ⇒ Object
Removes a resource Calls destroy method on resource
95 96 97 |
# File 'lib/cyrax/repository.rb', line 95 def delete(resource) finder_or_run(:delete, resource) end |
#delete!(resource) ⇒ Object
98 99 100 |
# File 'lib/cyrax/repository.rb', line 98 def delete!(resource) resource.destroy end |
#find(id) ⇒ object
Finds and returns a single item from the DB
76 77 78 |
# File 'lib/cyrax/repository.rb', line 76 def find(id) finder_or_run(:find, id) end |
#find!(id) ⇒ Object
79 80 81 |
# File 'lib/cyrax/repository.rb', line 79 def find!(id) scope.find(id) end |
#find_all ⇒ Array
Finds and returns a multiple items within the scope from the DB
66 67 68 |
# File 'lib/cyrax/repository.rb', line 66 def find_all finder_or_run(:find_all) end |
#find_all! ⇒ Object
69 70 71 |
# 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) ⇒ Object
17 18 19 20 |
# File 'lib/cyrax/repository.rb', line 17 def finder(name, *attrs) block = finders[name] instance_exec(*attrs, &block) end |
#finder?(name) ⇒ Boolean
22 23 24 |
# File 'lib/cyrax/repository.rb', line 22 def finder?(name) finders.has_key?(name) end |
#finder_or_run(name, *attrs) ⇒ Object
26 27 28 |
# File 'lib/cyrax/repository.rb', line 26 def finder_or_run(name, *attrs) finder?(name) ? finder(name, *attrs) : send("#{name}!", *attrs) end |
#save(resource) ⇒ Object
Saves a resource
85 86 87 |
# File 'lib/cyrax/repository.rb', line 85 def save(resource) finder_or_run(:save, resource) end |
#save!(resource) ⇒ Object
88 89 90 |
# File 'lib/cyrax/repository.rb', line 88 def save!(resource) resource.save end |
#scope ⇒ Object
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
40 41 42 |
# File 'lib/cyrax/repository.rb', line 40 def scope finder_or_run(:scope) end |
#scope! ⇒ Object
43 44 45 |
# File 'lib/cyrax/repository.rb', line 43 def scope! resource_class end |