Module: NanoStore::ModelInstanceMethods

Included in:
Model
Defined in:
lib/nano_store/model.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/nano_store/model.rb', line 21

def method_missing(method, *args)
  matched = method.to_s.match(/^([^=]+)(=)?$/)
  name = matched[1]
  modifier = matched[2]

  if self.class.attributes.include?(name.to_sym)
    if modifier == "="
      if args[0].nil?
        self.info.delete(name.to_sym)
      else
        self.info[name.to_sym] = args[0]
      end
    else
      self.info[name.to_sym]
    end
  else
    super
  end
end

Instance Method Details

#deleteObject

Raises:



12
13
14
15
16
17
18
19
# File 'lib/nano_store/model.rb', line 12

def delete
  raise NanoStoreError, 'No store provided' unless self.class.store

  error_ptr = Pointer.new(:id)
  self.class.store.removeObject(self, error: error_ptr)
  raise NanoStoreError, error_ptr[0].description if error_ptr[0]
  self
end

#saveObject

Raises:



3
4
5
6
7
8
9
10
# File 'lib/nano_store/model.rb', line 3

def save
  raise NanoStoreError, 'No store provided' unless self.class.store

  error_ptr = Pointer.new(:id)
  self.class.store.addObject(self, error:error_ptr)
  raise NanoStoreError, error_ptr[0].description if error_ptr[0]
  self
end