Module: NanoStore::ModelClassMethods

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

Instance Method Summary collapse

Instance Method Details

#attribute(name) ⇒ Object



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

def attribute(name)
  attributes << name

  define_method(name) do |*args, &block|
    self.info[name]
  end

  define_method((name + "=").to_sym) do |*args, &block|
    self.info[name] = args[0]
  end
end

#attributes(*attrs) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/nano_store/model.rb', line 57

def attributes(*attrs)
  if attrs.size > 0
    attrs.each{|attr| attribute attr}
  else
    @attributes ||= []
  end
end

#countObject



76
77
78
# File 'lib/nano_store/model.rb', line 76

def count
  self.store.count(self)
end

#create(data = {}) ⇒ Object

initialize a new object and save it



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

def create(data={})
  object = self.new(data)
  object.save
end

#delete(*args) ⇒ Object



80
81
82
83
# File 'lib/nano_store/model.rb', line 80

def delete(*args)
  keys = find_keys(*args)
  self.store.delete_keys(keys)
end

#new(data = {}) ⇒ Object

initialize a new object



28
29
30
31
32
33
34
35
36
37
# File 'lib/nano_store/model.rb', line 28

def new(data={})
  data.keys.each { |k|
    unless self.attributes.member? k.to_sym
      raise NanoStoreError, "'#{k}' is not a defined attribute for this model"
    end
  }

  object = self.nanoObjectWithDictionary(data)
  object
end

#storeObject



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

def store
  if @store.nil?
    return NanoStore.shared_store 
  end
  @store
end

#store=(store) ⇒ Object



72
73
74
# File 'lib/nano_store/model.rb', line 72

def store=(store)
  @store = store
end