Module: Campchair::Model
- Defined in:
- lib/campchair/model.rb
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#key ⇒ Object
Returns the value of attribute key.
Class Method Summary collapse
Instance Method Summary collapse
- #delete ⇒ Object (also: #destroy)
- #destroyed? ⇒ Boolean
- #initialize_model ⇒ Object
- #new_entity? ⇒ Boolean
- #persisted? ⇒ Boolean
- #reload(options = nil) ⇒ Object
- #save ⇒ Object
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
35 36 37 |
# File 'lib/campchair/model.rb', line 35 def attributes @attributes end |
#key ⇒ Object
Returns the value of attribute key.
34 35 36 |
# File 'lib/campchair/model.rb', line 34 def key @key end |
Class Method Details
.included(base) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/campchair/model.rb', line 3 def self.included(base) base.class_eval do include Store extend ClassMethods class << self _new = instance_method(:new) define_method(:new) do |*args| _new.bind(self).call(*args).tap(&:initialize_model) end end end end |
Instance Method Details
#delete ⇒ Object Also known as: destroy
59 60 61 62 63 |
# File 'lib/campchair/model.rb', line 59 def delete self.class.delete(key) if persisted? @destroyed = true freeze end |
#destroyed? ⇒ Boolean
41 42 43 |
# File 'lib/campchair/model.rb', line 41 def destroyed? @destroyed != false end |
#initialize_model ⇒ Object
28 29 30 31 32 |
# File 'lib/campchair/model.rb', line 28 def initialize_model @attributes = {} @new_entity = true @destroyed = false end |
#new_entity? ⇒ Boolean
37 38 39 |
# File 'lib/campchair/model.rb', line 37 def new_entity? @new_entity != false end |
#persisted? ⇒ Boolean
45 46 47 |
# File 'lib/campchair/model.rb', line 45 def persisted? !(new_entity? || destroyed?) end |
#reload(options = nil) ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/campchair/model.rb', line 66 def reload( = nil) if fresh_attributes = self.class[key] attributes.update(fresh_attributes) @new_entity = false self else nil end end |
#save ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/campchair/model.rb', line 49 def save if key self.class[key] = attributes else self.key = self.class << attributes end @new_entity = false true end |