swiss_db 0.7.0 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 04bd172ce896a99dd76a23f2670409210a16cbd3
4
- data.tar.gz: 234b633535968c2761a3cba2bc056cec691716d9
3
+ metadata.gz: 4566df7d0cf57f2b234c41c3d6422b83f80385c3
4
+ data.tar.gz: 745d33640c3c028f2312a8ae2cdbe57664447e35
5
5
  SHA512:
6
- metadata.gz: 8a780767d3fed17e7b36f145a678584e48b7d835c799a9af4732d5cf8ec5ce2d7f17cae428b824b713816209f8746980414791a75e64f736f63125b4db85911a
7
- data.tar.gz: cfa8a60bd0dfd045bc8f46823e2373afee309b59734fbe4f26a93ea7c6bf22c30b0930304ff6ca7b7d934d0e90a9f1b7340886acc452404b5f2e37d0d1106836
6
+ metadata.gz: d2f2ef4b40a7c083c48fa403c159f2d6f61af54e5a2097c841dc677b24e5b20dad0712676d6d95318cf71eb34b9ab18a2df6446759332bfb5d278ccc9062a7cf
7
+ data.tar.gz: 6bde8c2b157c12710a6a00985e08341ecd9e4a751aadb8eed9c274972f2b01da19e950b9e010f04bcbf95caca80026d2d2941f6b59c23e15582ed7c7b186f483
data/README.md CHANGED
@@ -91,6 +91,8 @@ end
91
91
 
92
92
  That's it! #all, #last, #first, #count, #save, #update_attribute and the usual are now available!
93
93
 
94
+ As of 0.7.1 all returned objects are SwissModel instances. Model methods will now work properly.
95
+
94
96
  # Planned
95
97
 
96
98
  * update_attributes support
@@ -3,22 +3,22 @@
3
3
  # Convenience methods over the standard cursor
4
4
  # Used by Swiss DataStore
5
5
 
6
- class CursorModel
6
+ # class CursorModel # won't use model properties (custom methods)
7
7
 
8
- def initialize(h)
9
- h.each do |k,v|
10
- instance_variable_set("@#{k}", v)
11
- end
12
- end
8
+ # def initialize(h)
9
+ # h.each do |k,v|
10
+ # instance_variable_set("@#{k}", v)
11
+ # end
12
+ # end
13
13
 
14
- def method_missing(methId, *args)
15
- str = methId.id2name
16
- instance_variable_get("@#{str}")
17
- end
14
+ # def method_missing(methId, *args)
15
+ # str = methId.id2name
16
+ # instance_variable_get("@#{str}")
17
+ # end
18
18
 
19
- end
19
+ # end
20
20
 
21
- class Cursor # < Array
21
+ class Cursor
22
22
 
23
23
  FIELD_TYPE_BLOB = 4
24
24
  FIELD_TYPE_FLOAT = 2
@@ -34,55 +34,63 @@ class Cursor # < Array
34
34
  @values = {}
35
35
  end
36
36
 
37
+ def model
38
+ @model
39
+ end
40
+
37
41
  def first
42
+ return nil if count == 0
38
43
  cursor.moveToFirst ? self : nil
44
+ model.new(to_hash, cursor)
39
45
  end
40
46
 
41
47
  def last
48
+ return nil if count == 0
42
49
  cursor.moveToLast ? self : nil
50
+ model.new(to_hash, cursor)
43
51
  end
44
52
 
45
53
  def [](pos)
54
+ return nil if count == 0
46
55
  cursor.moveToPosition(pos) ? self : nil
56
+ model.new(to_hash, cursor)
57
+ end
58
+
59
+ def to_hash
60
+ hash_obj = {}
61
+ $current_schema[model.table_name].each do |k, v|
62
+ hash_obj[k.to_sym] = self.send(k.to_sym)
63
+ end
64
+ hash_obj
47
65
  end
48
66
 
49
67
  def to_a
