Module: NanoStore::BagInstanceMethods

Included in:
NSFNanoBag
Defined in:
lib/nano_store/bag.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/nano_store/bag.rb', line 3

def self.included(base)
  base.class_eval do
    alias_method :saved, :savedObjects
    alias_method :unsaved, :unsavedObjects
    alias_method :removed, :removedObjects
    alias_method :size, :count
    alias_method :clear, :removeAllObjects
    alias_method :inflate, :inflateBag
    alias_method :deflate, :deflateBag
  end
end

Instance Method Details

#+(object_or_array) ⇒ Object

Add an object or array of objects to bag Return the bag

Raises:



40
41
42
43
44
45
46
47
48
49
# File 'lib/nano_store/bag.rb', line 40

def +(object_or_array)
  error_ptr = Pointer.new(:id)
  if object_or_array.is_a?(Array)
    self.addObjectsFromArray(object_or_array, error:error_ptr)
  else
    self.addObject(object_or_array, error:error_ptr)
  end
  raise NanoStoreError, error_ptr[0].description if error_ptr[0]
  self
end

#-(object_or_array) ⇒ Object

Remove an object or array of objects from bag Return the bag

Raises:



71
72
73
74
75
76
77
78
79
80
# File 'lib/nano_store/bag.rb', line 71

def -(object_or_array)
  error_ptr = Pointer.new(:id)
  if object_or_array.is_a?(Array)
    self.removeObjectsInArray(object_or_array, error:error_ptr)
  else
    self.removeObject(object, error_ptr)
  end
  raise NanoStoreError, error_ptr[0].description if error_ptr[0]
  self
end

#<<(object) ⇒ Object

Add an object to bag

Returns:

  • self

Raises:



31
32
33
34
35
36
# File 'lib/nano_store/bag.rb', line 31

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

#changed?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/nano_store/bag.rb', line 19

def changed?
  self.hasUnsavedChanges
end

#delete(object) ⇒ Object

Remove object from bag



52
53
54
55
# File 'lib/nano_store/bag.rb', line 52

def delete(object)
  self.removeObject(object)
  self
end

#delete_key(key) ⇒ Object

Remove object from bag with key params: key - a key or array of keys



60
61
62
63
64
65
66
67
# File 'lib/nano_store/bag.rb', line 60

def delete_key(key)
  if key.is_a?(Array)
    self.removeObjectsWithKeysInArray(key)
  else
    self.removeObjectWithKey(key)
  end
  self
end

#originalClassStringObject



15
16
17
# File 'lib/nano_store/bag.rb', line 15

def originalClassString
  'NSFNanoBag'
end

#reloadObject

Raises:



96
97
98
99
100
101
# File 'lib/nano_store/bag.rb', line 96

def reload
  error_ptr = Pointer.new(:id)
  result = self.reloadBagWithError(error_ptr)
  raise NanoStoreError, error_ptr[0].description if error_ptr[0]
  result
end

#saveObject

Raises:



88
89
90
91
92
93
94
# File 'lib/nano_store/bag.rb', line 88

def save
  self.store = NanoStore.shared_store unless self.store
  error_ptr = Pointer.new(:id)
  result = self.saveAndReturnError(error_ptr)
  raise NanoStoreError, error_ptr[0].description if error_ptr[0]
  result
end

#store=(store) ⇒ Object

Saving, Reloading and Undoing



84
85
86
# File 'lib/nano_store/bag.rb', line 84

def store=(store)
  store.addObject(self, error:nil)
end

#to_aObject



23
24
25
# File 'lib/nano_store/bag.rb', line 23

def to_a
  self.savedObjects.values + self.unsavedObjects.values
end

#undoObject

Raises:



103
104
105
106
107
108
# File 'lib/nano_store/bag.rb', line 103

def undo
  error_ptr = Pointer.new(:id)
  result = self.undoChangesWithError(error_ptr)
  raise NanoStoreError, error_ptr[0].description if error_ptr[0]
  result
end