Module: ModestModel::Tenacity::ClassMethods
- Defined in:
- lib/modest_model/tenacity.rb
Instance Method Summary collapse
-
#create(attributes = nil, options = {}, &block) ⇒ Object
TODO - docs.
-
#destroy(&block) ⇒ Object
The passed
block
is set to_destroy
and called on destroyed. -
#find(id_ = nil, &block) ⇒ Object
When a block is passed, that block is set to
_find
. -
#save(&block) ⇒ Object
The passed
block
is set to_save
and called on save. -
#set_primary_key(attr) ⇒ Object
Sets the
primary_key
and its associated attribute. -
#undefine_attribute_method(attr) ⇒ Object
:nodoc:.
Instance Method Details
#create(attributes = nil, options = {}, &block) ⇒ Object
TODO - docs
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/modest_model/tenacity.rb', line 59 def create attributes = nil, = {}, &block if attributes.is_a?(Array) attributes.collect { |attr| create(attr, , &block) } else object = new(attributes, ) yield(object) if block_given? object.save object end end |
#destroy(&block) ⇒ Object
The passed block
is set to _destroy
and called on destroyed
76 77 78 |
# File 'lib/modest_model/tenacity.rb', line 76 def destroy &block self._destroy = block if block_given? end |
#find(id_ = nil, &block) ⇒ Object
When a block is passed, that block is set to _find
. When id_
is passed (or no block
is passed) a new record is instantiated with the passed id_
and the _find
block is called. If the return from the _find
block is nil
or false
, a ResourceNotFound error is raised.
48 49 50 51 52 53 54 55 56 |
# File 'lib/modest_model/tenacity.rb', line 48 def find id_=nil, &block if block_given? self._find = block else resource = new(primary_key.to_sym => id_) resource.send(:call_find!) || raise(ModestModel::ResourceNotFound) resource.found end end |
#save(&block) ⇒ Object
The passed block
is set to _save
and called on save
71 72 73 |
# File 'lib/modest_model/tenacity.rb', line 71 def save &block self._save = block if block_given? end |
#set_primary_key(attr) ⇒ Object
Sets the primary_key
and its associated attribute
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/modest_model/tenacity.rb', line 32 def set_primary_key attr undefine_attribute_method :id if attr == :id # Remove id, always if self.primary_key undefine_attribute_method self.primary_key # TODO: remove any validations on the current PK # self._validators.except!(self.primary_key.to_sym) end self.primary_key = attr attributes attr # TODO: add a presence validation on the new PK # validates attr, :presence => true end |
#undefine_attribute_method(attr) ⇒ Object
:nodoc:
25 26 27 28 29 |
# File 'lib/modest_model/tenacity.rb', line 25 def undefine_attribute_method attr # :nodoc: self._attributes = self._attributes - [attr] undef_method attr if method_defined? attr undef_method "#{attr}=" if method_defined? "#{attr}=" end |