Module: Campchair::Model

Defined in:
lib/campchair/model.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



33
34
35
# File 'lib/campchair/model.rb', line 33

def attributes
  @attributes
end

#keyObject

Returns the value of attribute key.



32
33
34
# File 'lib/campchair/model.rb', line 32

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

#deleteObject Also known as: destroy



57
58
59
60
61
# File 'lib/campchair/model.rb', line 57

def delete
  self.class.delete(key) if persisted?
  @destroyed = true
  freeze
end

#destroyed?Boolean



39
40
41
# File 'lib/campchair/model.rb', line 39

def destroyed?
  @destroyed != false
end

#initialize_modelObject



26
27
28
29
30
# File 'lib/campchair/model.rb', line 26

def initialize_model
  @attributes = {}
  @new_entity = true
  @destroyed  = false
end

#new_entity?Boolean



35
36
37
# File 'lib/campchair/model.rb', line 35

def new_entity?
  @new_entity != false
end

#persisted?Boolean



43
44
45
# File 'lib/campchair/model.rb', line 43

def persisted?
  !(new_entity? || destroyed?)
end

#reload(options = nil) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/campchair/model.rb', line 64

def reload(options = nil)
  fresh_attributes = self.class[key]
  attributes.update(fresh_attributes)
  @new_entity = false

  self
end

#saveObject



47
48
49
50
51
52
53
54
55
# File 'lib/campchair/model.rb', line 47

def save
  if key
    self.class[key] = attributes
  else
    self.key = self.class << attributes
  end
  @new_entity = false
  true
end