Class: SwissModel

Inherits:
Object show all
Defined in:
lib/swiss_db/swiss_model.rb

Overview

Swiss Model An ActiveRecord like Model for RubyMotion Android

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(h, cursor) ⇒ SwissModel

Returns a new instance of SwissModel.



14
15
16
17
18
19
20
# File 'lib/swiss_db/swiss_model.rb', line 14

def initialize(h, cursor)
  @cursor = cursor
  h.each do |k,v|
    instance_variable_set("@#{k}", v)
  end
  @values = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(methId, *args) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/swiss_db/swiss_model.rb', line 22

def method_missing(methId, *args)
  str = methId.id2name
  if instance_variable_get("@#{str}")
    return instance_variable_get("@#{str}")
  elsif str[-1] == '=' # setter
    @values[str.chop] = args[0]
  end
end

Instance Attribute Details

#cursorObject

meh? .. won’t work for now in java… created classes become java packages name will become the namespace of the package… def self.inherited(subclass)

puts "New subclass: #{subclass.class.name.split('.').last}"

end



12
13
14
# File 'lib/swiss_db/swiss_model.rb', line 12

def cursor
  @cursor
end

#valuesObject

meh? .. won’t work for now in java… created classes become java packages name will become the namespace of the package… def self.inherited(subclass)

puts "New subclass: #{subclass.class.name.split('.').last}"

end



12
13
14
# File 'lib/swiss_db/swiss_model.rb', line 12

def values
  @values
end

Class Method Details

.allObject



79
80
81
82
83
# File 'lib/swiss_db/swiss_model.rb', line 79

def self.all
  # select_all
  cursor = store.select_all(@table_name, self)
  cursor
end

.class_nameObject



54
55
56
# File 'lib/swiss_db/swiss_model.rb', line 54

def self.class_name
  @class_name
end

.create(obj) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/swiss_db/swiss_model.rb', line 103

def self.create(obj)
  # create a row
  result = store.insert(@table_name, obj)
    if result == -1
      puts "An error occured inserting values into #{@table_name}"
    else
      return result
    end
end

.destroy_all!Object

def destroy

# destroy this row

end



117
118
119
120
# File 'lib/swiss_db/swiss_model.rb', line 117

def self.destroy_all!
  # destroy all of this kind (empty table)
  store.destroy_all(@table_name)
end

.firstObject



91
92
93
94
95
# File 'lib/swiss_db/swiss_model.rb', line 91

def self.first
  # select all and get first
  cursor = all.first
  cursor
end

.lastObject



97
98
99
100
101
# File 'lib/swiss_db/swiss_model.rb', line 97

def self.last
  # select all and get last
  cursor = all.last
  cursor
end

.primary_keyObject



75
76
77
# File 'lib/swiss_db/swiss_model.rb', line 75

def self.primary_key
  @primary_key.nil? ? "id" : @primary_key
end

.set_class_name(class_name) ⇒ Object

hack, class.name not functioning in RM Android…



58
59
60
61
# File 'lib/swiss_db/swiss_model.rb', line 58

def self.set_class_name(class_name) # hack, class.name not functioning in RM Android...
  @class_name = class_name
  set_table_name(class_name.tableize)
end

.set_primary_key(primary_key) ⇒ Object



71
72
73
# File 'lib/swiss_db/swiss_model.rb', line 71

def self.set_primary_key(primary_key)
  @primary_key = primary_key
end

.set_table_name(table_name) ⇒ Object



63
64
65
# File 'lib/swiss_db/swiss_model.rb', line 63

def self.set_table_name(table_name)
  @table_name = table_name
end

.storeObject



48
49
50
51
52
# File 'lib/swiss_db/swiss_model.rb', line 48

def self.store
  context = DataStore.context
  @store ||= DataStore.new(context)
  @store
end

.table_nameObject



67
68
69
# File 'lib/swiss_db/swiss_model.rb', line 67

def self.table_name
  @table_name
end

.where(values) ⇒ Object



85
86
87
88
89
# File 'lib/swiss_db/swiss_model.rb', line 85

def self.where(values)
  # select <table> where <field> = <value>
  cursor = store.select(@table_name, values, self)
  cursor
end

Instance Method Details

#saveObject



31
32
33
34
# File 'lib/swiss_db/swiss_model.rb', line 31

def save
  pk_value = self.send(self.class.primary_key.to_sym)
  self.class.store.update(self.class.table_name, @values, {self.class.primary_key => pk_value})
end

#update_attribute(key, value) ⇒ Object



36
37
38
39
# File 'lib/swiss_db/swiss_model.rb', line 36

def update_attribute(key, value)
  pk_value = self.send(self.class.primary_key.to_sym)
  self.class.store.update(self.class.table_name, {key => value}, {self.class.primary_key => pk_value})
end

#update_attributes(hash) ⇒ Object



41
42
43
44
45
# File 'lib/swiss_db/swiss_model.rb', line 41

def update_attributes(hash)
  hash.each do |k, v|
    update_attribute(k, v)
  end
end