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.



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

def attributes
  @attributes
end

#keyObject

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

#deleteObject 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

Returns:

  • (Boolean)


41
42
43
# File 'lib/campchair/model.rb', line 41

def destroyed?
  @destroyed != false
end

#initialize_modelObject



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

Returns:

  • (Boolean)


37
38
39
# File 'lib/campchair/model.rb', line 37

def new_entity?
  @new_entity != false
end

#persisted?Boolean

Returns:

  • (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(options = nil)
  if fresh_attributes = self.class[key]
    attributes.update(fresh_attributes)
    @new_entity = false
    self
  else
    nil
  end
end

#saveObject



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