Module: Omnidata::Persistence::ClassMethods

Defined in:
lib/omnidata/persistence.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



25
26
27
# File 'lib/omnidata/persistence.rb', line 25

def adapter
  @adapter
end

Instance Method Details

#build_model(pk, attrs) ⇒ Object



33
34
35
36
37
# File 'lib/omnidata/persistence.rb', line 33

def build_model(pk, attrs)
  model = new(attrs)
  model.id = pk
  model
end

#countObject



61
62
63
# File 'lib/omnidata/persistence.rb', line 61

def count
  adapter.count(table_name)
end

#create(attrs) ⇒ Object



46
47
48
49
# File 'lib/omnidata/persistence.rb', line 46

def create(attrs)
  key = adapter.create(table_name, attrs)
  build_model(key, attrs)
end

#destroy(pk) ⇒ Object



56
57
58
59
# File 'lib/omnidata/persistence.rb', line 56

def destroy(pk)
  adapter.destroy(pk, table_name)
  self
end

#find(pk) ⇒ Object



39
40
41
42
43
44
# File 'lib/omnidata/persistence.rb', line 39

def find(pk)
  attrs = adapter.find(pk, table_name)
  if (attrs)
    build_model(pk, attrs)
  end
end

#update(pk, attrs) ⇒ Object



51
52
53
54
# File 'lib/omnidata/persistence.rb', line 51

def update(pk, attrs)
  adapter.update(pk, table_name, attrs)
  self
end

#use_database(name) ⇒ Object



27
28
29
30
31
# File 'lib/omnidata/persistence.rb', line 27

def use_database(name)
  adapter = Adapters::AdapterManager.instance.adapter(name)
  raise Adapters::AdapterError.new("adapter #{name} does not exist") if adapter.nil?
  @adapter = adapter
end

#with_database(name, &block) ⇒ Object



65
66
67
68
69
70
# File 'lib/omnidata/persistence.rb', line 65

def with_database(name, &block)
  old = self.adapter.name
  self.use_database(name)
  block.call
  self.use_database(old)
end