Module: Ribs::Repository::ClassMethods

Includes:
Ribs::Repository
Defined in:
lib/ribs/repository.rb

Overview

The ClassMethods are everything that's available when getting the repository for a specific model class. It includes most of the things you'd expect to do on the class itself in ActiveRecord - stuff like finders, creators and things like that.

Instance Attribute Summary

Attributes included from Ribs::Repository

#database, #model

Instance Method Summary collapse

Methods included from Ribs::Repository

const_missing, create_repository, ensure_repository

Instance Method Details

#allObject

Returns all instances for the current model



105
106
107
108
109
# File 'lib/ribs/repository.rb', line 105

def all
  Ribs.with_handle(self.database) do |h|
    h.all(self..persistent_class.entity_name)
  end
end

#create(attrs = {}) ⇒ Object

First creates a model object based on the values in attrs and then saves this to the database directly.



98
99
100
101
102
# File 'lib/ribs/repository.rb', line 98

def create(attrs = {})
  val = new(attrs)
  R(val, self.database).save
  val
end

#define_accessorsObject

Define accessors for this model



60
61
62
63
64
# File 'lib/ribs/repository.rb', line 60

def define_accessors
  self..properties_and_identity.each do |name, _|
    self.model.send :attr_accessor, name.downcase
  end
end

#destroy(id) ⇒ Object

Destroys the model with the id id.



120
121
122
123
124
# File 'lib/ribs/repository.rb', line 120

def destroy(id)
  Ribs.with_handle(self.database) do |h|
    h.delete(get(id))
  end
end

#destroyed(obj) ⇒ Object

Makes a specific instance of this class be marked destroyed



77
78
79
# File 'lib/ribs/repository.rb', line 77

def destroyed(obj)
  (@destroyed ||= {})[obj.object_id] = true
end

#destroyed?(obj) ⇒ Boolean

Checks if a specific instance is marked as destroyed

Returns:

  • (Boolean)


82
83
84
# File 'lib/ribs/repository.rb', line 82

def destroyed?(obj)
  @destroyed && @destroyed[obj.object_id]
end

#get(id) ⇒ Object

Will get the instance with id or return nil if no such entity exists.



113
114
115
116
117
# File 'lib/ribs/repository.rb', line 113

def get(id)
  Ribs.with_handle(self.database) do |h|
    h.get(self..persistent_class.entity_name, id)
  end
end

#metadataObject

Get the meta data for this model



55
56
57
# File 'lib/ribs/repository.rb', line 55

def 
  
end

#new(attrs = {}) ⇒ Object

Create a new instance of this model object, optionally setting properties based on attrs.



88
89
90
91
92
93
94
# File 'lib/ribs/repository.rb', line 88

def new(attrs = {})
  obj = self.model.new
  attrs.each do |k,v|
    obj.send("#{k}=", v)
  end
  obj
end

#persistent(obj) ⇒ Object

Makes a specific instance of this class be marked persistent



67
68
69
# File 'lib/ribs/repository.rb', line 67

def persistent(obj)
  (@persistent ||= {})[obj.object_id] = true
end

#persistent?(obj) ⇒ Boolean

Checks if a specific instance is marked as persistent

Returns:

  • (Boolean)


72
73
74
# File 'lib/ribs/repository.rb', line 72

def persistent?(obj)
  @persistent && @persistent[obj.object_id]
end