Class: Cyrax::Repository

Inherits:
Object
  • Object
show all
Includes:
HasActiveLogger::Mixin
Defined in:
lib/cyrax/repository.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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 = {})
  @options = options
  @accessor = options[:as]
  @params = options[:params]
  @resource_class = options[:resource_class]
  @finders = options[: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

#accessorObject

Returns the value of attribute accessor.



4
5
6
# File 'lib/cyrax/repository.rb', line 4

def accessor
  @accessor
end

#findersObject

Returns the value of attribute finders.



7
8
9
# File 'lib/cyrax/repository.rb', line 7

def finders
  @finders
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/cyrax/repository.rb', line 3

def options
  @options
end

#paramsObject

Returns the value of attribute params.



5
6
7
# File 'lib/cyrax/repository.rb', line 5

def params
  @params
end

#resource_classObject

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

Parameters:

  • id (int)

    ID or nil if you want a new object

  • attributes (hash) (defaults to: {})

    Attributes you want to add to the resource

Returns:

  • (object)

    The object



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_attributesObject



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

Parameters:

  • resource (object)

    The resource to destroy



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

Parameters:

  • id (int)

    ID of item

Returns:

  • (object)

    The object



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_allArray

Finds and returns a multiple items within the scope from the DB

Returns:

  • (Array)

    Array of objects



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

Returns:

  • (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

Parameters:

  • resource (object)

    The resource to save



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

#scopeObject

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

Examples:

Overriding scope

class Products::Repository < Cyrax::Repository
  def scope
    accessor.products
  end
end


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