68
+ return nil if count == 0
50
69
  arr = []
51
70
  (0...count).each do |i|
52
71
  # puts i
53
- hash_obj = {}
54
72
  cursor.moveToPosition(i)
55
- $current_schema[model.table_name].each do |k, v|
56
- hash_obj[k.to_sym] = self.send(k.to_sym)
57
- end
58
- arr << CursorModel.new(hash_obj)
73
+ arr << model.new(to_hash, cursor)
59
74
  end
60
75
  arr
61
76
  end
62
77
 
78
+ # todo: take out setter code. it's not used anymore. leave the getter code. it is used. (see #to_hash)
79
+
63
80
  def method_missing(methId, *args)
64
81
  method_name = methId.id2name
65
-
66
- if valid_setter_getter?(method_name)
67
- handle_get_or_set(method_name, args)
82
+ # puts "cursor method missing #{method_name}"
83
+ if valid_getter?(method_name)
84
+ get_method(method_name)
68
85
  else
69
86
  super
70
87
  end
71
88
  end
72
89
 
73
- def valid_setter_getter?(method_name)
74
- method_name.chop! if is_setter? method_name
90
+ def valid_getter?(method_name)
75
91
  column_names.include? method_name
76
92
  end
77
93
 
78
- def handle_get_or_set(method_name, args)
79
- if is_setter? method_name
80
- set_method(args)
81
- else
82
- get_method(method_name)
83
- end
84
- end
85
-
86
94
  def is_setter?(method_name)
87
95
  method_name[-1] == '='
88
96
  end
@@ -105,23 +113,6 @@ class Cursor # < Array
105
113
  end
106
114
  end
107
115
 
108
- def set_method(method_name, args)
109
- @values[method_name.chop] = args[0]
110
- end
111
-
112
- def save
113
- primary_key = model.primary_key
114
- pk_value = self.send(primary_key.to_sym)
115
- model.store.update(model.table_name, @values, {primary_key => pk_value})
116
- end
117
-
118
- # we are updating an existing row.. makes more sense on cursor...
119
- def update_attribute(key, value)
120
- primary_key = model.primary_key
121
- pk_value = self.send(primary_key.to_sym)
122
- model.store.update(model.table_name, {key => value}, {primary_key => pk_value})
123
- end
124
-
125
116
  def count
126
117
  cursor.getCount
127
118
  end
@@ -9,7 +9,41 @@ class SwissModel
9
9
  # puts "New subclass: #{subclass.class.name.split('.').last}"
10
10
  # end
11
11
 
12
- # attr_accessor :table_name, :class_name
12
+ attr_accessor :cursor, :values
13
+
14
+ def initialize(h, cursor)
15
+ @cursor = cursor
16
+ h.each do |k,v|
17
+ instance_variable_set("@#{k}", v)
18
+ end
19
+ @values = {}
20
+ end
21
+
22
+ def method_missing(methId, *args)
23
+ str = methId.id2name
24
+ if instance_variable_get("@#{str}")
25
+ return instance_variable_get("@#{str}")
26
+ elsif str[-1] == '=' # setter
27
+ @values[str.chop] = args[0]
28
+ end
29
+ end
30
+
31
+ def save
32
+ pk_value = self.send(self.class.primary_key.to_sym)
33
+ self.class.store.update(self.class.table_name, @values, {self.class.primary_key => pk_value})
34
+ end
35
+
36
+ def update_attribute(key, value)
37
+ pk_value = self.send(self.class.primary_key.to_sym)
38
+ self.class.store.update(self.class.table_name, {key => value}, {self.class.primary_key => pk_value})
39
+ end
40
+
41
+ def update_attributes(hash)
42
+ hash.each do |k, v|
43
+ update_attribute(k, v)
44
+ end
45
+ end
46
+
13
47
 
14
48
  def self.store
15
49
  context = DataStore.context
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swiss_db
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Silverman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-17 00:00:00.000000000 Z
11
+ date: 2015-10-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler